interface
---------
interface is similar to a class which consists
final fields
abstract methods
default methods
static methods
we declare interface using keyword "interface"
syntax
------
accessmodifier interface interfacename
{
//final fields
//abstract methods
//default methods
//static methods
}
ex:
public interface Shape
{
public static final double pi=3.14;
abstract public double area();
}
By default variables of interface are public static and final.
By default methods of interfaces are public and abstract.
we implement an interface in a class.
we use the keyword "implements" to implemet interface in a class.
syntax
------
interface InterfaceName
{
}
class SubClass implements InterfaceName
{
}
A -----variable cannot be modified.
final variables are used to declare-----
-------is the only modifier declared with local variables.
------method cannot be overridden.
------and ----- combination is illegal.
------class cannot be inherited.
we can restrict inheritance in ---ways and they are -------and -------.
interface consists of---,---,---,---.
------keyword is used to declare interface.
variables of interface are by default---,---,---.
abstract methods of interface are by default ---and ---.
we -------interface in a------using------keyword.
we override abstract methods of interface in a sub class.
if we implement interface in a class we should override all the abstract methods of interface in a subclass otherwise the subclass also must be declared as abstract.
we cannot instantiate interfaces.
Program
-------
package com.manohar.java;
interface I1{
void m1();
void m2();
void m3();
}
public class I1Impl implements I1 {
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
@Override
public void m3() {
System.out.println("m3");
}
public static void main(String[] args) {
I1Impl i1=new I1Impl();
i1.m1();
i1.m2();
i1.m3();
}
}
Multiple inheritance using interfaces
-------------------------------------
Deriving a subclass from 2 or more super classes is known as multiple inheritance.
we can implement any number of interfaces in a subclass depends on requirement.
syntax to implement multiple interfaces in a subclass
--------
interface interface1
{
}
interface interface2
{
}
.
.
.
class SubClassName implements interface1,interface2,...
{
//
}
In java we can extend only one class from another class whereas we can implement multiple interfaces in a subclass.
Program
-------
package com.manohar.java;
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2{
public abstract void n1();
public abstract void n2();
}
public class I1Impl implements I1,I2{
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
@Override
public void n1() {
System.out.println("n1");
}
@Override
public void n2() {
System.out.println("n2");
}
public static void main(String[] args) {
I1Impl impl=new I1Impl();
impl.m1();
impl.m2();
impl.n1();
impl.n2();
//
I1 i1=new I1Impl();
i1.m1();
i1.m2();
I2 i2=new I1Impl();
i2.n1();
i2.n2();
}
}
interfaces supports loose coupling.
coupling
tight coupling
loose coupling(interfaces)
coupling
--------
The degree of dependency of one class on another is called as coupling.
Tight coupling
Loose coupling
Tight coupling
--------------
The degree of dependecy of one class on another is high such kind of coupling is called tight coupling.
ex:
class A
{
B b=new B();
}
class B
{
C c=new C();
}
class C
{
}
In the above code if A must exist before that B must exist,if B must exist before that C must exist.Here there is a dependecy of one class on another such kind of dependency is called Coupling.
Loose coupling
--------------
if there is less dependency of one class on another we call this as loose coupling.
we can accomplish loose coupling by using pojo-poji model.
POJI--Plain old java interface[simple java interface].
POJO--Plain old java Object[simple java class].
In a simple note implementing an interface in a class is known pojo-poji model which is higly useful in frameworks.
Program
-------
interface Vehicle
{
public abstract void move();
}
class Bus implements Vehicle
{
public void move()
{
System.out.println("Hey iam on Bus");
}
}
class Car implements Vehicle
{
public void move()
{
System.out.println("Huuu iam on Car");
}
}
class Journey{
private Vehicle v;
public Journey(Vehicle v)
{
this.v=v;
}
public void travel()
{
v.move();
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
Car c=new Car();
Journey j1=new Journey(c);
j1.travel();
}
}
In the above code we are implementing Vehicle interface in Bus and Car classes.
This approach makes objects loosely coupled,as we know from dynamic method dispatch a super class refer to a sub class object,an interface reference also can refer to its implementation class objects.
May be we can't instantiate interfaces,abstract classes but still the reference of interface or abstract class can refer to a subclass to object.
This approch is very popular in jdbc,spring,hibernate etc.
In the above code we are declaring Journey class,it consists of Vehicle as instance variable.
we are initializing Vehicle through constructor as shown in the above code.
Declaring object of one class in another class is called as HAS-A relation also this called as composition.
In the code Vehicle is called as dependency in Journey class.
Journey is called dependent.
In tight coupling if we make any changes to a dependency automatically it will affect dependent i.e if we make changes to Bus or Car then Journey will get affected.see below code.
ex:
class Journey
{
Bus b=new Bus();
..
..
}
Here in the above code if we replace Bus with Car i,e if we change dependency then Journey get affected.
So whenever we make changes to dependency it is going to impact dependent to avoid this loose coupling is introduced.
we can achieve loose coupling with above approach declared in Journey class,here we are using inteface reference to refer to subclass objects using constructor injection.
Here to the constructor we may pass Car object or Bus object we dont need to make any changes to Journey,so it reduces dependency of objects in a class.
Constructor injection
---------------------
Passing primitives or objects to a constructor is called Constructor injection.
case1:
if 2 interfaces has same methods then it is sufficient to provide a common implementation for both for these interfaces in a subclass for one time.
Program
-------
interface I1
{
public abstract void m1();
}
interface I2
{
public abstract void m1();
}
class I1I2 implements I1,I2
{
public void m1()
{
System.out.println("m1");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
}
}
case2:if 2 interfaces has same variables then we should invoke them in a sub class using InterfaceName,as we know variables of interface are final static and public,so we can access a variable of inteface using InterfaceName,otherwise it leads to ambiguity error.
Program
-------
interface I1
{
int a=10;
}
interface I2
{
int a=20;
}
class I1I2 implements I1,I2
{
public void m1()
{
System.out.println("a is "+I1.a);
System.out.println("a is "+I2.a);
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
}
}
Extending interfaces
--------------------
It is possible to extend one interface from another interface.
syntax
------
accessmodifier interface Interface1
{
//
}
accessmodifier interface Interface2 extends Interface1
{
//
}
Program to demonstrate extending one interface from another and implementing in a subclass.
--------------------------------------------------
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2 extends I1
{
public abstract void m3();
}
class I1I2 implements I2
{
public void m1()
{
System.out.println("m1");
}
public void m2()
{
System.out.println("m2");
}
public void m3()
{
System.out.println("m3");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
i1.m3();
}
}
In the above code we are extending I2 from I1 and implementing I2 in a subclass I1I2,here if we impement I2 in a subclass it is sufficient and we should override both the abstract of I1,I2.
case1:
we can extend multiple interfaces to another interface i,e multiple inheritance is possible between interfaces.
Program
-------
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2
{
public abstract void m3();
}
interface I3 extends I1,I2
{
public abstract void m4();
}
class I1I2 implements I3
{
public void m1()
{
System.out.println("m1");
}
public void m2()
{
System.out.println("m2");
}
public void m3()
{
System.out.println("m3");
}
public void m4()
{
System.out.println("m4");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
i1.m3();
i1.m4();
}
}
Hybrid multiple inheritance
---------------------------
It is possible to extend a class and implement interfaces together in a subclass.
syntax
------
class Class1
{
}
interface I1
{
}
interface I2
{
}
..
class SubClass extends Class1 implements I1,I2,...
{
}
Program
-------
class Class1
{
public void m1(){
System.out.println("m1");
}
}
interface I1
{
public abstract void m2();
}
class ABC extends Class1 implements I1
{
public void m2()
{
System.out.println("m2");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
}
}
default methods in interfaces
-----------------------------
A method with default implementation in an inteface is called as default method.
we should declare default method in interface using "default" keyword.
syntax
------
public default returntype methodname()
{
}
ex:
public default void m1()
{
}
default methods makes maintenance of projects easy and simple.
suppose over a period of time the number of abstarct methods we add to interfaces increases to a level where addition of new abstract methods to an interface becomes difficult because the number of implementation classes also grows parallely once we introduce a new abstract method all the implementation classes should override these methods even if is necessary or not,to avoid this situation default methods are introduced so that we don't need to implement these methods in a subclass,the subclass which needs these methods will override them and the other classes may use default methods as it is.
Program
-------
interface I1 {
void m1();
void m2();
default void m3() {
System.out.println("m3");
}
}
class A implements I1 {
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
}
public class DefaultDemo {
public static void main(String[] args) {
A a = new A();
a.m1();
a.m2();
a.m3();
}
}
static method
-------------
We can declare static methods in interfaces from 1.8 version onwards.
static methods are used to implement utility logic.
syntax
------
public static returntype methodname()
{
//
}
Program
-------
interface I1 {
public static int max(int x, int y) {
if (x > y) {
return x;
} else {
return y;
}
}
public default void m3() {
System.out.println("m3");
}
}
class A implements I1 {
public void m1() {
System.out.println("m1");
}
}
public class DefaultDemo {
public static void main(String[] args) {
A a = new A();
a.m1();
a.m3();
System.out.println(I1.max(10, 20));
}
}
---------
interface is similar to a class which consists
final fields
abstract methods
default methods
static methods
we declare interface using keyword "interface"
syntax
------
accessmodifier interface interfacename
{
//final fields
//abstract methods
//default methods
//static methods
}
ex:
public interface Shape
{
public static final double pi=3.14;
abstract public double area();
}
By default variables of interface are public static and final.
By default methods of interfaces are public and abstract.
we implement an interface in a class.
we use the keyword "implements" to implemet interface in a class.
syntax
------
interface InterfaceName
{
}
class SubClass implements InterfaceName
{
}
A -----variable cannot be modified.
final variables are used to declare-----
-------is the only modifier declared with local variables.
------method cannot be overridden.
------and ----- combination is illegal.
------class cannot be inherited.
we can restrict inheritance in ---ways and they are -------and -------.
interface consists of---,---,---,---.
------keyword is used to declare interface.
variables of interface are by default---,---,---.
abstract methods of interface are by default ---and ---.
we -------interface in a------using------keyword.
we override abstract methods of interface in a sub class.
if we implement interface in a class we should override all the abstract methods of interface in a subclass otherwise the subclass also must be declared as abstract.
we cannot instantiate interfaces.
Program
-------
package com.manohar.java;
interface I1{
void m1();
void m2();
void m3();
}
public class I1Impl implements I1 {
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
@Override
public void m3() {
System.out.println("m3");
}
public static void main(String[] args) {
I1Impl i1=new I1Impl();
i1.m1();
i1.m2();
i1.m3();
}
}
Multiple inheritance using interfaces
-------------------------------------
Deriving a subclass from 2 or more super classes is known as multiple inheritance.
we can implement any number of interfaces in a subclass depends on requirement.
syntax to implement multiple interfaces in a subclass
--------
interface interface1
{
}
interface interface2
{
}
.
.
.
class SubClassName implements interface1,interface2,...
{
//
}
In java we can extend only one class from another class whereas we can implement multiple interfaces in a subclass.
Program
-------
package com.manohar.java;
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2{
public abstract void n1();
public abstract void n2();
}
public class I1Impl implements I1,I2{
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
@Override
public void n1() {
System.out.println("n1");
}
@Override
public void n2() {
System.out.println("n2");
}
public static void main(String[] args) {
I1Impl impl=new I1Impl();
impl.m1();
impl.m2();
impl.n1();
impl.n2();
//
I1 i1=new I1Impl();
i1.m1();
i1.m2();
I2 i2=new I1Impl();
i2.n1();
i2.n2();
}
}
interfaces supports loose coupling.
coupling
tight coupling
loose coupling(interfaces)
coupling
--------
The degree of dependency of one class on another is called as coupling.
Tight coupling
Loose coupling
Tight coupling
--------------
The degree of dependecy of one class on another is high such kind of coupling is called tight coupling.
ex:
class A
{
B b=new B();
}
class B
{
C c=new C();
}
class C
{
}
In the above code if A must exist before that B must exist,if B must exist before that C must exist.Here there is a dependecy of one class on another such kind of dependency is called Coupling.
Loose coupling
--------------
if there is less dependency of one class on another we call this as loose coupling.
we can accomplish loose coupling by using pojo-poji model.
POJI--Plain old java interface[simple java interface].
POJO--Plain old java Object[simple java class].
In a simple note implementing an interface in a class is known pojo-poji model which is higly useful in frameworks.
Program
-------
interface Vehicle
{
public abstract void move();
}
class Bus implements Vehicle
{
public void move()
{
System.out.println("Hey iam on Bus");
}
}
class Car implements Vehicle
{
public void move()
{
System.out.println("Huuu iam on Car");
}
}
class Journey{
private Vehicle v;
public Journey(Vehicle v)
{
this.v=v;
}
public void travel()
{
v.move();
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
Car c=new Car();
Journey j1=new Journey(c);
j1.travel();
}
}
In the above code we are implementing Vehicle interface in Bus and Car classes.
This approach makes objects loosely coupled,as we know from dynamic method dispatch a super class refer to a sub class object,an interface reference also can refer to its implementation class objects.
May be we can't instantiate interfaces,abstract classes but still the reference of interface or abstract class can refer to a subclass to object.
This approch is very popular in jdbc,spring,hibernate etc.
In the above code we are declaring Journey class,it consists of Vehicle as instance variable.
we are initializing Vehicle through constructor as shown in the above code.
Declaring object of one class in another class is called as HAS-A relation also this called as composition.
In the code Vehicle is called as dependency in Journey class.
Journey is called dependent.
In tight coupling if we make any changes to a dependency automatically it will affect dependent i.e if we make changes to Bus or Car then Journey will get affected.see below code.
ex:
class Journey
{
Bus b=new Bus();
..
..
}
Here in the above code if we replace Bus with Car i,e if we change dependency then Journey get affected.
So whenever we make changes to dependency it is going to impact dependent to avoid this loose coupling is introduced.
we can achieve loose coupling with above approach declared in Journey class,here we are using inteface reference to refer to subclass objects using constructor injection.
Here to the constructor we may pass Car object or Bus object we dont need to make any changes to Journey,so it reduces dependency of objects in a class.
Constructor injection
---------------------
Passing primitives or objects to a constructor is called Constructor injection.
case1:
if 2 interfaces has same methods then it is sufficient to provide a common implementation for both for these interfaces in a subclass for one time.
Program
-------
interface I1
{
public abstract void m1();
}
interface I2
{
public abstract void m1();
}
class I1I2 implements I1,I2
{
public void m1()
{
System.out.println("m1");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
}
}
case2:if 2 interfaces has same variables then we should invoke them in a sub class using InterfaceName,as we know variables of interface are final static and public,so we can access a variable of inteface using InterfaceName,otherwise it leads to ambiguity error.
Program
-------
interface I1
{
int a=10;
}
interface I2
{
int a=20;
}
class I1I2 implements I1,I2
{
public void m1()
{
System.out.println("a is "+I1.a);
System.out.println("a is "+I2.a);
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
}
}
Extending interfaces
--------------------
It is possible to extend one interface from another interface.
syntax
------
accessmodifier interface Interface1
{
//
}
accessmodifier interface Interface2 extends Interface1
{
//
}
Program to demonstrate extending one interface from another and implementing in a subclass.
--------------------------------------------------
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2 extends I1
{
public abstract void m3();
}
class I1I2 implements I2
{
public void m1()
{
System.out.println("m1");
}
public void m2()
{
System.out.println("m2");
}
public void m3()
{
System.out.println("m3");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
i1.m3();
}
}
In the above code we are extending I2 from I1 and implementing I2 in a subclass I1I2,here if we impement I2 in a subclass it is sufficient and we should override both the abstract of I1,I2.
case1:
we can extend multiple interfaces to another interface i,e multiple inheritance is possible between interfaces.
Program
-------
interface I1
{
public abstract void m1();
public abstract void m2();
}
interface I2
{
public abstract void m3();
}
interface I3 extends I1,I2
{
public abstract void m4();
}
class I1I2 implements I3
{
public void m1()
{
System.out.println("m1");
}
public void m2()
{
System.out.println("m2");
}
public void m3()
{
System.out.println("m3");
}
public void m4()
{
System.out.println("m4");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
i1.m3();
i1.m4();
}
}
Hybrid multiple inheritance
---------------------------
It is possible to extend a class and implement interfaces together in a subclass.
syntax
------
class Class1
{
}
interface I1
{
}
interface I2
{
}
..
class SubClass extends Class1 implements I1,I2,...
{
}
Program
-------
class Class1
{
public void m1(){
System.out.println("m1");
}
}
interface I1
{
public abstract void m2();
}
class ABC extends Class1 implements I1
{
public void m2()
{
System.out.println("m2");
}
public static void main(String args[])
{
I1I2 i1=new I1I2();
i1.m1();
i1.m2();
}
}
default methods in interfaces
-----------------------------
A method with default implementation in an inteface is called as default method.
we should declare default method in interface using "default" keyword.
syntax
------
public default returntype methodname()
{
}
ex:
public default void m1()
{
}
default methods makes maintenance of projects easy and simple.
suppose over a period of time the number of abstarct methods we add to interfaces increases to a level where addition of new abstract methods to an interface becomes difficult because the number of implementation classes also grows parallely once we introduce a new abstract method all the implementation classes should override these methods even if is necessary or not,to avoid this situation default methods are introduced so that we don't need to implement these methods in a subclass,the subclass which needs these methods will override them and the other classes may use default methods as it is.
Program
-------
interface I1 {
void m1();
void m2();
default void m3() {
System.out.println("m3");
}
}
class A implements I1 {
@Override
public void m1() {
System.out.println("m1");
}
@Override
public void m2() {
System.out.println("m2");
}
}
public class DefaultDemo {
public static void main(String[] args) {
A a = new A();
a.m1();
a.m2();
a.m3();
}
}
static method
-------------
We can declare static methods in interfaces from 1.8 version onwards.
static methods are used to implement utility logic.
syntax
------
public static returntype methodname()
{
//
}
Program
-------
interface I1 {
public static int max(int x, int y) {
if (x > y) {
return x;
} else {
return y;
}
}
public default void m3() {
System.out.println("m3");
}
}
class A implements I1 {
public void m1() {
System.out.println("m1");
}
}
public class DefaultDemo {
public static void main(String[] args) {
A a = new A();
a.m1();
a.m3();
System.out.println(I1.max(10, 20));
}
}
No comments:
Post a Comment