22
LEC. 07: INHERITANCE 1

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

Embed Size (px)

Citation preview

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

1

LEC. 07: INHERITANCE

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

2

2015 SPRING CONTENT

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]

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

3

INHERITANCE BASICS

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.

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

4

INHERITANCE

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.

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

5

USING KEYWORD extends

In Java, the keyword extends is used to specify the relationship between a subclass and its superclass.

Syntax form class <subclass-name> extends <superclass-name> { // class body }

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

6

EXAMPLEclass 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

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

7

EXAMPLE

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; } }

double widthdouble heightvoid showDim()

is-a

TwoDShape

boolean isSquare()double area()

Rectangle

Architecture design

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

8

class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); } }

double widthdouble heightvoid showDim()

is-a

TwoDShape

String style double area()void showStyle()

Triangle

is-a

boolean isSquare()double area()

Rectangle

common

單一繼承

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

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()); } }

Width and height are 2.0 and 4.0is a Square? falseArea is 8.0

Triangle is filledWidth and height are 4.0 and 4.0Area is 8.0

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

EXERCISE 1A

Based on class TwoDShape, create a subclass called Volume. 利用 class Volume 來計算一個長方體的體積 .

10

double widthdouble heightvoid showDim()

TwoDShape

double z;double vol()

Volume

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

EXERCISE 1Bclass 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();

}

} 11

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

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

12

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

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

13

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

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

14

EXAMPLE

class TwoDShape { private double width; private double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); } }

Modifier Same Class Same Package Subclass Universe

private Yes

default Yes Yes

protected Yes Yes Yes

public Yes Yes Yes Yes

protected double width; protected double height;

double width; double height;

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

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()); } }

errorerror

error

- double width- double heightvoid showDim()

TwoDShape

String style double area()void showStyle()

Triangle

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

16

EXAMPLEclass 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); } }

getter

setter

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

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()); } }

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

EXERCISE 2

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

18

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

19

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

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

20

EXAMPLE 1class 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(); }}

In a's constructer ......In a's constructer ...In b's constructer ...

A()

is-a

A

B()

B

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

21

EXAMPLE 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

}}

In a's constructer ...In b's constructer ...Hello from Java!

+ A()

is-a

A

+ B(String)

B

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

22

class A {A() {

System.out.println("In a's 1st constructer ...");}A(String s) {

System.out.println("In a's 2nd 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();}

}

In a's 1st constructer ...In b's constructer ...

+ A()+ A(String)

is-a

A

+ B()

B

overload

EXAMPLE 3

2 hr