Java interface and programming example?

 

Java interface

Q.1  what is the interface?

v  Interface is similar to an abstract class but having all the methods of abstract type interface are the blueprint of the class it specifies what a class must do and mot now.

v  Interface is a mechanism to achieve abstraction in java.

v  Since the java  8th    version we add default and static methods in an interface.

v  Since the java  9th    version we add default and private methods in an interface.

Write a program interface?

interface   Test    {
public  abstract  void show ();

}

class   Test1  implements Test   {

public void show  ()   {

System.out.println(“one”)

}

public static void main(String   args[])   {

Test1  t1= new  Test1();

t1.show();

}

}

An interface can support multiple inheritances?

Ø  Yes we can achieve multiple inheritances by using an interface.

Example:-

interface   Test    {
public  abstract  void show ();

}

interface   Test1    {
public  abstract  void show1 ();

}

 

class   Test3  implements Test1, Test2   {

public void show  ()   {

System.out.println(“one”)

}

public void show1  ()   {

System.out.println(“two”)

}

 

public static void main(String   args[])   {

Test2  t1= new  Test2();

t2.show();

t2.show1();

}

}

 

interface class example

interface   A1   {

abstract void show ();  

}

v  It is used to achieve abstraction.

v  It supports multiple inheritances.

v  It can be used to achieve loose coupling.

Why use the java interface?

v  It is used to achieve abstraction

v  By interface we can  support the functionality of multiple inheritances

v  It can be used to achieve loose coupling.

v  Similertices between abstracts class and interfaces:-

v  Both can contain abstract method

v  We cannot create an instance of abstracts class and interfaces.

What is loose coupling?

v  Loose coupling is a design goal that seeks to reduce the interdependencies between components of the system to reduce the risk that changes in one component will require changes in any other component. Loose coupling is a much more generic concept intended to increase the flexibility of the system make it more maintainable and makes the entire framework more stable.

Syntax:--

interface   Test    {
abstract  void show ();

}

Ø  Fields –public/static /final/  access modifier

Ø  After 8th  version add feature

Ø  Default concreate method

Ø  Static method

Ø  After 9th version add

Ø  Privet method

Example:-

interface  Test   {

abstract void show() ;     }

public final int a=10;

default  void display()  {

System.out.println(“one”)

}

static void run()   {

System.out.println(“programming khata”)

}

privet  void show()   {

System.out.println(“programming khata”)

}

}

Example1:-

interface  Test   {

public abstract void show() ;     }     //main function of the interface

Example2:-

interface  Test   {

public abstract void show() ;    

default  void show()  {

System.out.println(“one”)

}

}

Example3:-

interface  Test   {

public abstract void show() ;    

default  void show()

static  void show()    {

System.out.println(“one”)

}

}

Example4:-

interface  Test   {

public static final  int a=10;

public abstract void show() ;    

default  void show()    {

//  statement hare

}

static  void show()    {

System.out.println(“one”)

}

}

Write a program interface?

interface   Test    {
public  abstract  void show ();

}

class   Test1  implements Test   {

public void show  ()   {

System.out.println(“one”)

}

public static void main(String   args[])   {

Test1  t1= new  Test1();

t1.show();

}

}.

Example:-

interface   Test    {
public  abstract  void show ();

}

interface   Test1    {
public  abstract  void show1 ();

}

 

class   Test3  implements Test1, Test2   {

public void show  ()   {

System.out.println(“one”)

}

public void show1  ()   {

System.out.println(“two”)

}

 

public static void main(String   args[])   {

Test2  t1= new  Test2();

t2.show();

t2.show1();

}

}

 

Abstract class

Interface class

1.        Class can have instances method that implements a default behavior. 

2.        Method of a java interface are implements abstracts and cannot have implementation.

3.        An abstract class may contain a not-final variable.

4.        Interface contains public, static, final, variables only

5.        Method & variable can have any. access modifier i.e public, static default, and private.

6.        Method variables are always public

7.       Java abstract class  should be extended using the “extends”  keyword

8.       Java interface should be implimentaited using. Implementation keyword

9.        An  abstract class  can extend another Java class and Implement multiple java interface

10.    In interface can extend another Java interface only.

 


Post a Comment

0 Comments