0% found this document useful (0 votes)
8 views

Chapter 3.3

This document discusses the concept of interfaces in Java, explaining their role as blueprints for classes that provide abstraction and support multiple inheritance. It details how to declare interfaces, implement them in classes, and demonstrates hybrid inheritance through examples. Additionally, it covers nested interfaces and provides homework assignments related to the topic.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter 3.3

This document discusses the concept of interfaces in Java, explaining their role as blueprints for classes that provide abstraction and support multiple inheritance. It details how to declare interfaces, implement them in classes, and demonstrates hybrid inheritance through examples. Additionally, it covers nested interfaces and provides homework assignments related to the topic.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit III

Inheritance, Interface and


Package

Mr. R. M. Patil
SY CO, JPR
2023-2024
3.3. Interface in Java
 An interface in Java is a blueprint of a class. It has static
constants and abstract methods.
 The interface in Java is a mechanism to
achieve abstraction.
 There can be only abstract methods in the Java interface,
not method body. It is used to achieve abstraction and
multiple inheritance in Java.
 In other words, you can say that interfaces can have
abstract methods and variables. It cannot have a method
body.
3.3. Interface in Java
Why use Java interface?
3.3. Interface in Java
How to declare an interface?
 An interface is declared by using the interface keyword.
 It provides total abstraction; means all the methods in an interface
are declared with the empty body, and all the fields are public, static
and final by default.
 A class that implements an interface must implement all the
methods declared in the interface.

interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
3.3. Interface in Java
3.3. Interface in Java
As shown in the figure given below, a class extends
another class, an interface extends another interface, but
a class implements an interface.
3.3. Interface in Java Java Interface Example:
interface print
{
void show();
}
class demo implements print
{
public void show()
{
System.out.println("Hello");
}
public static void main(String args[])
{
demo d = new demo();
d.show();
}
}
3.3. Interface in Java Java Interface Example: (Multiple Inheritance)
interface first
{
public void dispaly();
}
interface second
class test
{
{
public void show(); public static void main(String[] args)
} {
class Demo implements first, second Demo d = new Demo();
{ d.display();
public void display() d.show();
{ }
System.out.println("Some text.."); }
}
public void show()
{
System.out.println("Some other text...");
}
}
3.3. Interface in Java – Extending Interface-Hybrid Inheritance
While inheriting an interface through another interface
“extends” keyword is used.

 Hybrid inheritance is the combination of more than one


type of inheritances.

 Since java doesn’t support multiple inheritance with only


classes, the hybrid inheritance is also not possible with only
classes.

 In java, we can achieve hybrid inheritance through


Interfaces.
3.3. Interface in Java – Extending Interface-Hybrid Inheritance
interface A
{
…..
}
interface B extends A
{
…..
}
interface C extends A
{
…..
}
class D implements B, C
{
…..
…..
}
3.3. Interface in Java – Extending Interface-Hybrid Inheritance
interface A class D implements B, C
{ {
void display1(); void display1();
} {
interface B extends A System.out.println(“Class A”);
{ }
void display2(); void display2();
} {
interface C extends A System.out.println(“Class B”);
{ }
void display3(); void display3();
} {
System.out.println(“Class C”);
}
}
3.3. Interface in Java – Extending Interface-Hybrid Inheritance

class demo
{
public static void main(String args[])
{
D d1 = new D();
d1.display1();
d1.display2();
d1.display3();
}
}
3.3. Interface in Java – Interface Reference

Runtime polymorphism can be achieved using interface.

 We can create reference for parent interface.

 This reference can point to an object of the child classes


which implements the corresponding interfaces.
3.3. Interface in Java – Interface Reference

Example:
interface area
void calc();

class demo class circle implements area class triangle implements area
void calc(){….} void calc(){….}
{
public static void main(String args[])
{
area a;
a = new circle();
a.calc(); //Call to circle class method
a = new triangle();
a.calc(); //Call to triangle class method
}
}
3.3. Interface in Java –Nested Interface

An interface i.e. declared within another interface or


class is known as nested interface.

 The nested interfaces are used to group related


interfaces so that they can be easy to maintain.

 The nested interface must be referred by the outer


interface or class. It can't be accessed directly.
3.3. Interface in Java –Nested Interface
Syntax of nested interface which is declared within the interface:
interface interface_name
{
...
interface nested_interface_name
{
...
}
}
Syntax of nested interface which is declared within the class:
class class_name
{
...
interface nested_interface_name
{
...
}
}
3.3. Interface in Java –Nested Interface - Rules
interface Showable
{
void show(); 3.3. Interface in Java –
interface Message Nested Interface - Example
{
void msg();
}
}
class demo implements Showable.Message
{
public void msg()
{
System.out.println("Hello nested interface");
}
public static void main(String args[])
{
Showable.Message m=new demo();
m.msg();
}
}
Homework
1. What is interface? With suitable program explain the use of interface.
W14, W15, S16, S17 – 8M
2. Explain with example how to achieve multiple inheritance with interface.
S15, S16, W16, W17, S18 – 8M
3. Write a program to implement following inheritance. W14, S17 – 4M

interface : salary class : employee


basic_salary name, age
basic_sal() display()

class : gross_salary
TA, DA, HRA
total_sal()
Homework
4. Write a java program to extend interface assuming suitable data. S18 - 6M

5. Difference between Abstract Class and Interface. W14, S16 – 4M

You might also like