1 / 24

Inheritance

Inheritance. Chapter 9. Inheritance. Superclass. subclass of Animal superclass of dog and cat. Subclass. 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.

arin
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Chapter 9

  2. Inheritance Superclass subclass of Animal superclass of dog and cat Subclass

  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: • color, xPosition, yPosition, Diameter • Triangle has: • color, xPosition, yPosition, length, height • Make a parent: Shape

  6. 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

  7. 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

  8. 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.

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

  10. 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

  11. 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

  12. } critical } Also critical Casting and Inheritance PracticeA. 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( );

  13. 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

  14. Mammal 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() { }

  15. 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;

  16. 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?

  17. Inheritance Syntax public class Superclass { } public class Subclass extends Superclass { }

  18. 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 } Inheritance code for Person parent classes are no different from any classes we have written.

  19. 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; } } inheritance is implemented with “extends” Code for Student Must call parent constructor as first line of subclass constructor Must call parent constructor as first line of subclass constructor

  20. 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.

  21. Client using Person and StudentA. 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 }

  22. 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

More Related