Week 7 - L19-L21
Week 7 - L19-L21
Unit No:4
Unit Name: Inheritance and Packages
2
Module No 4: Inheritance and Packages
Lecture No: 19
Importance of Inheritance, Different Types of
Inheritance
Importance of Inheritance
4
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Defining a Subclass
• Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
• The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to increase the
functionality.
• Extends keyword signifies that properties of the super class are extended to
sub class
• Sub class will not inherit private members of super class
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output
Programmer salary is:40000.0
Bonus of programmer is:10000
7
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Inheritance and it’s Types
• A class can inherit the properties of another class and add its own
modification.
• The class whose properties are extended is known as super or base or
parent class.
• The class which extends the properties of super class is known as sub or
derived or child class
• To declare inherited classes ‘extends’ keyword is used
• A is super class
• B is sub class
B
A X
B A B C
MultiLevel Inheritance
9
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 2:Single Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
OUTPUT
barking...
eating...
10
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Single Inheritance)
11
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 3: Multilevel Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
OUTPUT:
weeping...
barking...
eating...
12
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Multilevel Inheritance)
13
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 4: Hierarchical Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}}
OUTPUT
meowing...
eating...
14
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Hierarchical Inheritance)
15
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Program 4
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
16
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Explanation (Multiple Inheritance)
• Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have same
method or different, there will be compile time error.
17
Lecture 19-Importance of Inheritance, Different Types of Inheritance
Module No 4: Inheritance and Packages
Lecture No: 20
Super keyword
Using super
20
Lecture 20- Super keyword
1.super can be used to refer immediate parent class instance variable.
21
Lecture 20- Super keyword
1.super can be used to refer immediate parent class instance variable.
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
OUTPUT:
black
white
22
Lecture 20- Super keyword
2. Super can be used to invoke parent class method
• In the given example Animal and Dog both classes have eat()
method if we call eat() method from Dog class, it will call the eat()
method of Dog class by default because priority is given to local.
23
Lecture 20- Super keyword
2. Super can be used to invoke parent class method
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}
OUTPUT
eating...
barking...
24
Lecture 20- Super keyword
3. super() can be used to invoke immediate parent class constructor.
25
Lecture 20- Super keyword
3. super() can be used to invoke immediate parent class constructor.
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
OUTPUT
animal is created
dog is created
26
Lecture 20- Super keyword
Module No 4: Inheritance and Packages
Lecture No: 21
Abstract class and Abstract method
Abstract Class in Java
29
Lecture 21- Abstract class and Abstract method
Example 1
OUTPUT
running safely
30
Lecture 21- Abstract class and Abstract method
Example 2
OUTPUT
drawing circle
31
Lecture 21- Abstract class and Abstract method
Thank You