SlideShare a Scribd company logo
Types of Inheritance
Types :
1.Single Inheritance
2.Multilevel Inheritance
3.Hierarchical Inheritance
4.Multiple Inheritance
Single Inheritance
In single inheritance, a sub-class is derived from only one super
class.
It inherits the properties and behavior of a single-parent class.
A – Parent class
B – child class
Sample program : Write a java program to illustrate the concept of single inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
// Parent class
class One {
void display1()
{
System.out.println(“MEPCO");
}
}
class Two extends One {
public void display2()
{
System.out.println(“JAVA"); }
}
// Driver class
class Main {
public static void main(String[] args)
{
Two g = new Two();
g.display1();
g.display2();
}
}
Multilevel Inheritance
• In Multilevel Inheritance, a derived class will be inheriting a
base class, and as well as the derived class also acts as the
base class for other classes
Write a simple java program to illustrate the concept of Multilevel inheritance :
class Name {
public void print_firstname()
{
System.out.println(“Mepco");
}
}
Class Middlename extends Name {
public void print_middlename() {
System.out.println(“Schnlek");
}
}
class Lastname extends Middlename{
public void print_lastname() {
System.out.println(“College");
}
}
// Driver class
public class Main {
public static void main(String[] args) {
Lastname g = new Lastname();
Calling method from class One
g.firstname();
g.middlename();
g.lastname();
}
}
Hierarchical Inheritance
• In Hierarchical Inheritance, one class serves as a superclass
(base class) for more than one subclass. In the below image,
class A serves as a base class for the derived classes B, C, and
D.
Write a simple java program to illustrate the concept of Hierarchial inheritance :
class A {
public void print_A() { System.out.println("Class A"); }
}
class B extends A {
public void print_B() { System.out.println("Class B"); }
}
class C extends A {
public void print_C() { System.out.println("Class C"); }
}
class D extends A {
public void print_D() { System.out.println("Class D"); }
}
public class Test {
public static void main(String[] args)
{ B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
C obj_C = new C();
obj_C.print_A();
obj_C.print_C();
D obj_D = new D();
obj_D.print_A();
obj_D.print_D();
}
}
Multiple Inheritance :
• In Multiple inheritances, one class can have more than one
superclass and inherit features from all parent classes.
• Please note that Java does not support multiple inheritances
with classes.
• In Java, we can achieve multiple inheritances only through
Interfaces.
What is Interfaces ?
The interface in Java is a mechanism to achieve abstraction.
An interface could only have abstract methods (methods
without a body) and public, static, and final variables by
default.
It is used to achieve abstraction and multiple inheritances in
Java.
In other words, interfaces primarily define methods that
other classes must implement.
Relationship words :
Syntax :
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
In other words,
Rules to declare “interface” class :
• That means all the methods in an interface are declared
with an empty body and are public and all fields are public,
static, and final by default.
• A class that implements an interface must implement all
the methods declared in the interface.
• To implement the interface, use the implements keyword.
it is the quick gest about the interfaces in java
Concept #1:
• We can have method body in interface. But we need to
make it default method. Let's see an example:
interface Interface1 {
default void show()
{
System.out.println(“Greetings!!!");
}
}
interface Interface2 extends Interface1 {
// Abstract method
void display();
}
// Interface 3
interface Interface3 extends Interface1 {
// Abstract method
void print();
}
// Main class
class TestClass implements Interface2, Interface3 {
// Overriding the abstract method from Interface2
public void display()
{ System.out.println(“Hello everyone");
}
• // Overriding the abstract method from Interface3
• public void print()
• {
• System.out.println(“WELOME TO JAVA WORLD");
• }
• // Main driver method
• public static void main(String args[])
• {
• TestClass d = new TestClass();
• // Now calling the methods from both the interfaces
• d.show(); // Default method from API
• d.display(); // Overridden method from Interface1
• d.print(); // Overridden method from Interface2
• }
• }
•
Concept #2:
We can have static method in Interface and it is allowed to use it in
Java.
Concept 3 : Static Method in Interface
interface Drawable
{
void draw();
static int cube(int x)
{
return x*x*x;
}
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle"
);}
}
class A{
public static void main(String args[])
{
Drawable d=new Rectangle();
d.draw();
Points to remember :
• One interface can inherit another by the use of keyword
“extends”.
• When a class implements an interface that inherits another
interface, it must provide an implementation for all methods
required by the interface inheritance chain.
• A class implements an interface, but one interface extends another
interface.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple
inheritance.
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
9)Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
Points
Abstract Class Interface
Type of Methods Can have both abstract and concrete methods
Can have only abstract methods (until Java 7), and from Java 8,
can have default and static methods, and from Java 9, can
have private methods.
Variables Can have final, non-final, static, and non-static variables. Only static and final variables
Inheritance Can extend only one class (abstract or not) A class can implement multiple interfaces
Constructors Can have constructors Cannot have constructors
Implementation Can provide the implementation of interface methods Cannot provide the implementation of abstract class methods
Practice program 1 :
Create a Drawable interface has only one method “draw”. Its
implementation is provided by Rectangle and Circle classes.
Source code :
1. interface Drawable{
2. void draw();
3. }
4. //Implementation: by second user
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle implements Drawable{
9. public void draw(){System.out.println("drawing circle");}
10. }
11. //Using interface: by third user
12. class TestInterface1{
13. public static void main(String args[]){
14. Drawable d=new Circle();
15. d.draw();
16. }}
•
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw()
{
System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Practice program 2 :
Let's see another example of java interface which provides
the implementation of Bank interface.
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10.
class TestInterface2{
11.
public static void main(String[] args){
12.
Bank b=new SBI();
13.
System.out.println("ROI: "+b.rateOfInterest());
14.
}}

More Related Content

Similar to it is the quick gest about the interfaces in java (20)

PPTX
abstract,final,interface (1).pptx upload
dashpayal697
 
PPT
Java interface
Arati Gadgil
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
Inheritance
disha singh
 
PPTX
ITTutor Advanced Java (1).pptx
kristinatemen
 
DOCX
Java interface
HoneyChintal
 
PPTX
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
PPTX
Interface
vvpadhu
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Java notes of Chapter 3 presentation slides
SuyashPatil72
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
Interface in java
PhD Research Scholar
 
PPT
L7 inheritance
teach4uin
 
PPT
L7 inheritance
teach4uin
 
abstract,final,interface (1).pptx upload
dashpayal697
 
Java interface
Arati Gadgil
 
Pi j3.2 polymorphism
mcollison
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Inheritance
disha singh
 
ITTutor Advanced Java (1).pptx
kristinatemen
 
Java interface
HoneyChintal
 
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
Interface
vvpadhu
 
oops with java modules i & ii.ppt
rani marri
 
Inheritance in java
Tech_MX
 
Java notes of Chapter 3 presentation slides
SuyashPatil72
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Interface in java
PhD Research Scholar
 
L7 inheritance
teach4uin
 
L7 inheritance
teach4uin
 

More from arunkumarg271 (9)

PDF
Measurement_of_Blood_Pressure_and_Heart_Sound_1722949324827.pdf
arunkumarg271
 
PPTX
Power system analysis in load flow analysis.pptx
arunkumarg271
 
PPTX
Block chain Technology with digital and social impacts.pptx
arunkumarg271
 
PDF
Hydro_Electric_Power_plant_1738642644263.pdf
arunkumarg271
 
PPTX
it's about the swing programs in java language
arunkumarg271
 
PPTX
it is about the abstract classes in java
arunkumarg271
 
PPT
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
 
PPTX
EM BRAKES.pptx
arunkumarg271
 
PPTX
GRT.pptx
arunkumarg271
 
Measurement_of_Blood_Pressure_and_Heart_Sound_1722949324827.pdf
arunkumarg271
 
Power system analysis in load flow analysis.pptx
arunkumarg271
 
Block chain Technology with digital and social impacts.pptx
arunkumarg271
 
Hydro_Electric_Power_plant_1738642644263.pdf
arunkumarg271
 
it's about the swing programs in java language
arunkumarg271
 
it is about the abstract classes in java
arunkumarg271
 
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
 
EM BRAKES.pptx
arunkumarg271
 
GRT.pptx
arunkumarg271
 
Ad

Recently uploaded (20)

PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Thermal runway and thermal stability.pptx
godow93766
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Ad

it is the quick gest about the interfaces in java

  • 2. Types : 1.Single Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance
  • 3. Single Inheritance In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. A – Parent class B – child class
  • 4. Sample program : Write a java program to illustrate the concept of single inheritance import java.io.*; import java.lang.*; import java.util.*; // Parent class class One { void display1() { System.out.println(“MEPCO"); } } class Two extends One { public void display2() { System.out.println(“JAVA"); } } // Driver class class Main { public static void main(String[] args) { Two g = new Two(); g.display1(); g.display2(); } }
  • 5. Multilevel Inheritance • In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes
  • 6. Write a simple java program to illustrate the concept of Multilevel inheritance : class Name { public void print_firstname() { System.out.println(“Mepco"); } } Class Middlename extends Name { public void print_middlename() { System.out.println(“Schnlek"); } } class Lastname extends Middlename{ public void print_lastname() { System.out.println(“College"); } } // Driver class public class Main { public static void main(String[] args) { Lastname g = new Lastname(); Calling method from class One g.firstname(); g.middlename(); g.lastname(); } }
  • 7. Hierarchical Inheritance • In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D.
  • 8. Write a simple java program to illustrate the concept of Hierarchial inheritance : class A { public void print_A() { System.out.println("Class A"); } } class B extends A { public void print_B() { System.out.println("Class B"); } } class C extends A { public void print_C() { System.out.println("Class C"); } } class D extends A { public void print_D() { System.out.println("Class D"); } } public class Test { public static void main(String[] args) { B obj_B = new B(); obj_B.print_A(); obj_B.print_B(); C obj_C = new C(); obj_C.print_A(); obj_C.print_C(); D obj_D = new D(); obj_D.print_A(); obj_D.print_D(); } }
  • 9. Multiple Inheritance : • In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. • Please note that Java does not support multiple inheritances with classes. • In Java, we can achieve multiple inheritances only through Interfaces.
  • 10. What is Interfaces ? The interface in Java is a mechanism to achieve abstraction. An interface could only have abstract methods (methods without a body) and public, static, and final variables by default. It is used to achieve abstraction and multiple inheritances in Java. In other words, interfaces primarily define methods that other classes must implement.
  • 12. Syntax : interface { // declare constant fields // declare methods that abstract // by default. } In other words,
  • 13. Rules to declare “interface” class : • That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. • A class that implements an interface must implement all the methods declared in the interface. • To implement the interface, use the implements keyword.
  • 15. Concept #1: • We can have method body in interface. But we need to make it default method. Let's see an example:
  • 16. interface Interface1 { default void show() { System.out.println(“Greetings!!!"); } } interface Interface2 extends Interface1 { // Abstract method void display(); } // Interface 3 interface Interface3 extends Interface1 { // Abstract method void print(); } // Main class class TestClass implements Interface2, Interface3 { // Overriding the abstract method from Interface2 public void display() { System.out.println(“Hello everyone"); } • // Overriding the abstract method from Interface3 • public void print() • { • System.out.println(“WELOME TO JAVA WORLD"); • } • // Main driver method • public static void main(String args[]) • { • TestClass d = new TestClass(); • // Now calling the methods from both the interfaces • d.show(); // Default method from API • d.display(); // Overridden method from Interface1 • d.print(); // Overridden method from Interface2 • } • } •
  • 17. Concept #2: We can have static method in Interface and it is allowed to use it in Java.
  • 18. Concept 3 : Static Method in Interface interface Drawable { void draw(); static int cube(int x) { return x*x*x; } } class Rectangle implements Drawable { public void draw() { System.out.println("drawing rectangle" );} } class A{ public static void main(String args[]) { Drawable d=new Rectangle(); d.draw();
  • 19. Points to remember : • One interface can inherit another by the use of keyword “extends”. • When a class implements an interface that inherits another interface, it must provide an implementation for all methods required by the interface inheritance chain. • A class implements an interface, but one interface extends another interface. • It is used to achieve abstraction. • By interface, we can support the functionality of multiple inheritance.
  • 20. Abstract class Interface 1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only. 7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements". 8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default. 9)Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); }
  • 21. Points Abstract Class Interface Type of Methods Can have both abstract and concrete methods Can have only abstract methods (until Java 7), and from Java 8, can have default and static methods, and from Java 9, can have private methods. Variables Can have final, non-final, static, and non-static variables. Only static and final variables Inheritance Can extend only one class (abstract or not) A class can implement multiple interfaces Constructors Can have constructors Cannot have constructors Implementation Can provide the implementation of interface methods Cannot provide the implementation of abstract class methods
  • 22. Practice program 1 : Create a Drawable interface has only one method “draw”. Its implementation is provided by Rectangle and Circle classes.
  • 23. Source code : 1. interface Drawable{ 2. void draw(); 3. } 4. //Implementation: by second user 5. class Rectangle implements Drawable{ 6. public void draw(){System.out.println("drawing rectangle");} 7. } 8. class Circle implements Drawable{ 9. public void draw(){System.out.println("drawing circle");} 10. } 11. //Using interface: by third user 12. class TestInterface1{ 13. public static void main(String args[]){ 14. Drawable d=new Circle(); 15. d.draw(); 16. }} •
  • 24. interface Drawable{ void draw(); default void msg(){System.out.println("default method");} } class Rectangle implements Drawable{ public void draw() { System.out.println("drawing rectangle");} } class TestInterfaceDefault{ public static void main(String args[]){ Drawable d=new Rectangle(); d.draw(); d.msg(); }}
  • 25. Practice program 2 : Let's see another example of java interface which provides the implementation of Bank interface.
  • 26. 1. interface Bank{ 2. float rateOfInterest(); 3. } 4. class SBI implements Bank{ 5. public float rateOfInterest(){return 9.15f;} 6. } 7. class PNB implements Bank{ 8. public float rateOfInterest(){return 9.7f;} 9. } 10. class TestInterface2{ 11. public static void main(String[] args){ 12. Bank b=new SBI(); 13. System.out.println("ROI: "+b.rateOfInterest()); 14. }}