Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism."— Presentation transcript:

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

2 RIT Computer Science Dept. Question from Review l 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?

3 RIT Computer Science Dept. The Answer l A String is a type of Object l 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

4 RIT Computer Science Dept. Inheritance l Inheritance is a relationship where one class shares the structure or behaviors defined in another class

5 RIT Computer Science Dept. Why would you want inheritance? l Code re-use l Provides a way to specify some properties/behaviors that all subclasses must exhibit l It’s an appropriate way to represent the “is-a- kind-of” relationship between classes l 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

6 RIT Computer Science Dept. The Java Class Hierarchy l The root of the class hierarchy is the Object class l Every class (except the root) has exactly one parent l The parent is the (direct) superclass of the child l The children are (direct) subclasses of the parent.

7 RIT Computer Science Dept. Short Inheritance Questions l 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.

8 RIT Computer Science Dept. Inheritance Examples l [See handout]

9 RIT Computer Science Dept. Write a class that uses inheritance l Using the superclass Goose given in class, write a subclass for your favorite type of Goose and the noise that your favorite Goose makes.

10 RIT Computer Science Dept. To Do l Name 5 classes that could be derived from Goose l Which is the correct assignment?: n Goose myGoose = new GrannyGoose(); n GrannyGoose myCat = new Goose();

11 RIT Computer Science Dept. Method Overriding l A subclass inherits everything from its superclass. l However, a subclass can override methods as shown in the GrannyGoose example [see handout] l A subclass can also add variables and methods

12 RIT Computer Science Dept. Comments about super() l super() will call the constructor for the superclass of the class it is called in l It must be the first call in a constructor (or else Java gives a syntax error) l If you do not call super() yourself, Java will automatically add the call at the beginning of the constructor l It’s better to call super() yourself rather than to depend on the default behavior that Java currently has

13 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 name public void makeNoise ( ) { System.out.print( name ); } } // TalkingGoose

14 RIT Computer Science Dept. Access l Most classes provide three levels of access to their members (state and behavior): n Public: can be accessed by the methods in a class, in its subclasses, and by other classes. n 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) n Private: can only be accessed by methods in the class.

15 RIT Computer Science Dept. Packages l Most classes are grouped into packages –For example, java.util is a package –Packages are like libraries of classes l If you don’t specify what package your class belongs to, it will belong to an unnamed default package

16 RIT Computer Science Dept. Abstract Classes l An abstract class is a class from which no instances can be generated. l Abstract classes can contain abstract methods: methods without implementation

17 RIT Computer Science Dept. Abstract Class Example l [See handout]

18 RIT Computer Science Dept. Polymorphism l 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.

19 RIT Computer Science Dept. Polymorphism (cont’d) l All classes are descendants of Object. So, a variable of type Object can refer to an instance of any class. l 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.

20 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; }

21 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

22 RIT Computer Science Dept. Yet more casting l This won’t compile: l 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;

23 RIT Computer Science Dept. Using Inheritance l 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 l 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

24 RIT Computer Science Dept. How Final Works on Classes and Methods l A method may be declared as final. This means that the method cannot be overridden in a subclass. l A class may also be declared as final. Such a class cannot be subclassed.

25 RIT Computer Science Dept. Why final? l Why would you make a class final for security reasons?


Download ppt "RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism."

Similar presentations


Ads by Google