DOC

Java as a second language

By Maria Alexander,2014-04-24 12:40
15 views 0
Java as a second language

Lecture 5

Overview of Lecture

    ? Wang

    ? Chapter 2 Sections 1-8,11

    ? Chapter 4 Sections

    ? 1-16

    ? Encapsulation

    ? Abstract Data Types

    ? Access Modifiers I

    ? Package Access

    ? Constructors

    ? Composition

    ? Inheritance and Polymorphism ? Subclass Superclass Relationships

    ? Abstract Superclasses and Interfaces

     CS3773 Lecture 5 Page 1 of 26

Lecture 5

Encapsulation

    ? Objects encapsulate data and methods

    ? Data represents an objects attributes

    ? Methods represent an objects behavior ? Information Hiding

    ? We interact with objects using the public methods of the object

    ? The public methods of a class are said to define the “interface”

    to the class

    ? We do not need to know implementation details of private data

    members or methods

    ? In a Stack class is the implementation done as an array

    or as a linked list?

    ? We really don’t care!

     CS3773 Lecture 5 Page 2 of 26

Lecture 5

Abstract Data Types

    ? Java provides us with a set of primitive data types

    ? These primitive data types are the atoms available to Java

    Programmers

    ? An abstract data type is a programmer defined type

    (class) designed to model some real world item

    ? Generally composed of other abstract data types and primitive

    types

    ? We can define abstract data types to represent any item

    that can be described by a set of attributes

    ? Example: A Time abstract data type

    ? Time is logically composed of hours, minutes, and seconds

    ? Can’t be expressed easily using a primitive data type

    ? Hides the actual storage mechanism

    ? private instance variables: hour, minute, second ? Typical of instance variables (i.e. they are private)

    ? public methods provide the interface to the class ? Time() constructor to create Time objects ? setTime() to modify, or mutate, Time objects ? toString() to provide a String representation

     CS3773 Lecture 5 Page 3 of 26

Lecture 5

    ? The Object class is the root class for all objects in Java

    ? Classes that do not explicitly extend (inherit from) any other

    class, implicitly extend Object

    ? The Object class defines the toString() method (among

    others)

    ? System.out is a PrintStream object

    ? A PrintStream knows how to print all of the primitive data types

    as well as the String class and the Object class

    ? To print an Object, PrintStream invokes the toString()

    method of the Object class

    ? If a derived class overrides toString() , then the

    newly defined toString() method is called rather than

    Object.toString().

    ? Result: No need to define friend functions, as in C++, to

    provide standard output functionality

     CS3773 Lecture 5 Page 4 of 26

Lecture 5

Controlling Access to Members

    ? Member access modifiers public, protected,

    private

    ? Permits or restricts access to class variables and

    methods from outside class scope

    ? The public modifier permits clients (users) of a class to

    directly access an instance variable, or invoke a method

    ? Can be dangerous

    ? Provides unchecked access to instance variables (i.e. no error

    checking)

    ? May lead to an object in an invalid state

    ? What if users could modify the length attribute of an array?

    ? The private modifier prevents clients from directly

    accessing methods and variables

    ? Private variable can only be accessed within class scope

    ? Only class methods may directly read or write private

    variables

    ? Should provide get and set methods to allow clients to

    indirectly read or write private variables

     CS3773 Lecture 5 Page 5 of 26

Lecture 5

    Using get and set Methods ? It is generally not a good idea to make instance variables

    public

    ? We can not necessarily guarantee that our object will

    remain in a consistent state

    ? What if the “minutes” instance variable in a Time class

    was public?| Time t = new Time();

    t.minutes = 73; // not a good idea

    ? Making minutes private and providing a setMinutes

    method allows us to validate changes to minutes

    public void setMinute(int m) {

     minute = (m >= 0 && m < 60) ? m : 0;

    }

    ? The above version handles errors by rejecting bad data and

    substituting a default value

    public void setMinute(int m) {

     if (m >= 0 && m < 60) {

     minute = m; ? m : 0;

     }

     else {

     minute = m % 60;

     setHour(hour + m / 60);

     }

    }

    ? The second version above takes a different approach to error

    handling

    ? In either case, minute will never have an invalid value, this is

    the benefit of set methods

    ? get methods allow us to hide implementation details and return

    information to a client in a more natural format

     CS3773 Lecture 5 Page 6 of 26

     Lecture 5

    ? Example: MSWindows encodes dates and times into a

    double ? date is encoded into the integer portion

    ? time is encoded into the fractional portion

    ? This makes extracting “minutes” more complicated than simply

    returning the value of a minutes instance variable ? Summary of get & set benefits:

    ? hide implementation details of internal representation ? provide error checking before making actual changes so that

    object remains in a valid state

     CS3773 Lecture 5 Page 7 of 26

     Lecture 5

    Package Access

    ? The three access specifiers we have seen so far are

    private, protected and public

    ? When we omit the access specifier from ANY declaration

    (class, variable, method) that item is considered to have

    “package” access

    ? Package access allows classes in the same package to

    access data members and methods without the need for

    set and get methods

    ? Think of this as a form of limited public access. ? The declared item has public access as far as other classes in

    the package are concerned

    ? The item has private access as far as classes outside the

    package are concerned

    ? Note: Classes that are not declared to belong to a particular

    package belong to the global “unnamed” package.

     CS3773 Lecture 5 Page 8 of 26

     Lecture 5

    package demo;

public class PublicDemo {

     int x; //package access

     public PublicDemo() {

     PackageDemo p = new PackageDemo();

     }

    }

    ----------------------------------------- package demo;

class PackageDemo {

     public PackageDemo() {

     PublicDemo p = new PublicDemo();

     p.x = 3; //legal same package

     }

    }

    Example: The demo package above

    ? Two classes PublicDemo and PackageDemo

    ? Note the class declarations PackageDemo is not public

    import demo.*; //import all public classes

     //from the demo package

class Test {

     public Test() {

     PublicDemo pub = new PublicDemo();

     pub.x = 7; //illegal

     //next line illegal also

     PackageDemo pack = new PackageDemo();

     }

    }

     CS3773 Lecture 5 Page 9 of 26

Lecture 5

    Initializing Class Objects: Constructors

    ? All objects have a constructor

    ? If you do not define one, the compiler will generate a default no-

    argument constructor

    ? If you define at least one constructor, the compiler will NOT

    provide a no-arg constructor (you may define your own however) ? Constructors may be public, protected or private

    ? Constructors never have a return type

    ? Special method that is invoked whenever an object is

    created in order to perform initialization

    ? Constructor has the same name as the class it

    constructs

    Object Creation

    ? When objects are created the following steps occur:

    ? 1. The constructor for the object’s superclass is called, either

    implicitly or explicitly

    ? Remember, at a minimum, all objects derive from class Object

    ? 2. The object’s instance variables are initialized

    ? 3. The statements in the constructor are executed

     CS3773 Lecture 5 Page 10 of 26

Report this document

For any questions or suggestions please email
cust-service@docsford.com