Inheritance
is the property of deriving a new class from an existing class.
Types:
Note:
Hierarchical Inheritance
The
existing class or old class is referred to as base class or super class or
parent class
The
new class is referred to as derived class or subclass or child class.
Use
extends keyword to inherit a new
class from existing class.
Inheritance
represents the IS-A relationship.
Note:
Sub class can access both super and sub
class values but super class can access only its own class value.
Advantage
1) Code reusability (You can reuse same code again and again)
2) Extensibility (New class is generated from existing class)
Syntax:
Class derived-class extends base-class
{
//statements
}
Types:
There are three types of inheritance
in JAVA,they are
·
Single
Inheritance
·
Multilevel
Inheritance
·
Hierarchical
Inheritance
Note:
Java does not support
multiple inheritance.In case of interface it supports both Multiple Inheritance and Hybrid Inheritance .
In single inheritance, there
is only one base class and one derived class. The Derived class gets inherited
from its base class
Syntax:
Class A
{
//statements
}
Class B extends A
{
//statements
}
Example:
public class A{
void show(){
System.out.println("Hi");
}
}
class B extends A{
void display(){
System.out.println("Welcome
to LearningPoint92");
}
}
class Demo {
public static void
main(String args[]) {
B d1=new B();
d1.show();
d1.display();
}
}
Output:
Hi
Welcome to LearningPoint92
Multilevel Inheritance
Multilevel Inheritance
The
classes can also be derived from the classes that are already derived. This
type of inheritance is called multilevel inheritance
In
this type of inheritance the derived class inherits from a class, which in turn
inherits from some other class.
Syntax:
class A{
//Statements
}
class B extends A{
//Statements
}
class C extends B{
//Statements
}
Example:
public
class A{
void
show(){System.out.println("Hi");}
}
class
B extends A{
void
display(){System.out.println("Welcome to LearningPoint92");}
}
Class
C extends B
{
void get()
{
System.out.println("Online
training materials are available");
}
}
class
Demo {
public
static void main(String args[]) {
C c1=new C();
c1.show();
c1.display();
c1.get();
}
}
Output:
Hi
Welcome to LearningPoint92
Online training materials
are available
Hierarchical Inheritance
In
Hierarchical inheritance, multiple derived
classes inherits from a single base class
Syntax:
class A{
//statements
}
class B extends A{
//statements
}
class C extends A {
//statements
}
class D extends A{
class D extends A{
//statements
}
Example:
public class
A{
void
show(){System.out.println("Hi");}
}
class B extends
A{
void
display(){System.out.println("Welcome to LearningPoint92");}
}
Class C extends A
{
void get()
{
System.out.println("Online
training materials are available");
}
}
class Demo {
public static void
main(String args[]) {
A a1=new A();
a1.show();
b b1=new B();
B1.display();
C c1=new C();
c1.get();
}
}
Output:
Hi
Welcome to LearningPoint92
Online training materials
are available
Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.
ReplyDelete