Interface in Java

Dipanjan sahoo
3 min readSep 19, 2021
Interface in Java tech with gyan.online

An interface in java is a blueprint of a class. It has static constants and abstract methods.The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritances in Java.

Java Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

go main website to get Fresh Article

Internal addition by compiler

Understanding relationship between classes and interfaces

//Interface declaration: by first user

interface Drawable{

void draw();

}

//Implementation: by second user

class Rectangle implements Drawable{

public void draw(){System.out.println(“drawing rectangle”);}

}

class Circle implements Drawable{

public void draw(){System.out.println(“drawing circle”);}

}

//Using interface: by third user

class TestInterface1{

public static void main(String args[]){

Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable() d.draw();

}}

Output:

drawing circle

Multiple inheritance in Java by interface

interface Printable{

void print();

}

interface Showable{

void show();

}

class A7 implements Printable,Showable{

public void print(){System.out.println(“Hello”);} public void show(){System.out.println(“Welcome”);} public static void main(String args[]){

A7 obj = new A7(); obj.print();

obj.show();

} }

Output:

Hello

Welcome

Difference between Abstract Class and Interface;

……..Click here to get difference .

Java Inner Classes:

Java inner class or nested class is a class which is declared inside the class or interface.

We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.

Syntax of Inner class

1. class Java_Outer_class{

2. //code

3. class Java_Inner_class{

4. //code

5. } }

Read fresh Articile

Advantage of java inner classes

There are basically three advantages of inner classes in java. They are as follows:

Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.

  1. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
  2. Code Optimization: It requires less code to write.

Difference between nested class and inner class in Java:

Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes

There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.

  • Non-static nested class (inner class)
  1. Member inner class
  2. Anonymous inner class
  3. Local inner class
  • Static nested class

Rede More>>

please visit our main website to get Fresh Article click here (https://techwithgyan.online)

--

--

Dipanjan sahoo
0 Followers

hey i am dipanjan sahoo, i am a professional digital marketer,