Tag Archives: Java abstraction

Java: Interface


An interface is the blueprint of a class to achieve total abstraction.

  • Abstract Class: Abstraction (0 to 100%)
  • Interface: Abstraction (100%)

Points to remember for Interface in Java

  • Interface in java use interface keyword to declare.
  • Interface in java allows only abstract methods i.e methods don’t have a body.
  • Interface in java allows static constants.
  • Interface in java supports multiple inheritance.
  • Interface in java not required to declare the method as public and abstract, internally all methods are public and abstract.
  • Interface in java having all fields as public, static and final.
  • Interface in java represents the IS-A relationship.
  • Interface in java can not be instantiated just like an abstract class.

Note:
There are some enhancements in the interface as per Java versions:

  • Java 8 introduced default and static methods in an interface.
  • Java 9, allowed to support private methods in an interface.

Why use Java interface?

There is the main reason to use interface in java:

  • Interface in java used to achieve abstraction.
  • Interface in java is used to support the multiple inheritance functionality.
  • Interface in java is used to achieve loose coupling.

How to declare an interface?

Interface in java is declared by using the interface keyword. All interface methods declared with the empty body are by default public and abstract. All declared fields inside the interface are public, static and final by default.

If a java class that implements an interface must implement all the methods declared in the interface.

Syntax:

interface interface_name{  
     // declare constant fields by default public , static and final  
    // declare methods that public and abstract by default   
} 

Note: If you are not adding any modifier in methods and variables in Java interface. The compiler adds as below while compilation:

  • Java compiler will add the public and abstract before the interface methods.
  • Java compiler will add public, static and final before fields inside java interface.

Java Classes and Interface Relationship

In java a class extends another class, an interface extends another interface, but a class implements an interface.

Interface Example

In the given example, the Shape interface has only one method as draw(). This method is implemented by Rectangle and Circle classes.

In a real scenario, an interface provides an abstraction to users i.e interface is defined by someone else, but its method implementation is provided by different providers. Moreover, it is used by someone else to interact with the method but the implementation part is hidden by the user.


interface Shape{
	void draw();
}
class Rectangle implements Shape{
public void draw(){System.out.println("Drawing Rectangle Shape");}
}
class Circle implements Shape{
public void draw(){System.out.println("Drawing Circle Shape");}
}  

class InterfaceExample1{
public static void main(String args[]){

Shape d=new Circle();
      d.draw();
	  d=new Rectabgle();
      d.draw();
}}

Output


Drawing Circle Shape
Drawing Rectangle Shape

Multiple inheritances in Java by the interface

Multiple inheritance in java class can be achieve by implements multiple interfaces, or an interface by extends multiple interfaces.

interface Printable{
void print();
}
interface Shape{
void draw();
}
class Rectangle implements Shape,Printable{
public void draw(){System.out.println("Drawing Rectangle Shape");}
public void print(){System.out.println("Print Rectangle Area");}
}
class Circle implements Shape,Printable{
public void draw(){System.out.println("Drawing Circle Shape");}
public void print(){System.out.println("Print Circle Area");}
}
class TestMultipleInterface{
public static void main(String args[]){
Shape d=new Circle();
d.draw();
d.print();
d=new Rectabgle();
d.draw();
d.print();
}
}

Output


Drawing Circle Shape
Print Circle Area
Drawing Rectangle Shape
Print Rectangle Area

Multiple inheritances are not supported through the class in java, but it is possible by an interface, why?

Multiple inheritances are not supported in the case of class because of ambiguity but supported by the interface because there is no ambiguity. It is because this method implementation is provided by the implementation class.

As shown in the below example,  Showable and Printable interface have the same display() methods but its implementation is provided by class MultipleInterfaceExmple, so there is no ambiguity.

interface Printable{
void display();
}
interface Showable{
void display();
}

class MultipleInterfaceExmple implements Printable, Showable{
public void dispaly(){
System.out.println("Print Test Multiple Inheritance in java");
}
public static void main(String args[]){
MultipleInterfaceExmple obj = new MultipleInterfaceExmple();
obj.print();
}
}

Output

Print Test Multiple Inheritance in java 

