Professional Documents
Culture Documents
PROGRAMMING
LECTURE # 7: TYPES OF
INHERITANCE
Department of CS
1 BSSE-2
Hitec University Taxila
TYPES OF INHERITANCE (1/4)
2
Class Mango
SINGLE INHERITANCE (SYNTAX)
5
class Fruit
{
//methods and fields
}
class Mango extends Fruit
{
//methods and fields
}
SINGLE INHERITANCE (EXAMPLE)
6
//Base-class
class Fruit
{
void fruitname()
//Main Program
{
System.out.println("I am a Fruit");
} public class SinglelevelInheritance
} {
public static void main(String args[])
{
//Derived-class Mango obj = new Mango();
class Mango extends Fruit
obj.fruitname();
{
obj.taste();
void taste()
}
{
System.out.println("My taste is sweet"); }
}
}
MULTILEVEL INHERITANCE (CONCEPT)
7
class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends Y
{
//methods and fields
}
MULTILEVEL INHERITANCE (EXAMPLE)
9
Class X
{
public void methodX()
{ //Main Program
System.out.println("Class X method"); public static void main(String args[])
}
{
}
Class Y extends X Z obj = new Z();
{ //calling grand parent class method
public void methodY() obj.methodX();
{ //calling parent class method
System.out.println("class Y method"); obj.methodY();
}
}
//calling local method
Class Z extends Y obj.methodZ();
{ }
public void methodZ()
{
System.out.println("class Z method");
}
HIERARCHICAL INHERITANCE (CONCEPT)
10
• Hierarchical
inheritance When two or
Class X
more classes inherits a
single class, it is known
as hierarchical
inheritance.
Class Y Class Z
• In the example given
below, Y and Z classes
inherits the X class, so
there is hierarchical
inheritance.
HIERARCHICAL INHERITANCE (SYNTAX)
11
class X
{
//methods and fields
}
class Y extends X
{
//methods and fields
}
class Z extends X
{
//methods and fields
}
HIERARCHICAL INHERITANCE (EXAMPLE)
12
Multiple Inheritance:
When one class extends more than one classes then
this is called multiple inheritance. For example:
Class C extends class A and B then this type of
inheritance is known as multiple inheritance.
Java doesn’t allow multiple inheritance. We will discuss
later why java doesn’t allow multiple inheritance and
how we can use interfaces instead of classes to achieve
the same purpose.
Hybrid Inheritance:
Do yourself?
THIS AND SUPER KEYWORD IN JAVA
14
THIS KEYWORD
15
CODE
class Main
class Test {
{ public static void main (String[] args)
int i; {
void setValue(int j) Test obj = new Test();
{ obj.setValue(10);
i=j; obj.show()
} }
void show()
{
System.out.println(i);
}
16
THIS KEYWORD (CODE)
18
SUPER KEYWORD (CODE)
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}}
class Dog extends Animal {
void sound() {
super.sound(); // Calling superclass method
System.out.println("Dog barks");
}}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}} 19
USES OF "THIS" KEYWORD
1. This keyword can be used to to refer current
class instance variable.
2. This keyword can be used to invoke current class
method (implicitly).
3. This() can be used to invoke current class
constructor.
4. This can be used to pass as an argument in the
method call.
5. This can be used to pass as an argument in the
constructor call.
6. This can be used to return the current class 20
instance from the method.