rit computer science dept. goals l inheritance l modifiers: private, public, protected l...

25
RIT Computer Science Dept. Goals Inheritance Modifiers: private, public, protected Polymorphism

Upload: hillary-blankenship

Post on 13-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Goals

Inheritance Modifiers: private, public, protected Polymorphism

Page 2: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Question from Review

Why do you have to cast an object (such as a String or Integer) being popped off of a stack and not an object (such as a String or Integer) being pushed onto the stack?

Page 3: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

The Answer

A String is a type of Object Is an Object a type of String?

– No! This is why you have to tell the compiler what type of Object it’s dealing with

– Note: You could ask the same about mammals and humans: A human is a type of mammal, but a mammal is not a type of human

Page 4: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Inheritance

Inheritance is a relationship where one class shares the structure or behaviors defined in another class

Page 5: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Why would you wantinheritance?

Code re-use Provides a way to specify some

properties/behaviors that all subclasses must exhibit

It’s an appropriate way to represent the “is-a-kind-of” relationship between classes

Inheritance also provides the ability to generalize -- a method can be written to work with the super-class but subclasses can be passed as arguments

Page 6: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

The Java Class Hierarchy

The root of the class hierarchy is the Object class

Every class (except the root) has exactly one parent

The parent is the (direct) superclass of the child

The children are (direct) subclasses of the parent.

Page 7: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Short InheritanceQuestions

If class Tornado inherits from class Storm, mark the following statements as true or false:

– There are more objects that conform to the type Tornado than there are that conform to the type Storm.

– Tornado instances probably have more fields (attributes) than Storm instances, and definitely not fewer.

– All methods declared in Storm can be called on instances of Tornado.

– All methods declared in Storm will perform the same way whether they are invoked on instances of Storm or of Tornado.

Page 8: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Inheritance Examples

[See handout]

Page 9: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Write a class that uses inheritance

Using the superclass Goose given in class, write a subclass for your favorite type of Goose and the noise that your favorite Goose makes.

Page 10: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

To Do

Name 5 classes that could be derived from Goose

Which is the correct assignment?: Goose myGoose = new GrannyGoose(); GrannyGoose myCat = new Goose();

Page 11: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Method Overriding

A subclass inherits everything from its superclass.

However, a subclass can override methods as shown in the GrannyGoose example [see handout]

A subclass can also add variables and methods

Page 12: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Comments about super()

super() will call the constructor for the superclass of the class it is called in

It must be the first call in a constructor (or else Java gives a syntax error)

If you do not call super() yourself, Java will automatically add the call at the beginning of the constructor

It’s better to call super() yourself rather than to depend on the default behavior that Java currently has

Page 13: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Why won’t this compile?

public class TalkingGoose extends Goose {

public TalkingGoose ( String name ) {super( name, true );

}

// have this goose speak its namepublic void makeNoise ( ) {

System.out.print( name );}

} // TalkingGoose

Page 14: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Access

Most classes provide three levels of access to their members (state and behavior):

Public: can be accessed by the methods in a class, in its subclasses, and by other classes.

Protected: can only be accessed by methods in the class, in its subclasses, and in classes in the same package. (Note: Wu doesn’t include the info. about packages – see page 668)

Private: can only be accessed by methods in the class.

Page 15: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Packages

Most classes are grouped into packages– For example, java.util is a package– Packages are like libraries of classes

If you don’t specify what package your class belongs to, it will belong to an unnamed default package

Page 16: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Abstract Classes

An abstract class is a class from which no instances can be generated.

Abstract classes can contain abstract methods: methods without implementation

Page 17: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Abstract Class Example

[See handout]

Page 18: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Polymorphism

In its simplest form, polymorphism allows a variable of type X to refer to any object that is an instance of X or an instance of a subclass of X.

Page 19: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Polymorphism (cont’d)

All classes are descendants of Object. So, a variable of type Object can refer to an instance of any class.

Using a Stack, this is why we can call

push with a String argument, or with an Integer argument, or with a Rectangle argument, etc.

Page 20: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

A Polymorphic Method Call

class TestGoose3 {

public static void main ( String[] args ) {Goose aGoose;GrannyGoose myGoose = new GrannyGoose( “Snookums” );Object anotherGoose;WolfGoose wolfie = new WolfGoose( “Tigger” );

if ( Math.random() < 0.5 ) {aGoose = myGoose;

} else {aGoose = wolfie;

}

Page 21: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

(cont’d)

// The next line is a polymorphic method call:// Which method gets called depends on the type of // object that aGoose refers to. This will be// determined during run-time.aGoose.makeNoise();anotherGoose = aGoose;

// This line won’t compile: why?anotherGoose.makeNoise();

}} // TestGoose3

Page 22: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Yet more casting

This won’t compile:

This will compile, but will throw an exception/error:

Goose aGoose = new WildCat( “Tigger” );WolfGoose myGoose = aGoose;

Goose aGoose = new GrannyGoose( “Selma” );WolfGoose myGoose = (WolfGoose) aGoose;

Page 23: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Using Inheritance

Use inheritance to implement an “is-a” relationship

– If A is a B, then make A a subclass of B– GrannyGoose is a type of Goose, so it is a subclass of

Goose

Do not use inheritance to implement a “has-a” relationship

– For example, a bird has feathers, a beak, toys, etc. These things should NOT be subclasses of Bird

– This type of relationship is implemented by Bird containing an instance of Toy or Feathers and is called composition

Page 24: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

How Final Works onClasses and Methods

A method may be declared as final. This means that the method cannot be overridden in a subclass.

A class may also be declared as final. Such a class cannot be subclassed.

Page 25: RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism

RIT Computer Science Dept.

Why final?

Why would you make a class final for security reasons?