Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7- Inheritance.

Similar presentations


Presentation on theme: "Chapter 7- Inheritance."— Presentation transcript:

1 Chapter 7- Inheritance

2 Overview What is inheritance? How to inherit from other classes
Overloading and overriding Inheritance details Review

3 What is inheritance?

4 Inheritance defined- The birds and the bees.
Inheritance is the process of automatically taking on properties of an ancestor: You inherited genes from your parents. Organisms inherit their species from ancestors. Classes can also have inheritance.

5 Class inheritance. When class B inherits characteristics from class A, class B receives all the variables and public methods that class A had. Class B can still have more methods and variables, but it has all of A’s stuff as bonus. Class B can also modify the methods it receives from A to better suit its needs.

6 Class inheritance. The ancestors are often called “parents” and the classes that receive the inheritance are called “children.” Also sometimes called “base class” and “derived class”, respectively. Usually inheritance of classes creates a kind of family tree that goes from a general class down to a more specific class that is actually useful. There can be multiple levels in this tree.

7 Class Inheritance. Parent Child Child

8 Class Inheritance example
Shape Polygon Squiggly Quadri- lateral Triangle Rectangle Square Rhombus

9 Class Inheritance Example:
Person Student Employee Under- graduate Graduate Faculty Staff Masters PhD NonDegree

10 How to inherit from other classes.

11 How to inherit. To have class A inherit from class B, we only have to add two words to the class declaration of A: “extends B” public class Student extends Person {…} public class Square extends Rectangle public class Triangle extends Polygon

12 What does extending do? When we inherit from class B, we take into our class all of the following: B’s variables Public variables of B Private variables of B B’s methods Only the public methods of B.

13 What does that mean? Say that Shape had a private variable called color and public methods setColor and getColor. Then Triangle, Rectangle, Square, and any other class that inherits from Shape also have a private variable called color and public methods setColor and getColor. If Person had a private name variable, then all of the Students and Employees would automatically have a name variable.

14 A trick to get access to private variables?
We can’t use inheritance to give us access to another class’s private variables. In the previous examples, color was private, so Rectangle would have inherited the color variable, but it would not have direct access to the variable. It would have to go through the public methods getColor and setColor. We can have direct access to public variables though.

15 Private variable example.
Rectangle a = new Rectangle(); a.color = “Blue”; //ILLEGAL a.setColor(“Blue”); //Legal. a.getColor(); //Legal, would return blue a.color; //ILLEGAL

16 When to inherit? A question soon occurs, when do I want to use inheritance from a class and when do I want to just have an object of the class be an instance variable? If you have a relationship between two classes that is an “is a” relationship then inheritance would be good. If the relationship is a “has a” relationship, then inheritance probably wouldn’t be good.

17 When to inherit examples
A Student is a Person Student inherits from Person. A Student has a Pencil. Student does not inherit from Pencil. They have an instance variable of type Pencil. A Pencil is a WritingInstrument Pencil inherits from WritingInstrument. A Pencil is Yellow. Yellow is a single property of Pencil. Pencil doesn’t inherit from Yellow.

18 Review- How to inherit Which of the following can be inherited from this class? public class muglets { private int cost; public String name; public getCost() {…} private modifyCost(int something) }

19 Review- How to inherit Which of the following are legal if color is an inherited private variable, size(int) is an inherited public variable and all the methods (getSize, setSize, etc.) were public methods in Shape? Rectangle r = new Rectangle(); r.size = 5; r.getSize(); r.setSize(15); r.color = “Red”; r.setColor(“Red”); r.getColor(); ...

20 Review- How to inherit Which of the following should inherit from one another(which way should they inherit?)? Which should just be instance variables? Horse. Animal. Computer. Keyboard. GamePiece. ChessPiece. LightSource. Lamp.

21 Overriding methods

22 Overriding Sometimes when we inherit methods, we wish to change the method slightly so that it is more meaningful for our class. We can do this with overriding. Overriding is when an derived(child) class writes out an alternate definition for a method from the base class. The two have the same name, and the same parameters. Note this is not overloading, as the two methods do not differ in their parameters.

23 Overriding Imagine there was a method in Shape:
Then we could override that in rectangle with the following method: public void writeOutput() { System.out.println(“I am a shape.”); } public void writeOutput() { System.out.println(“I am a rectangle.”); }

