filmov
tv
java 31 | Extending Interfaces | Accessing interface fields

Показать описание
In this class we are going to talk about inheriting interfaces and accessing inheritance fields.
A class can inherit multiple interfaces but not multiple classes
An interface can also inherit multiple interfaces
Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity.
It is because its implementation is provided by the implementation class
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface2 implements Printable, Showable{
public static void main(String args[ ]){
TestInterface2 obj = new TestInterface2();
}
}
Output : printing….
As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestInterface2, so there is no ambiguity.
A class implements an interface, but one interface extends another interface.
interface Printable{
void print( );
}
interface Showable extends Printable{
void show();
}
class TestInterface3 implements Showable{
public static void main(String args [ ]){
TestInterface3 obj = new TestInterface3();
}
}
printing…
showing…
Since all the fields of an interface are static by default, you can access them using the name of the interface as −
Example
Interface I1{ public static int num = 100; public void display(); }
public class InEx implements I1{
public static int num = 10000;
public static void main(String args[])
{ InEx obj = new InEx();
}
output:-
Value of num of the interface 100
Value of num of the class 10000
since the variables of an interface are final you cannot reassign values to them.
If you try to do so, a compile-time error will be generated.
A class can implement more than one interface.
A class that implements interface must implements all the methods in interface.
all the fields of an interface are static by default, so you can access them using the name of the interface as
Assignment
Write a program to create an interface Vehicles, and put functionality like changGear, applyBrake,speedUp. And lets Bicycle, Bike, Car classes implement all these functionalities in their own class in their own way.
list of suggested reading
Java: The Complete Reference Book by Herbert Schildt
A class can inherit multiple interfaces but not multiple classes
An interface can also inherit multiple interfaces
Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity.
It is because its implementation is provided by the implementation class
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface2 implements Printable, Showable{
public static void main(String args[ ]){
TestInterface2 obj = new TestInterface2();
}
}
Output : printing….
As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestInterface2, so there is no ambiguity.
A class implements an interface, but one interface extends another interface.
interface Printable{
void print( );
}
interface Showable extends Printable{
void show();
}
class TestInterface3 implements Showable{
public static void main(String args [ ]){
TestInterface3 obj = new TestInterface3();
}
}
printing…
showing…
Since all the fields of an interface are static by default, you can access them using the name of the interface as −
Example
Interface I1{ public static int num = 100; public void display(); }
public class InEx implements I1{
public static int num = 10000;
public static void main(String args[])
{ InEx obj = new InEx();
}
output:-
Value of num of the interface 100
Value of num of the class 10000
since the variables of an interface are final you cannot reassign values to them.
If you try to do so, a compile-time error will be generated.
A class can implement more than one interface.
A class that implements interface must implements all the methods in interface.
all the fields of an interface are static by default, so you can access them using the name of the interface as
Assignment
Write a program to create an interface Vehicles, and put functionality like changGear, applyBrake,speedUp. And lets Bicycle, Bike, Car classes implement all these functionalities in their own class in their own way.
list of suggested reading
Java: The Complete Reference Book by Herbert Schildt