OOP IN JAVA -( INTERFACE - "Implement" KEYWORD)
INTERFACE
An interface is a concept like a class that can be used to inherit because there is no way to do multiple inheritance in java, but it is not a class.
WHY USE INTERFACE
- It is used to achieve abstraction.
- By interface, we can support the functionality of multiple inheritance.
- It can be used to achieve loose coupling
NOTE : Implements keyword is used in interface just like extend in class
- interfaces cannot be used to create objects and other thing is an interface cannot contain a constructor (as it cannot be used to create objects)
Ex - interface School{
}
Animal a = new Animal(); // Total Wrong creation method
- Interface methods are by default
abstract
andpublic
Ex - interface Banuka{
public abstract void run () ;
}
- Interface attributes are by default
public
,static
andfinal
Ex - interface Human{
public abstract void run ();
public abstract final string rice;
}
- Interface methods do not have a body - the body is provided by the "implement" class and other thing is an implementation of an interface, you must override all of its methods by using implements keyword
Ex - class Country {
}
interface Cities{
}
class towns extends Country implements Cities {
}
Ex - class A{}
interface c {}
interface d {}
interface e {}
class B extend A implements c, d ,e { - multiple inheritance
}
- An interface cannot contain a constructor (as it cannot be used to create objects).
OBJECT CREATION WITH INTERFACE
EX - class A{}
interface c {}
interface d {}
interface e {}
class B extend A implements c, d ,e {
}
C c = new B () ;
D d = new B () ;
E e = new B () ;
THANK YOU!
Comments
Post a Comment