Introduction To Java - MFC 158 G

Week 9 Lecture notes - Fall 2000

 

Chapter 9 -  Object Oriented Programming- MFC 158 (1 of 2)

 

 

Inheritance – a form of software reusability in which we derive new classes from existing ones.

 

Polymorphism - enables us to write programs in a general fashion to handle a wide variety of existing and future classes.

 

Superclass – direct (using extends)  indirect (using extends of extends)

 

 

                            Person

 

            Student                    Employee

 

    Grad      Undergrad    Faculty       Staff

 

 

Protected members

 Superclass public members – accessible anywhere there is a reference in the program

 Superclass private members – accessible only by the methods in the superclass

 Superclass protected members – accessible by members of the superclass, subclass and methods in the same package.

 

 

Work in groups – Analysis of Fig 9.4

-          sketch out the hierarchy

-          understand the code

 

Constructors and finalizers in subclasses

 

-          when a subclass is instantiated, the superclass’s constructor should be called to do any initiation (if not, the default constructor is called)

-          Superclass constructors are not inherited into subclasses, but can be called via the super reference.

-          If using finalize in a subclass, be sure to call the superclass finalize member to assure that all parts of an object are finalized.

 

 

Inheritance

 

-          use it to create a new class from an existing class, but with extensible qualities (attributes and behaviors)

-          the superclass specifies commonality.  All classes derived from it inherit all the qualities.

-          If classes produced through inheritance are larger than they need to be, memory and processing resources may be wasted.

-          Inherit from the class ‘closest’ to what you need

-          In  OO systems, classes are closely related.  Factor out the common attributes and behaviors and place in a superclass.

 

 

 

 

 

 

 

Polymorphism  and Dynamic method binding

 

We can avoid polymorphism by using conditional logic (if type=circle do this, etc.)

 

 

If we had a set of shape classes such as: Circle, Triangle, Rectangle, Square, etc. and we wanted to have a draw method for each situation.  We could do the following:

 

-          create a superclass Shape

-          create an abstract draw() method

-          create a subclass for each shape.

-          create a draw method for each subclass (Circle, Triangle, etc.) that would draw the particular shape.

-          let the program determine (at run time) which method to be called.

 

 

Interfaces

 

Public class Point extends Object implements Shape {

 

}

 

-          can be used in place of an abstract superclass.

-          A class that implements an interface MUST define every method in the interface with the exact return types and arguments (used in the interface).

-          Implementing an interface is like signing a contract with the compiler stating “I will define all methods specified by the interface”.

-          Typically used in place of an abstract class when there is no default implementation to inherit or instance variables.

 

 

Work in groups – Analysis of Fig 9.9

-          sketch out the hierarchy

-          understand the code