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