Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Chapter 9.

Similar presentations


Presentation on theme: "Inheritance Chapter 9."— Presentation transcript:

1 Inheritance Chapter 9

2 Inheritance Superclass subclass of Animal superclass of dog and cat

3 Superclass has its own fields and methods
Subclass inherits all of superclass fields and methods and adds some of its own Subclass has its own fields and methods

4 Inheritance Advantages
One superclass for lots of subclasses Saves code rewriting for client Save code rewriting within class Note: There does not have to be an object of a superclass – there are no “animal” objects, but there may be: (triangle, isosceles triangle, right angle isosceles triangle).

5 Shapes Example Circle has: Triangle has: Make a parent: Shape
color, xPosition, yPosition, Diameter Triangle has: color, xPosition, yPosition, length, height Make a parent: Shape

6

7

8 Assignment Circle c = new Circle ( ); Shape s = new Shape( );
s = c; // ok, a circle IS a shape c = s; // NOT ok. A shape might be something other than a Circle

9 Assignment a = b; // all b are a
b must the same class as a, or a subclass of a b IS-A a (note: backwards of assignment order) dog is a mammal Circle is a Shape does NOT work for a IS-A b (same order as assignment) mammal is not necessarily a dog

10 Objects & methods obj.method( )
method must be in obj class or a parent class Circle c = new Circle(); c.draw( ); // ok. draw is a Circle method c.changeColor("green"); // ok. changeColor is a Shape method Shape s; s.draw( ) ; // NOT ok. draw is a method of its children s.changeColor("green"); // ok. changeColor is a Shape method If method is in the super class, ok. that's the point of inheritance.

11 Subtyping Object variables in Java can hold objects of the declared type, or of subtypes of the declared type. Big advantage in many applications

12 Subtyping Shape s1 = new Shape( ); // ok
Circle c = new Circle( ); // ok Triangle t = new Triangle( ); // ok Shape s2 = new Circle( ); //ok. all circles are shapes Circle c2 = new Shape( ); // NOT ok. not all shapes // are circles c.findCircumference ( ); // ok if method exists s2.findCircumference( ); // NOT ok ((Circle)s2).findCircumference( ); // ok s2 is a Circle LHS must be same level or higher than RHS object must be same level or lower than method Casting

13 Casting Turn one type into another (when it’s ok)
double sphere = 4 / 3 * Math.PI * r * r * r; fix: double sphere = (double) 4 / 3 * Math.PI * r * r * r; 4.0 / 3 = double/ int = double / double = 1.333… 4 / 3 = 1 Creates a new value, 4.0 (double) from 4 3 is automatically coerced into a double. Casting does it explicitly, and is programmer controlled

14 Casting and Inheritance Practice A. Ok B. Not OK
int y=3; double g=3; int x = (double) y; double z = (int) g; MyShape s = new Circle( ); Circle c = new Circle( ); s.moveVertical(10); c.moveVertical(10); c.findCircumference( ); s.findCircumference( ); ((Circle) s).findCircumference( ); critical } Also critical }

15 Methods and Inheritance
ok to call method in the class or in a superclass NOT ok to call method of a subclass To do that, object must be cast to the class where method appears ((Circle) s).draw( ); // no syntax error // ok if s is a Circle Will get a run-time error if object is NOT the subclass Shape s = new Square( ) ; // ok ((Circle) s).draw( ); // ok in syntax. run time error

16 Practice Dog d = new Mammal(); Mammal m = new Dog( );
private final int nbrlegs = 4; private final stuff covering = FUR; Mammal() { // constructor} FeedsYoung() { nurses();} GivesBirth() { liveyoung(); } Practice A. OK B. NOT OK Dog d = new Mammal(); Mammal m = new Dog( ); StBernard s = new Dog(); m.FeedsYoung( ); d.slobbers(); s.wagstail(); s.FeedsYoung(); Dog // fields for dogs Dog() {// constructor } public void wagstail() { } StBernard // fields for St. Bernards ------ StBernard { /* constructor*/} public void slobbers() { }

17 Practice: A. OK B. Not OK Assume we have 4 classes: Person, Teacher, Student and PhDStudent. Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student. Which of the following are legal? Person p1 = new Student( ); Person p2 = new PhDStudent( ); PhDStudent phd1 = new Student ( ); Teacher t1 = new Person( ); Student s1 = new PhDStudent( ); s1 = p1; s1 = p2; p1 = s1; t1 = s1; s1 = phd1; phd1 = s1;

18 Practice 2: do on the board
8.17 Look at the code below. You have four classes (O, X, T and M) and a variable of each of these. O o; X x; T t; M m; The following assignments are all legal: m = t; m = x; o = t; The following assignment are all illegal: o = m; o = x; x = o; What can you say about the relationships of these classes?

19 Inheritance Syntax public class Superclass { }
public class Subclass extends Superclass

20 Inheritance code for Person
public class Person { private String name; public Person() // constructor { // do constructor-like stuff } public Person(String n) { name = n; } public void getName( ) { return name; } // other methods } parent classes are no different from any classes we have written.

21 Code for Student public class Student extends Person
inheritance is implemented with “extends” Code for Student public class Student extends Person { private String school; public Student( ) { super( ); school = "Christopher Newport University"; } public Student(String name, String school) { super(name); this.school = school; } public void getSchool( ) { return school; } } Must call parent constructor as first line of subclass constructor Must call parent constructor as first line of subclass constructor

22 Superclass constructor call
Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.

23 Client using Person and Student A. OK; B. NOT ok
public class MyClient { Person p = new Person( ); Student s = new Student( ); Person r = new Student( ); public MyClient( ) p.getName( ); // 1 s.getSchool( ); // 2 s.getName( ); // 3 r.getSchool( ); // 4 }

24 Inheritance: what do we know?
extends // used in subclass superclass has no indication inheritance is being used super() // calls constructor of superclass must be first statement of subclass


Download ppt "Inheritance Chapter 9."

Similar presentations


Ads by Google