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.
INTERFACES. with machine learning and data
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.
}}
Ad

More Related Content

Similar to INTERFACES. with machine learning and data (20)

OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Abstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphismAbstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
Ganesh Samarthyam
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdfFINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
myrajendra
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.pptweb program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
KumarUtsav24
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
TharuniDiddekunta
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Inheritance
InheritanceInheritance
Inheritance
abhay singh
 
Jacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.pptJacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
JAVA.pptx
JAVA.pptxJAVA.pptx
JAVA.pptx
SohamMathur1
 
Java interface
Java interface Java interface
Java interface
HoneyChintal
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
ssuser84e52e
 
Abstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphismAbstraction encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
Ganesh Samarthyam
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdfFINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
myrajendra
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.pptweb program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Jacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.pptJacarashed-1746968053-300050282-Java.ppt
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 

Recently uploaded (20)

Simple_AI_Explanation_English somplr.pptx
Simple_AI_Explanation_English somplr.pptxSimple_AI_Explanation_English somplr.pptx
Simple_AI_Explanation_English somplr.pptx
ssuser2aa19f
 
computer organization and assembly language.docx
computer organization and assembly language.docxcomputer organization and assembly language.docx
computer organization and assembly language.docx
alisoftwareengineer1
 
FPET_Implementation_2_MA to 360 Engage Direct.pptx
FPET_Implementation_2_MA to 360 Engage Direct.pptxFPET_Implementation_2_MA to 360 Engage Direct.pptx
FPET_Implementation_2_MA to 360 Engage Direct.pptx
ssuser4ef83d
 
C++_OOPs_DSA1_Presentation_Template.pptx
C++_OOPs_DSA1_Presentation_Template.pptxC++_OOPs_DSA1_Presentation_Template.pptx
C++_OOPs_DSA1_Presentation_Template.pptx
aquibnoor22079
 
Cleaned_Lecture 6666666_Simulation_I.pdf
Cleaned_Lecture 6666666_Simulation_I.pdfCleaned_Lecture 6666666_Simulation_I.pdf
Cleaned_Lecture 6666666_Simulation_I.pdf
alcinialbob1234
 
How to join illuminati Agent in uganda call+256776963507/0741506136
How to join illuminati Agent in uganda call+256776963507/0741506136How to join illuminati Agent in uganda call+256776963507/0741506136
How to join illuminati Agent in uganda call+256776963507/0741506136
illuminati Agent uganda call+256776963507/0741506136
 
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjks
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjksPpt. Nikhil.pptxnshwuudgcudisisshvehsjks
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjks
panchariyasahil
 
Ch3MCT24.pptx measure of central tendency
Ch3MCT24.pptx measure of central tendencyCh3MCT24.pptx measure of central tendency
Ch3MCT24.pptx measure of central tendency
ayeleasefa2
 
Stack_and_Queue_Presentation_Final (1).pptx
Stack_and_Queue_Presentation_Final (1).pptxStack_and_Queue_Presentation_Final (1).pptx
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Molecular methods diagnostic and monitoring of infection - Repaired.pptx
Molecular methods diagnostic and monitoring of infection  -  Repaired.pptxMolecular methods diagnostic and monitoring of infection  -  Repaired.pptx
Molecular methods diagnostic and monitoring of infection - Repaired.pptx
7tzn7x5kky
 
183409-christina-rossetti.pdfdsfsdasggsag
183409-christina-rossetti.pdfdsfsdasggsag183409-christina-rossetti.pdfdsfsdasggsag
183409-christina-rossetti.pdfdsfsdasggsag
fardin123rahman07
 
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
James Francis Paradigm Asset Management
 
VKS-Python Basics for Beginners and advance.pptx
VKS-Python Basics for Beginners and advance.pptxVKS-Python Basics for Beginners and advance.pptx
VKS-Python Basics for Beginners and advance.pptx
Vinod Srivastava
 
03 Daniel 2-notes.ppt seminario escatologia
03 Daniel 2-notes.ppt seminario escatologia03 Daniel 2-notes.ppt seminario escatologia
03 Daniel 2-notes.ppt seminario escatologia
Alexander Romero Arosquipa
 
Conic Sectionfaggavahabaayhahahahahs.pptx
Conic Sectionfaggavahabaayhahahahahs.pptxConic Sectionfaggavahabaayhahahahahs.pptx
Conic Sectionfaggavahabaayhahahahahs.pptx
taiwanesechetan
 
Minions Want to eat presentacion muy linda
Minions Want to eat presentacion muy lindaMinions Want to eat presentacion muy linda
Minions Want to eat presentacion muy linda
CarlaAndradesSoler1
 
Deloitte Analytics - Applying Process Mining in an audit context
Deloitte Analytics - Applying Process Mining in an audit contextDeloitte Analytics - Applying Process Mining in an audit context
Deloitte Analytics - Applying Process Mining in an audit context
Process mining Evangelist
 
Developing Security Orchestration, Automation, and Response Applications
Developing Security Orchestration, Automation, and Response ApplicationsDeveloping Security Orchestration, Automation, and Response Applications
Developing Security Orchestration, Automation, and Response Applications
VICTOR MAESTRE RAMIREZ
 
