Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Writing and using Classes effectively.

Similar presentations

Presentation on theme: "Inheritance Writing and using Classes effectively."— Presentation transcript:

1 Inheritance Writing and using Classes effectively

2 OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Write a subclass that extends a superclass Decide what belongs in superclass vs. subclass Write a method that overrides a superclass method Demonstrate the priority of methods of superclasses and subclasses

3 Vocabulary & Terms The words below will be covered in the following slides: inherit/inheritance extends superclass subclass this super override

4 Many people have pets, they’re nice. There are some things that all pets have in common. Give some thought to those things, and see some of them suggested on the next page.

5 What do all pets have? -Name (String) -Age (int) -Ability to speak The following slide has an example of the Pet class.

6 public class Pet { //variables private int age; private String name; //constructor public Pet() { age = 1; name = "Fido"; } //methods public void setName(String aName) { = aName; } public String getName() { return name; } public void setAge(int ageNumber) { this.age = ageNumber; } public int getAge() { return this.age; } public String speak() { return "Hi there"; } } Compile this in DrJava, and then create a new object in the interactions panel and make it speak: Pet mine = new Pet(); System.out.println(mine.speak());

7 If you performed the last slide correctly, your Pet said “Hi there”. Pets don’t normally say “Hi there”. They usually speak unique sounds. Our next class “is a type of” Pet: a Bird. The following page is the Bird class.

8 public class Bird extends Pet { //constructor public Bird() { super(); } //methods public String speak() { return "Tweet Tweet"; } See the words “extends Pet” ? That means if Pet does something that you like, you can just use what Pet has, and you don’t have to rewrite it. Because Pet does it, you don’t have to write the setAge or getAge, setName or getName functions. Because you used the words “extends Pet”, you “inherit” everything Pet does!

9 public class Bird extends Pet { //constructor public Bird() { super(); } //methods public String speak() { return "Tweet Tweet"; } If you don’t like something Pet does, then you can rewrite it in Bird, like the speak() method. We didn’t want our Bird to say “Hi there” like the general Pet, we want it to say “Tweet Tweet”. Our Bird speak() method overrides the Pet speak() method. The other methods of Pet still work!

10 Pet is the superclass, Bird is the subclass. Compile Bird and Pet in DrJava. Create new Pet and Bird objects, then use the setAge(), speak(), and getAge() methods on each: Pet a = new Pet(); Bird b = new Bird(); a.setAge(3); b.setAge(5); System.out.println(a.getAge()); System.out.println(b.getAge()); System.out.println(a.speak()); System.out.println(b.speak()); Bird doesn’t have setAge() or getAge(), it uses the ones “inherited” from Pet!

11 Because your Bird is a “subclass”, and the Pet is the “superclass”, you can connect the Bird and Pet still. If you want the Bird class to have a method to use the Pet’s “speak” method, you need to use the word “super” to refer to it. The following slide has the method.

12 (In the Bird class) public String parentSpeak() { return super.speak(); } “super.speak()” is the reference from the Bird to the “speak()” method of the parent, Pet. Add this to your Bird class, and try using it!

13 public Bird() { super(); } You may have also noticed that super() is inside the constructor for Bird. This is a common habit if your constructor has nothing else. It helps to create the variables, etc., for your subclass.

14 What if your Bird class and Pet class and Dog class all use the same name for something? How do we avoid conflicts and confusion? For example, if your Bird has an age variable, and your Pet has an age variable, how is it ordered?

15 Your computer will assume that you want the variable or method of the object that calls it. For example, if your Bird has an age variable, and “bill” is a Bird, bill.getAge() should look for the age in Bird. But, you can use the “this” keyword to make sure.

16 Assume Pet has an “age” and Bird has an “age”. public int getAge() { return age; } in the Bird class assumes the age in Bird. To be explicit, you can say: public int getAge() { return this.age; }

17 Assume Pet has an “age” and Bird has an “age”. If you wanted to explicitly reference the age variable of the Pet (super) class, you would type it this way: public int getAge() { return super.getAge(); } (Use super for the superclass’ stuff, and this for the class’ stuff)

18 What order does it follow by default? If a subclass (like Bird) has a method (like setAge), then it would be called before the super’s method (like Pet’s setAge). If a subclass does not have a method, but the super does, then the super’s method is called automatically.

19 When writing your programs, think it through first, and you can save a lot of time. Think of all of the “things”, and if they have anything in common. If so, you can create a “superclass” that holds many of those things. If you then want to add a specialized subclass, you can add variables, methods, and even override methods!

Download ppt "Inheritance Writing and using Classes effectively."

Similar presentations

Ads by Google