● Inheritance
● Overriding and hiding methods
● Polymorphism
● Final methods and classes
● Abstract methods and classes
Inheritance
● A class that is derived from another class is called a
subclass (derived class, extended class, child class).
● The superclass is also called base class or parent
class.
● In Java, excepting Object, every class has one and
only one direct sup
17 trang |
Chia sẻ: candy98 | Lượt xem: 491 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 5: Inheritance - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture 5: Inheritance
Lê Hồng Phương
phuonglh@gmail.com
Department of Mathematics, Mechanics and Informatics,
Vietnam National University, Hanoi
2012-2013 Object-Oriented Programming: Inheritance 2
Content
● Inheritance
● Overriding and hiding methods
● Polymorphism
● Final methods and classes
● Abstract methods and classes
2012-2013 Object-Oriented Programming: Inheritance 3
Inheritance
● A class that is derived from another class is called a
subclass (derived class, extended class, child class).
● The superclass is also called base class or parent
class.
● In Java, excepting Object, every class has one and
only one direct superclass.
2012-2013 Object-Oriented Programming: Inheritance 4
Inheritance
● A subclass inherits all the members (fields, methods,
and nested classes) from its superclass.
● Constructors are not members, so they are not
inherited by subclasses.
● The constructor of the superclass can be invoked
from the subclass.
2012-2013 Object-Oriented Programming: Inheritance 5
Inheritance
public class Bicycle {
private int cadence = 0;
private int speed = 0;
private int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("(" + cadence + ", " + speed + ", " + gear + “)”);
}
}
The fields represent
the state of the object.
The methods define its
interaction with the outside world.
2012-2013 Object-Oriented Programming: Inheritance 6
Inheritance
public class Bicycle {
// ...
public Bicycle(int c, int s, int g) {
this.cadence = c;
this.speed = s;
this.gear = g;
}
//
}
A constructor defines how an object
is initialized when constructed.
Bicycle myBike = new Bicycle(30, 15, 4);
2012-2013 Object-Oriented Programming: Inheritance 7
Inheritance
public class MountainBike extends Bicycle {
private int seatHeight;
public MountainBike(int height, int cadence, int speed, int gear) {
super(cadence, speed, gear);
seatHeight = height;
}
public int getSeatHeight() {
return seatHeight;
}
}
Call the constructor of the
superclass to initialize its 3 fields.
Bicycle
MountainBike
2012-2013 Object-Oriented Programming: Inheritance 8
Inheritance
● A subclass inherits all the public and protected
members of its parent, no matter what the package the
subclass is in.
● If the subclass is in the same package as its parent, it
also inherits the package-private members of the
parent.
● A subclass does not inherit the private member of its
parent class.
– If it wants to access this member, it must call public
or protected methods of the parent class.
2012-2013 Object-Oriented Programming: Inheritance 9
Overriding and hiding methods
● If you declare a field in the subclass with the same
name as the one in the superclass, you hide it (not
recommended).
● If you write a new instance method that has the same
signature as the one in the superclass, you override
it.
● If you write a new static method that has the same
signature as the one in the superclass, you hide it.
2012-2013 Object-Oriented Programming: Inheritance 10
Overriding and hiding methods
public class MountainBike extends Bicycle {
//...
@Override
void applyBrakes(int decrement) {
// ...
}
//...
}
The subclass has a method
which overrides that of its superclass.
● This allows a class to inherit from a superclass whose
behavior is similar and then to modify behavior as needed.
● Use @Override annotation to instruct the compiler (optional).
2012-2013 Object-Oriented Programming: Inheritance 11
Casting objects
● If class A is a subclass of class B then we can say A
is B.
– MountainBike is Bicycle
– Car is Vehicle
– Dog is Animal
– A is Object
● Casting object: convert an object of one type to
another type.
2012-2013 Object-Oriented Programming: Inheritance 12
Casting objects
● Implicit casting:
● Here, b is both a Bicycle and a MountainBike.
● Explicit casting:
Bicycle b = new MountainBike(100, 30, 15, 4);
if (b instanceof MountainBike) {
MountainBike myBike = (MountainBike) b;
}
2012-2013 Object-Oriented Programming: Inheritance 13
Polymorphism
● Polymorphism is the ability to create a behavior or an
object that has more than one form.
● The exact behavior of an object is determined at run-
time instead of at compile time.
– This is called late binding or dynamic binding.
● Polymorphism is a very important concept of object-
oriented programming.
– You must completely understand this concept!
2012-2013 Object-Oriented Programming: Inheritance 14
Polymorphism
● Example:
– Pet is class which defines an instance method named
void say().
– Dog is a subclass of Pet which overrides the method
say().
– Cat is another subclass of Pet which overrides the
method say().
2012-2013 Object-Oriented Programming: Inheritance 15
Polymorphism
public class Pet {
public void say() {
System.out.println("I don't know who I am.");
}
}
class Dog extends Pet {
@Override
public void say() {
System.out.println("Wooof!");
}
}
class Cat extends Pet {
@Override
public void say() {
System.out.println("Meow!");
}
}
Pet
Dog Cat
2012-2013 Object-Oriented Programming: Inheritance 16
Polymorphism
Pet
Dog Cat
public class PetTester {
public static void main(String[] args) {
Pet p;
p = new Pet();
p.say();
p = new Dog();
p.say();
p = new Cat();
p.say();
}
}
2012-2013 Object-Oriented Programming: Inheritance 17
Object as a superclass
● The class java.lang.Object is the root class of all the
classes.
● Some important methods of Object that we may want
to inherit and/or override are:
protected Object clone() throws CloneNotSupportedException;
public boolean equals(Object obj);
protected void finalize() throws Throwable;
public int hashCode();
public String toString();
Override this method if you want
to convert an object to a nice string
representation (e.g, for printing the object).