24 Overriding example. … Shape a = new Shape();
Rectangle b = new Rectangle(); a.writeOutput(); //I am a shape. b.writeOutput(); //I am a rectangle.

25 Overriding We can override any kind of public method from our ancestor classes. Even constructors. We will talk more about overriding constructors later.

26 Overriding Java will figure out at run-time which of the methods you wanted to call(the base class one or the overridden one) when you invoke a method in some code. It will determine this by checking to see what class was used with “new” to originally initialize the object. We have actually been doing overriding already, whenever we create an equals method in our classes. We are overriding the equals methods of the Object class.

27 Object class. The Object class is an ancestor to EVERY class in Java. There are methods that we have already been inheriting that are useful, such as toString and equals. toString tells Java how to output the object to a string when it is concatenated with a string.

28 Review-Overriding Write a method for the Square class that will override the writeOutput method. Have it output “I am a square.”

29 Other details about inheritance.

30 How to stop inheritance
If you include the modifier “final” in a method declaration or a class declaration, you will keep anyone from overriding that function or inheriting from that class, respectively. public final class Square {…}

31 Constructors in derived classes.
A constructor in a derived class will automatically call the default constructor of its base class as the first step in the constructor. The only way out of this is to use “this” or “super” (a couple more slides).

32 Constructors in derived classes
public Shape() { //default constructor for Object automatically //called. System.out.println(“I’m making a Shape.”); } … and then in another file. public Rectangle() { //default constructor for Shape automatically //called. System.out.println(“I’m making a rectangle.”); } Rectangle r = new Rectangle(); //I’m making a Shape. //I’m making a rectangle.

33 Calling an base class constructor- super.
We can use “super” to refer to the base class. We can directly call a constructor by saying super(parameters); public Rectangle() { super(); ... } public Rectangle() { ... } =

34 Calling other constructors in the same class- this.
We can also call a different constructor inside of the current class by using the “this” keyword. Just say “this(parameters)” and supply the appropriate parameters. See the Die example.

35 Rules about this and super in constructors.
They must be used in the very first line of the constructor. They are not valid in any other position. public Rectangle() { super(); //Legal this(4,5); //ILLEGAL }

36 Calling an overridden method -super again.
We can also call methods that we have overridden with a new method in our derived class. To refer to the base class’s method we say “super.methodName(parameters)” Doesn’t need to be the first line of the method, that is just a rule for constructors. public void writeOldOutput() //in Rectangle. { super.writeOutput(); //I am a shape. }

37 Objects of derived classes have more than one type.
Objects of derived classes have all the types of all their ancestors. Thus all objects are of type Object. A Square is also of type Rectangle, Polygon, and Shape. You can cast or store an object to any of its types.

38 Inheritance Review

39 Inheritance Review Shape Polygon Squiggly Quadri- lateral Triangle
Rectangle Square

40 Inheritance Review Which of the following should inherit from one another(which way should they inherit?)? Which should just be instance variables? Horse. Animal. Book. Chapter. Sports Equipment. Skis.

41 Inheritance Review Which of the following can be inherited from this class? public class muglets { private int cost; public String name; public getCost() {…} private modifyCost(int something) }

42 Inheritance Review Which of the following are legal if color is an inherited private variable, size(int) is an inherited public variable and all the methods (getSize, setSize, etc.) were public methods in Shape? Rectangle r = new Rectangle(); r.size = 5; r.getSize(); r.setSize(15); r.color = “Red”; r.setColor(“Red”); r.getColor(); ...

43 Inheritance Review What methods can we override from a class?
How do we override a method called “blar” that takes one integer parameter and returns a double? What word do we use to stop any class from inheriting from one of our classes? What word do we use to stop any child classes from overriding one of our methods?

44 Inheritance Review What is the super(w,h) instruction doing?
public class Rectangle extends Quadrilateral { public Rectangle(int w, int h) super(w, h); } What is the super(w,h) instruction doing? How would we call the overridden method writeOutput (takes not parameters) of our base class?

45 Inheritance Review Which of the following types is the variable a?
Rectangle a = new Rectangle(); Which of the following types is the variable a? Shape Quadrilateral Rectangle Square


Download ppt "Chapter 7- Inheritance."

Similar presentations


Ads by Google