Download presentation
Presentation is loading. Please wait.
Published byKatherine Fisher Modified over 9 years ago
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
E XAMPLE 4 class A { A(){ System.out.println("In a's 1st constructer..."); } A(String s) { System.out.println("In a's 2 nd constructer..."); System.out.println(s); } class B extends A { B(String s) { super(s); // must appear at the 1st line System.out.println("In b's constructer..."); System.out.println(s); } class App223a { public static void main(String[] args) { B obj = new B("Hello from Java!"); } 2 In a's 2 nd constructer... Hello from Java! In b's constructer... Hello from Java! + A() + A(String) is-a A + B(String) B
4
E XAMPLE 5 class A { //A(int i) { System.out.print("A"); } A() { System.out.print("A"); } class B extends A { B() { // error. Require Int. System.out.print("B"); } class C extends B { C() { System.out.print("C"); } public class App223b { public static void main(String[] args) { C obj = new C(); } 3 ABC ♥
5
E XAMPLE 4 class TwoDShape { private double width; private double height; TwoDShape(double w, double h) { width = w; height = h; } 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 + " " + height); } - double width - double height TwoDShape(double, double) void showDim() is-a TwoDShape private String style Triangle(String, double, double) double area() void showStyle() Triangle ♥
6
5 class Triangle extends TwoDShape { private String style; Triangle(String s, double w, double h) { super(w, h); style = s; } double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); } class Shapes4 { public static void main(String args[]) { Triangle t1 = new Triangle(“filled", 8.0, 12.0); t1.showStyle(); System.out.println(t1.area()); } - double width - double height TwoDShape(double, double) void showDim() is-a TwoDShape - String style Triangle(String, double, double) double area() void showStyle() Triangle Triangle is filled 48.0
7
E XERCISE 2 A 重複 Ex2. 利用 class Volume 來計算一個長方體的體積. 但是請利用建構子來給定初值. 6
8
E XAMPLE Executing the following program will output BCAD. 7 class Some { Some() { System.out.print(“A”); } Some(int i) { System.out.print(“B”); } class Foo extends Some { Foo() { super(3); System.out.print(“C”); } Foo(double d) { System.out.print(“D”); //super() is inserted } public static void main(String… args) { Foo obj; obj = new Foo(); obj = new Foo(3.3); } ♥
9
I LLEGAL EXAMPLE class Some { Some() { System.out.print(“A”); } Some(int i) { System.out.print(“B”); } class Foo extends Some { Foo() { Some(3); // illegal ( 不能被繼承 ), should use super(3) } Foo(int i) { super(3); super(); // illegal, super call must be the first statement } Foo(double d) { super(d); // illegal, no matched constructor } 8
10
E XAMPLE Executing the following program will output ADCAD. 9 class Some { Some() { System.out.print(“A”); } Some(int i) { System.out.print(“B”); } class Foo extends Some { Foo() { this(3.3); // must be the first statement System.out.print(“C”); } Foo(double d) { System.out.print(“D”); } public static void main(String… args) { Foo obj; obj = new Foo(); obj = new Foo(3.3); }
11
I LLEGAL EXAMPLE class Some { Some() { System.out.print(“A”); } Some(int i) { System.out.print(“B”); } } class Foo extends Some { Foo() { Foo(3); // illegal, should use this(3) } Foo(int i) { super(3); this(); // illegal, this call must be the first statement } Foo(double d) { this(“ABC”); // illegal, no matched constructor in Foo } 10
12
S UPERCLASS REFERENCES AND SUBCLASS OBJECTS A reference variable of a superclass can be assigned a reference to an object of any subclass derived from that superclass. In other words, a superclass reference can refer to a subclass object. Remember that a subclass “is-a” superclass, so a subclass object is a superclass object. Conversely, a reference variable of a subclass cannot be assigned a reference to an object of its superclass. 11
13
E XAMPLE 1 class X { int a; X(int i) { a = i; } } class Y extends X { int b; Y(int i, int j) { super(j); b = i; } class SupSubRef { public static void main(String args[]) { X x = new X(10); Y y = new Y(5, 6); X x2 = new Y(5, 6); System.out.println(x2.a); //System.out.println(x2.b); } 12 6 int a is-a X int b Y SupSubRef.java:23: error: cannot find symbol System.out.println(x2.b); ^ symbol: variable b location: variable x2 of type X 1 error 別墅設計圖 別墅 + 游泳池 地址
14
E XAMPLE 2 class X { int a; X(int i) { a = i; } } class Y extends X { int b; Y(int i, int j) { super(j); b = i; } class SupSubRef { public static void main(String args[]) { Y y2 = new X(10); // Error, incompatible types } 13 int a is-a X int b Y 別墅設計圖 別墅 + 游泳池
15
E XERCISE 2B 參考下圖繼承關系. Vehicle 類別的 reference 是否能叫 用 Car 實體的 drive method? 14 + start() Vehicle + drive() Car
16
N OTES ON S UPERCLASS REFERENCES AND SUBCLASS OBJECTS The type of the reference variable — not the type of the object that it refers to — determines what members can be accessed. When a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. So, objX2.b is illegal. The above rules apply to all the hidden members. In fact, the binding for hidden members is static binding and compiler has no knowledge of the object type. 15
17
T ESTING CLASS TO WHICH AN OBJECT BELONGS The operator instanceof is used to check if an object is instantiated from a specific class. Example Number obj = new Integer(3); Then the following two expressions are true obj instanceof Number obj instanceof Integer But, the following expression is false obj instanceof Double 16 Number IntegerDouble is-a [Ex2B] Car c = new Car(); System.out.println(c instanceof Car); System.out.println(c instanceof Vehicle); 2 hr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.