04302025_CCC TUG_DataVista: The Design Story
04302025_CCC TUG_DataVista: The Design Story04302025_CCC TUG_DataVista: The Design Story
04302025_CCC TUG_DataVista: The Design Story
ccctableauusergroup
 
Flip flop presenation-Presented By Mubahir khan.pptx
Flip flop presenation-Presented By Mubahir khan.pptxFlip flop presenation-Presented By Mubahir khan.pptx
Flip flop presenation-Presented By Mubahir khan.pptx
mubashirkhan45461
 
Simple_AI_Explanation_English somplr.pptx
Simple_AI_Explanation_English somplr.pptxSimple_AI_Explanation_English somplr.pptx
Simple_AI_Explanation_English somplr.pptx
ssuser2aa19f
 
computer organization and assembly language.docx
computer organization and assembly language.docxcomputer organization and assembly language.docx
computer organization and assembly language.docx
alisoftwareengineer1
 
FPET_Implementation_2_MA to 360 Engage Direct.pptx
FPET_Implementation_2_MA to 360 Engage Direct.pptxFPET_Implementation_2_MA to 360 Engage Direct.pptx
FPET_Implementation_2_MA to 360 Engage Direct.pptx
ssuser4ef83d
 
C++_OOPs_DSA1_Presentation_Template.pptx
C++_OOPs_DSA1_Presentation_Template.pptxC++_OOPs_DSA1_Presentation_Template.pptx
C++_OOPs_DSA1_Presentation_Template.pptx
aquibnoor22079
 
Cleaned_Lecture 6666666_Simulation_I.pdf
Cleaned_Lecture 6666666_Simulation_I.pdfCleaned_Lecture 6666666_Simulation_I.pdf
Cleaned_Lecture 6666666_Simulation_I.pdf
alcinialbob1234
 
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjks
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjksPpt. Nikhil.pptxnshwuudgcudisisshvehsjks
Ppt. Nikhil.pptxnshwuudgcudisisshvehsjks
panchariyasahil
 
Ch3MCT24.pptx measure of central tendency
Ch3MCT24.pptx measure of central tendencyCh3MCT24.pptx measure of central tendency
Ch3MCT24.pptx measure of central tendency
ayeleasefa2
 
Stack_and_Queue_Presentation_Final (1).pptx
Stack_and_Queue_Presentation_Final (1).pptxStack_and_Queue_Presentation_Final (1).pptx
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Molecular methods diagnostic and monitoring of infection - Repaired.pptx
Molecular methods diagnostic and monitoring of infection  -  Repaired.pptxMolecular methods diagnostic and monitoring of infection  -  Repaired.pptx
Molecular methods diagnostic and monitoring of infection - Repaired.pptx
7tzn7x5kky
 
183409-christina-rossetti.pdfdsfsdasggsag
183409-christina-rossetti.pdfdsfsdasggsag183409-christina-rossetti.pdfdsfsdasggsag
183409-christina-rossetti.pdfdsfsdasggsag
fardin123rahman07
 
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
James Francis Paradigm Asset Management
 
VKS-Python Basics for Beginners and advance.pptx
VKS-Python Basics for Beginners and advance.pptxVKS-Python Basics for Beginners and advance.pptx
VKS-Python Basics for Beginners and advance.pptx
Vinod Srivastava
 
Conic Sectionfaggavahabaayhahahahahs.pptx
Conic Sectionfaggavahabaayhahahahahs.pptxConic Sectionfaggavahabaayhahahahahs.pptx
Conic Sectionfaggavahabaayhahahahahs.pptx
taiwanesechetan
 
Minions Want to eat presentacion muy linda
Minions Want to eat presentacion muy lindaMinions Want to eat presentacion muy linda
Minions Want to eat presentacion muy linda
CarlaAndradesSoler1
 
Deloitte Analytics - Applying Process Mining in an audit context
Deloitte Analytics - Applying Process Mining in an audit contextDeloitte Analytics - Applying Process Mining in an audit context
Deloitte Analytics - Applying Process Mining in an audit context
Process mining Evangelist
 
Developing Security Orchestration, Automation, and Response Applications
Developing Security Orchestration, Automation, and Response ApplicationsDeveloping Security Orchestration, Automation, and Response Applications
Developing Security Orchestration, Automation, and Response Applications
VICTOR MAESTRE RAMIREZ
 
04302025_CCC TUG_DataVista: The Design Story
04302025_CCC TUG_DataVista: The Design Story04302025_CCC TUG_DataVista: The Design Story
04302025_CCC TUG_DataVista: The Design Story
ccctableauusergroup
 
Flip flop presenation-Presented By Mubahir khan.pptx
Flip flop presenation-Presented By Mubahir khan.pptxFlip flop presenation-Presented By Mubahir khan.pptx
Flip flop presenation-Presented By Mubahir khan.pptx
mubashirkhan45461
 
Ad

INTERFACES. with machine learning and data

  • 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. }}