Interface inheritance

A class implements an interface, but one interface can extend another interface.

interface Printable{
void display();
}
interface Showable extends Printable{
void show();
}
class InterfaceInheritanceExmple implements Showable{
public void print(){System.out.println("Print Interface Inheritance in java");}
public void display(){System.out.println("Display Interface Inheritance in java");}

public static void main(String args[]){
InterfaceInheritanceExmple obj = new InterfaceInheritanceExmple();
obj.print();
obj.display();
}
}

Output

Print Interface Inheritance in java
Display Interface Inheritance in java

What is a marker or tagged interface?

An interface that has no methods is known as a marker or tagged interface, for example, Cloneable, Serializable, Remote, etc. Marker Interface in Java is used to provide information to the JVM so that JVM treats these objects specially.

Follow below link to get Detail knowledge of Marker interface with examples:

Marker Interface in Java and Uses

What is nested Interface?

An interface declared within another interface or class is called a nested interface.

Follow below link to get detail knowledge of nested interface with examples:

Java: Nested Interface

 

Java: OOPS Concepts


The object is Real-world entities i.e Pen, Paper, computer, watch, fan, etc.

Object-Oriented Programming is a paradigm or methodology to implement real-world entities by design a program using classes and objects.

OOPS(Object-Oriented programming System) provides a paradigm to simplifies the development and maintenance of a Software. Here are the main concepts of OOPS:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Apart from these basic concepts, there are some other terminologies used to show association and dependency between the objects: and classes:

  • Coupling
  • Cohesion
  • Association
  • Aggregation
  • Composition
  • Generalization(Super Class)
  • Specialization (Sub Class)
  • Realization(Interface)

See Also:

  • Java: Class & Instance Relationship
  • Java: OOPS Design Principles
  • Java: SOLID Design principles

simula and Smalltalk are considered as first truly object-oriented programming language But the main popular object-oriented languages are Java, C#, PHP, Python, C++, etc.

OOPS Concept

Object

A physical or logical entity that has state and behavior are known as an object. For Example Pen, Paper, computer, watch fan, etc.

An Object is an instance of a class. It contains an address and takes some space in heap memory. Communication between objects happens without knowing the detail of each other only condition is the type of message accepted and the type of response returned should be compatible.

For Example, A car is an object because it has states like color, make, model, engine, etc. as well as behaviors like speed, accelerator, music, etc.

Class

The collection of objects is called a class. Class is the blueprint of an object from which object created. Class doesn’t consume space.

For Example, Person is class and “Saurabh Gupta” is the object of that class.

Inheritance

Inheritance is a process where child class acquired all the properties and behaviors of the parent class. Inheritance is used when one object is based on another object. Here parent class also called a superclass and child class called as a subclass.

For Example,  Person is Parent class and Employee is a subclass of Person. which acquired all the properties and behavior of Person class.

Advantage of inheritance

  • Code Reusability
  • Runtime Polymorphism

Points about Inheritance

  • extends the keyword used to implement inheritance.
  • Java doesn’t support multiple inheritances. It’s possible by implementing multiple interfaces.
  • Inheritance has an “IS-A” relationship.

Polymorphism

Polymorphism (more-form) means the ability to take more than one form. It’s used when one task needs to perform in different ways.

For Example, areas of shape are having different formulae for each same like rectangle, square, circle, etc.

Polymorphism can be achieved in two ways:

  1. Runtime Polymorphism (Method Overriding)
  2. Compile-time Polymorphism (Method Overloading)

Abstraction

Abstraction is a concept to show only essential detail i.e hide internal detail and show functionality in simple terms. To achieve abstraction in Java, use abstract class or interface.

Example: In ATM we don’t know how internally work. In Car don’t know about the internal mechanism.

Abstraction can be achieved by two ways:

  • Abstract Class (0% to 100%)
  • Interface (100%)

Encapsulation

Binding (or wrapping) of data and function in a single unit is called encapsulation. The encapsulation technique is used to achieve abstraction in oops.

In Java, A class is an example of encapsulation where abstraction level depends on access modifiers(private, protected and public) uses for fields and methods. A class is considered as fully encapsulated if all data members are private.