Lecture 06 Inheritance
Lecture 06 Inheritance
Chapter 4
Inheritance
By: Mequanent Argaw
Debre Markos University
Constructors in Inheritance
Method Overriding
Advantages of Inheritance
12/7/2015 Department of ECE, College of Technology 2
Introduction to Inheritance
Inheritance is one of the primary capabilities of OOP.
modified capabilities.
The idea of inheritance is that you can create new classes that are built on
existing classes.
When you inherit from an existing class, you reuse ( or inherit) its methods
and fields and you add new methods and fields to adapt to your new class
to new situations.
The News Feed is the list of messages that should appear on screen when a
Initially, we will only have two types of posts appearing in our news feeds:
In this application we will prototype the engine that stores and displays these
messages.
12/7/2015 Department of ECE, College of Technology 5
The Network’s Application : Classes
Some of the classes needed to implement the application are easily
identified.
We must have a class MessagePost to represent message posts and a
class PhotoPost to represent photo posts.
have in common.
A PhotoPost is a Post
Finally, we add the extra details needed for a message post to the MessagePost
classes or Subclasses.
class itself.
Yes : protected
12/7/2015 Department of ECE, College of Technology 10
Protected Members
Using the access modifier protected offers an intermediate level of
itself. Rather they are hidden in its subclass and can be accessed only
superclass.
superclass.
constructor.
the subclass?
portion of the object, and the constructor for the subclass constructs
In this case you must use Java’s keyword, super, which has two
general forms:
The constructor executed by the super call will be the one that matches the
arguments if more than one constructor is defined in the superclass.
the superclass.
12/7/2015 Department of ECE, College of Technology 19
Multi level Hierarchy
Besides using simple class hierarchies that consist of only a superclass
and a subclass, you can also create hierarchies that contain multiple
inheritance layers.
is in turn a subclass of A.
In multilevel hierarchies each subclass inherits all of the traits found
superclass.
then all subclasses must pass those parameters “up the line.”
first ?
A) The one in the subclass
Answer:
In a class hierarchy, constructors are called in order of derivation, from superclass to
subclass.
Even if you use super(), since it is always executed first, the order is still from superclass
to subclass. If super() is not used, then the default( parameterless) constructor of each
subclass,
strictly enforced.
Therefore, a reference variable for one class type cannot normally refer to
In general, an object reference variable can refer only to objects of its type.
superclass
12/7/2015 Department of ECE, College of Technology 28
Subtyping Example
class X{ class SupSubRef{
int a; public static void main(String [] args){
public X(int i){ X x1 = new X(10);
a = i; X x2;
} Y y1 = new Y(5, 6);
x2 = x1;
} System.out.println(“x2.a: ” + x2.a);
class Y extends X{ x2 = y1;
int b; System.out.println(“x2.a: ” + x2.a);
public Y(int i, int j){ x2.a = 19;
super(j); x2.b = 27; // impossible
b = i; }
} }
}
variable – not the type of the object that it refers to – that determines
reference variable, you will have access only to those parts of the
superclass.
methods are identical. If not, then the two methods are simply overloaded.
Method overriding forms the basis for one of Java’s most powerful concepts:
class OverloadingExample{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){
return a+b+c;
}
}
Java Method Overriding example
class Animal{
void eat(){
System.out.println("eating...");
}
}
class Dog extends Animal{
void eat(){
System.out.println("eating bread...");
}
}
12/7/2015 Department of ECE, College of Technology 36
Advantages of Inheritance
Avoiding code duplication: the use of inheritance avoids the need to write
Code reuse : existing code can be reused. If a class similar to the one we
need already exists, we can derive the new class from the existing class and
reuse some of the existing code instead of writing everything from scratch.