[go: up one dir, main page]

0% found this document useful (0 votes)
24 views6 pages

Java p8

This document discusses inheritance in Java and provides 3 examples: 1) Single inheritance where the Dog class inherits from the Animal class. 2) Multilevel inheritance where the BabyDog class inherits from the Dog class which inherits from the Animal class. 3) Hierarchical inheritance where both the Dog class and Cat class inherit from the Animal class.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Java p8

This document discusses inheritance in Java and provides 3 examples: 1) Single inheritance where the Dog class inherits from the Animal class. 2) Multilevel inheritance where the BabyDog class inherits from the Dog class which inherits from the Animal class. 3) Hierarchical inheritance where both the Dog class and Cat class inherit from the Animal class.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 08:- Write programs in Java to implement inheritance

showing method and variable access in both parent class and sub class.

Introduction

Inheritance in Java is a concept that acquires the properties from one class to other
classes; for example, the relationship between father and son. Inheritance in Java is a
process of acquiring all the behaviours of a parent object.

We group the “inheritance concept” into two categories:


• Subclass (child) – the class that inherits from another class
• Superclass (parent) – the class being inherited from
To inherit from a class, use the extends keyword.

The concept of inheritance in Java is that new classes can be constructed on top of older
ones. You can use the parent class’s methods and properties when you inherit from an
existing class. You can also add additional fields and methods to your existing class.
The parent-child relationship, also known as the IS-A relationship, is represented by
inheritance.

One object can acquire all of a parent object’s properties and actions through the
technique of inheritance in Java Programming. It is a crucial component of OOPs (Object
Oriented programming system).

In Java, the idea of inheritance means that new classes can be built on top of existing
ones. When you derive from an existing class, you can use its methods and properties.
To your current class, you may also add new fields and methods.
Why use inheritance in java
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
//Syntax for inheritance:-

Class Subclass-name extends Superclass-name


{
//methods and fields
}

Types of Inheritance:-
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Note: Multiple inheritance is not supported in Java through class.

Fig.: Types of Inheritance

1. Single Inheritance :-

When a class inherits another class, it is known as a single inheritance. In the example
given below, Dog class inherits the Animal class, so there is the single inheritance.
//Java program of single Inheritance
Class Animal{
int num=2;
Void eat(){
System.out.println(“eating…(Inside Super Class”);
}
}
Class Dog extends Animal{
Void bark(){
System.out.println(“barking…(Inside Child Class)”);
System.out.println(“Number of dog= “+num);
}

}
Class SingleInheritance{
Public static void main(String args[]){ //Main Method
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output
2. Multilevel Inheritance

When there is a chain of inheritance, it is known as multilevel inheritance. As you can


see in the example given below, BabyDog class inherits the Dog class which again
inherits the Animal class, so there is a multilevel inheritance.
class shape{ //super class
int side=3;
int r=7;
int l=3;
int b=5;
}
class Perimeter extends shape{ //sub-child class
void Cal_perimeter(){
System.out.println("Perimeter of Rectangle= "+2*(l+b));
System.out.println("Perimeter of square="+4*side);
System.out.println("Circumference of circle="+3.14*2*r);
}
}
class Area extends Perimeter{ // Child Class
void Cal_area(){
System.out.println("\nArea of Rectangle= "+(l*b));
System.out.println("Area of square="+side*side);
System.out.println("Area of circle="+3.14*r*r);
}
}
public class Multilevel_Inheritance{
public static void main(String[] args) { //Main Method
Area obj1 = new Area();
obj1.Cal_perimeter();
obj1.Cal_area();
}
}
Output:

3. Hierarchical Inheritance

When two or more classes inherits a single class, it is known as hierarchical inheritance.
In the example given below, Dog and Cat classes inherits the Animal class, so there is
hierarchical inheritance.
class Animal{
void eat(){
System.out.println(“eating…”);
}
}
class Dog extends Animal{
void bark(){
System.out.println(“barking…-Dog”);
}
}
class Cat extends Animal{
void meow(){
System.out.println(“meowing…-Cat”);
}
}
class Hierarchical_Inheritance{
public static void main(String args[]){ //Main Method
Cat obj=new Cat();
obj.meow();
obj.eat();
//obj.bark(); //Compile .Time.Error
Dog obj2 =new Dog();
obj2.bark();
obj2.eat();
}
}
Output:

You might also like