Lab 8 Oop 11112020 013226pm 15122021 094301pm 1 22112022 021509pm

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Lab 8

INHERITANCE:
INHERITANCE:
• inheritance: a parent-child relationship between classes

• allows sharing of the behavior of the parent class into its child classes
• one of the major benefits of object-oriented programming (OOP) is this code
sharing between classes through inheritance
Terms used in Inheritance

• Class: A class is a group of objects which have common properties. It is


a template or blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class.
It is also called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which
facilitates you to reuse the fields and methods of the existing class
when you create a new class. You can use the same fields and methods
already defined in the previous class.
The syntax of Java Inheritance

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

• 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.
Types of inheritance in java
• On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.
Examples: Base Classes and Derived Classes

Ba se c la ss De rive d c la sse s
Student GraduateStudent
UndergraduateStudent

Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
Example :single
class Animal{   }  
void eat() }  
{ class TestInheritance{  
System.out.println("eating..."); public static void main(String args[])
}  }   {  
class Dog extends Animal{   Dog d=new Dog();  
void bark() d.bark();  
{ d.eat();  
System.out.println("barking..."); }}  
Multilevel Inheritance Example
class Animal{   {
void eat() System.out.println("weeping...");}  
{ }  
System.out.println("eating..."); class TestInheritance2{  
}  }   public static void main(String args[]){  
class Dog extends Animal{   BabyDog d=new BabyDog();  
void bark() d.weep();  
{ d.bark();  
System.out.println("barking..."); d.eat();  
}  }   }}  
class BabyDog extends Dog{  
void weep()
Hierarchical Inheritance Example

class Animal{   System.out.println("meowing...");
void eat() }  
{ }  
System.out.println("eating..."); class TestInheritance3{  
}  }   public static void main(String args[]){ 
class Dog extends Animal{   Cat c=new Cat();  
void bark() Dog d = new Dog();
{ d.bark();
System.out.println("barking..."); d.eat();
}  }   c.meow();  
class Cat extends Animal{   c.eat();  
void meow()  }} 
{
The protected Modifier
• The protected modifier allows a member of a base class to be
inherited into a child
• Protected visibility provides more encapsulation than public visibility
does
• However, protected visibility is not as tightly encapsulated as private
visibility
• Protected variables and methods can be shown with a # symbol
preceding them in UML diagrams
SYNTAX
protected void sleep()
{
System.out.println("sleeping");
}
Method overriding

•If subclass (child class) has the same


method as declared in the parent class,
it is known as method overriding in
java.
Rules for Java Method Overriding
1. method must have same name as in the parent
class

2. method must have same parameter as in the


parent class.

3. must be IS-A relationship (inheritance).


EXAMPLE:

public class JavaApplication39 {


public static void main(String[] args) {
Bike2 obj = new Bike2();
obj.run();
}
}
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running safely");}
}
super keyword in java:-

• The super keyword in java is a reference variable


that is used to refer immediate parent class
object.
• Whenever you create the instance of subclass,
an instance of parent class is created implicitly
i.e. referred by super reference variable.
Usage of java super Keyword

1. super is used to refer immediate parent class


instance variable.
2. super() is used to invoke immediate parent
class constructor.
3. super is used to invoke immediate parent class
method.
Problem without super keyword
1. class Vehicle{
2. int speed=50;
3. }
4. class Bike3 extends Vehicle{
5. int speed=100;
6. void display(){
7. System.out.println(speed);//will print speed of Bike
8. }
9. public static void main(String args[]){
10. Bike3 b=new Bike3();
11. b.display();
12. }
13. }
Output:100 In the above example Vehicle and Bike both class have a common property speed. Instance variable
of current class is refered by instance bydefault, but I have to refer parent class instance variable that is why we
use super keyword to distinguish between parent class instance variable and current class instance variable .
• Solution by super keyword
1. //example of super keyword
2. class Vehicle{
3. int speed=50; }
4. class Bike4 extends Vehicle{
5. int speed=100;
6. void display(){
7. System.out.println(super.speed);//will print speed of Vehicle
now }
8. public static void main(String args[]){
9. Bike4 b=new Bike4();
10. b.display(); } }
Output:50
• super is used to invoke parent class constructor. The super keyword can also be used to invoke the
parent class constructor as given below:

class Vehicle{
Vehicle()
{System.out.println("Vehicle is created");
}}
class Bike5 extends Vehicle{
Bike5()
{
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike5 b=new Bike5();
}}
Note: super() is added in each class constructor automatically by compiler.
• super can be used to invoke parent class method The super keyword can
also be used to invoke parent class method. It should be used in case
subclass contains the same method as parent class as in the example given
below:
Task 1:
Write a program in java showing the use of inheritance a follows:

Bahria

Faculty Student Staff


Task 2:
Write a program in java showing the use of inheritance a follows:

Vehicle

Bicylcle Bike Rikhshaw Truck Car


Task 3:
Write a program in java showing the use of inheritance a follows:

Animal

Reptile Bird Mammal

Snake Lizard Parrot Horse Bat

You might also like