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

More Related Content

PPTX
inheritance, Packages and Interfaces.pptx
PDF
Basic_Java_10.pdf
PPTX
Interface in java
PPTX
Java presentation
PDF
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
PPTX
Inheritance.pptx
PPTX
abstract,final,interface (1).pptx upload
PPTX
Lecture 6 inheritance
inheritance, Packages and Interfaces.pptx
Basic_Java_10.pdf
Interface in java
Java presentation
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
Inheritance.pptx
abstract,final,interface (1).pptx upload
Lecture 6 inheritance

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

PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PPT
Java interface
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PPTX
Pi j3.2 polymorphism
PPTX
ITTutor Advanced Java (1).pptx
PPTX
Inheritance
DOCX
Java interface
PPTX
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
PPTX
Object Oriented Programming - Polymorphism and Interfaces
PPTX
Inheritance in java
PPTX
Interface
PPT
oops with java modules i & ii.ppt
PPTX
Java notes of Chapter 3 presentation slides
PPTX
Inheritance & interface ppt Inheritance
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
Interface in java
DOC
How would you implement multiple inheritance in java
OOPS_Unit2.inheritance and interface objected oriented programming
Java interface
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Pi j3.2 polymorphism
ITTutor Advanced Java (1).pptx
Inheritance
Java interface
java_unitffjfjfjjrdteszserfdffvjyurt_6.pptx
Object Oriented Programming - Polymorphism and Interfaces
Inheritance in java
Interface
oops with java modules i & ii.ppt
Java notes of Chapter 3 presentation slides
Inheritance & interface ppt Inheritance
Interface in java ,multiple inheritance in java, interface implementation
ABSTRACT CLASSES AND INTERFACES.ppt
Interface in java By Dheeraj Kumar Singh
Interface in java
How would you implement multiple inheritance in java
Ad

Recently uploaded (20)

PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Introduction to machine learning and Linear Models
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
Foundation of Data Science unit number two notes
PDF
.pdf is not working space design for the following data for the following dat...
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PDF
annual-report-2024-2025 original latest.
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
1_Introduction to advance data techniques.pptx
Database Infoormation System (DBIS).pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
oil_refinery_comprehensive_20250804084928 (1).pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
climate analysis of Dhaka ,Banglades.pptx
Fluorescence-microscope_Botany_detailed content
Data_Analytics_and_PowerBI_Presentation.pptx
Supervised vs unsupervised machine learning algorithms
Introduction to machine learning and Linear Models
Introduction to Knowledge Engineering Part 1
Introduction-to-Cloud-ComputingFinal.pptx
Foundation of Data Science unit number two notes
.pdf is not working space design for the following data for the following dat...
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
annual-report-2024-2025 original latest.
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
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. }}