Presentation is loading. Please wait.

Presentation is loading. Please wait.

L EC. 07: I NHERITANCE 0. 2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.

Similar presentations


Presentation on theme: "L EC. 07: I NHERITANCE 0. 2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references."— Presentation transcript:

1 L EC. 07: I NHERITANCE 0

2 2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references and subclass objects  Hiding and overriding inherited members  Method overriding  Polymorphism [ 程設 4]  Abstract classes and methods [ 程設 4]  Final [ 程設 4]  Blueprint of java.lang.Object class [ 程設 4] 1

3 I NHERITANCE B ASICS  Inheritance is one of 4 major OOP features.  Inheritance allows a class to use the codes, including fields and methods, defined in a pre-exist class without recoding while adding new features.  With inheritance a class can be derived from other pre-exist classes, thereby inherit fields and methods from those classes.  A class that is derived from another class is called a subclass (also a derived class, extended class, or child class).  The class from which a subclass is derived is called a superclass (also a base class or parent class).  A superclass is more abstract or generic than its subclasses and a subclass is more specific than its superclass. 2

4 I NHERITANCE  Inheritance creates the “is-a” relationship between two classes: subclass is-a superclass, which means a subclass is a special case of its superclass.  Each subclass object is an object of the superclass,,e.g. a manager is a fulltime employee and a fulltime employee is an employee.  A superclass object may not be an object of its subbclass, e.g. an employee may not be a fulltime employee and a fulltime employee may not be a manager. 3

5 U SING KEYWORD extends  In Java, the keyword extends is used to specify the relationship between a subclass and its superclass.  Syntax form class extends { // class body } 4

6 E XAMPLE 5 class Vehicle { public void start() { System.out.println("Starting..."); } class Car extends Vehicle { public void drive() { System.out.println("Driving..."); } class App217 { public static void main(String[] args) { Car c = new Car(); c.start(); c.drive(); } + start() is-a vehicle + drive() car Starting... Driving... For efficient reuse UML 父 子 ♥

7 E XAMPLE class TwoDShape { double width; double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); } class Rectangle extends TwoDShape { boolean isSquare() { if(width == height) return true; return false; } double area() { return width * height; } 6 double width double height void showDim() is-a TwoDShape boolean isSquare() double area() Rectangle Architecture design

8 class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); } 7 double width double height void showDim() is-a TwoDShape String style double area() void showStyle() Triangle is-a boolean isSquare() double area() Rectangle common 單一繼承

9 class Shapes { public static void main(String args[]) { Rectangle r1 = new Rectangle(); Triangle t1 = new Triangle(); r1.width = 2.0; r1.height = 4.0; r1.showDim(); System.out.println("is a Square? " + r1.isSquare()); System.out.println("Area is " + r1.area()); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 8 Width and height are 2.0 and 4.0 is a Square? false Area is 8.0 Triangle is filled Width and height are 4.0 and 4.0 Area is 8.0

10 E XERCISE 1A  Based on class TwoDShape, create a subclass called Volume. 利用 class Volume 來計算一個長方體的體積. 9 double width double height void showDim() TwoDShape double z; double vol() Volume

11 E XERCISE 1B class Ex2b { public static void main(String[] args) { Bird bird = new Bird(); Dog dog = new Dog(); Fish fish = new Fish(); bird.sleep(); bird.eat(); dog.sleep(); dog.eat(); fish.sleep(); fish.eat(); } 10 sleep() Animal eat() Bird eat() Dog eat() Fish An animal sleeps... A bird eats... An animal sleeps... A dog eats... An animal sleeps... A fish eats... is-a

12 M EMBER A CCESS AND INHERITANCE  All the public-mode fields and methods of a superclass can be inherited and directly referenced by any subclass of the superclass.  All the protected-mode fields and methods of a superclass can be inherited and directly referenced by any subclass of the superclass.  All the default-mode fields and methods of a superclass can be inherited and directly referenced by a subclass of the superclass if they are in the same package. 11

13 W HAT CAN BE INHERITED …  A subclass does not inherit the private members of its parent class.  If the superclass has public or protected methods for accessing its private fields, a subclass can access these members via these methods.  Since a nested class has access to all the private members of its enclosing class—both fields and methods, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. 12

14 E XAMPLE class TwoDShape { private double width; private double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); } 13 ModifierSame Class Same Package SubclassUniverse privateYes defaultYes protectedYes publicYes protected double width; protected double height; double width; double height;

15 class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); } class Shapes2 { public static void main(String args[]) { Triangle t1 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 14 error - double width - double height void showDim() TwoDShape String style double area() void showStyle() Triangle

16 E XAMPLE class TwoDShape { private double width; private double height; double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } void showDim() { System.out.println("Width and height are " + width + " and " + height); } 15 getter setter

17 class Triangle extends TwoDShape { String style; double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); } class Shapes3 { public static void main(String args[]) { Triangle t1 = new Triangle(); t1.setWidth(4.0); t1.setHeight(4.0); t1.style = "filled"; t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); } 16

18 E XERCISE 2  把 Ex1A 的 instance width and height 設定為 private. 利 用 class Volume 來計算一個長方體的體積. 17

19 C ONSTRUCTORS AND INHERITANCE  When an object of a class is instantiated, the constructor of the class constructs its part and the constructor of the class’ superclass constructs the superclass portion of the object.  Remember the superclass has no knowledge of or access to any element in its subclass and the subclass has no responsibility to set inherited fields.  Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass  Invoking a superclass constructor can be performed only in constructors of associated subclasses. 18

20 E XAMPLE 1 class A { A() { System.out.println("In a's constructer..."); } class B extends A { B() { // super(); System.out.println("In b's constructer..."); } class App221 { public static void main(String[] args) { A objA = new A(); System.out.println("..."); B objB = new B(); } 19 In a's constructer...... In a's constructer... In b's constructer... A() is-a A B() B 父 子

21 E XAMPLE 2 class A { A() { System.out.println("In a's constructer..."); } class B extends A { B(String s) { // super(); System.out.println("In b's constructer..."); System.out.println(s); } class App222 { public static void main(String[] args) { B obj = new B("Hello from Java!"); // B obj = new B(); Error } 20 In a's constructer... In b's constructer... Hello from Java! + A() is-a A + B(String) B

22 class A { A() { System.out.println("In a's 1 st constructer..."); } A(String s) { System.out.println("In a's 2 nd constructer..."); System.out.println(s); } class B extends A { B() { // super(); System.out.println("In b's constructer..."); } class App223 { public static void main(String[] args) { B obj = new B(); } 21 In a's 1st constructer... In b's constructer... + A() + A(String) is-a A + B() B overload E XAMPLE 3 2 hr


Download ppt "L EC. 07: I NHERITANCE 0. 2015 S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references."

Similar presentations


Ads by Google