28.interface in Java
28.interface in Java
Vishnu
Interface in java
Interface in real world:
You can see number of interface examples. Let us take example of a TV. You press
change channel button of TV remote and channel is changed. Here remote is act as an
interface between you and TV.
A point where two systems, subjects, organizations, etc., meet and interact.
Interface in java:
Syntax:
Interface Interface_Name {
//abstract methods
Syntax:
1
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.
28. Interface By Mr. Vishnu
Note:
Example:
packagecom.sst;
Output:
2
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.
28. Interface By Mr. Vishnu
Name = vishnu
Age = 26
Example:
packagecom.sst;
/**
* This program is used to show multiple inheritance example.
*/
interface ShowAge {
// This method is used to print age.
void age(int age);
}
interface ShowName {
// This method is used to print name.
void name(String name);
}
/**
* This method is used to print name.
*/
@Override
public void name(String name) {
System.out.println("Name = " + name);
3
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.
28. Interface By Mr. Vishnu
// method call
obj.name("vishnu");
obj.age(26);
}
Output:
Name = vishnu
Age = 26
Why multiple inheritance is possible in case of interfaces but not possible in case
of classes?
Multiple inheritance is possible in case of interfaces but not possible in case of classes
because there is no ambiguity problem in case of interfaces as implementation is
provided by class that implements interfaces not by interfaces itself.
Example:
packagecom.sst;
/**
* This program is used to show multiple inheritance is possible in case of
* interfaces because there is no problem of ambiguity.
*/
interface Show {
// This method is used to print name and age.
void show(String name, intage);
}
interface Detail {
// This method is used to print name and age.
void show(String name, intage);
4
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.
28. Interface By Mr. Vishnu
Output:
Name = vishnu
Age = 26
packagecom.sst;
/**
* This program is used to show that an interface can extends multiple
* interfaces.
*/
interface ShowAge {
// This method is used to print age.
void age(intage);
}
interface ShowName {
// This method is used to print name.
void name(String name);
}
/**
* This method is used to print name.
*/
@Override
public void name(String name) {
System.out.println("Name = " + name);
}
/**
* This method is used to print rollNo.
*/
@Override
public void rollNo(int rollNo) {
System.out.println("RollNo = " + rollNo);
}
// method call
obj.name("vishnu");
obj.age(26);
obj.rollNo(4);
}
Output:
6
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.
28. Interface By Mr. Vishnu
Name = vishnu
Age = 26
RollNo = 4
Marker/Tagging Interfaces:
Syntax:
Because interface not have any instance fields so nothing to construct. Now the
question arise how interface can be inherited without constructor because subclass
constructor call super class constructor. Now we have two cases. First case is when an
interface extends other interface, in this case there is no issue because no interface
have constructor and hence no super class constructor call. Second case when a class
implements an interface, in this case there is no inheritance because class implements
interface not extends interface.
7
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile:+91-9885602332.