interface – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:28:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg interface – Programmerbay https://programmerbay.com 32 32 Difference Between Interface and Abstract Class in Java With Comparison Table https://programmerbay.com/difference-between-an-interface-and-abstract-class-in-java/ https://programmerbay.com/difference-between-an-interface-and-abstract-class-in-java/#respond Mon, 14 Sep 2020 18:07:10 +0000 https://programmerbay.com/?p=5161 An interface and abstract class are used for achieve abstraction feature in Java. Abstraction is one of the OOP’s feature that aims on hiding background details and shows only the essential details.

The basic difference between abstract class and interface is that an abstract class can have abstract and non-abstract methods, whereas, an interface supports only abstract methods.

Differences between abstract class and Interface in Java With Comparison Table

Abstract ClassInterface
It supports abstract methods, static methods, final methods, and concrete methods. In other words, it can have both abstract and non-abstract methodsIt supports abstract methods only. Java 8 onwards, it can have Default method and static method too.
It doesn't support multiple inheritanceIt supports multiple inheritance. An interface can extend two or more interfaces at a time
An abstract class can extend single class only, but can implement one or more interfacesAn interface can extend multiple interfaces only
It can have constructorsInterfaces don't
'abstract' keyword is used to make a class abstract'interface' keyword is used to declare an interface
It allows static, non-final, final and non-static variablesIt allows static and final variables
An abstract can have all types of members that involves private ,public, and protectedAn interface can have public members only

More Differences :-

  1. Static methods of an interface are accessed by interface name [interfaceName.StaticMethod()], while in case of abstract classes, static methods are simply accessed by class name [className.StaticMethod() , Subclassed name, not abstract Class name ].

Interface

An interface may consist of constants, default methods, abstract methods (without body )and static methods. It explains what a class should do and look like, but not how. It can be extended by other interfaces using ‘ extends’ and implemented by other classes using ‘implements’ keyword. In Java 8, a Default Method concept is added which allows implementation in that very method.

Java program to demonstrate the working of Interface

Program:

interface ShowingInterface{
int X= 20; // Constant as by default compiler would read it like this static final int x = 20
default void print(){
System.out.println("I am in default method, printing constant = "+X);
}

static void staticMethod(){ // Static method 
System.out.println("I am in Static method");

}
void display (); // abstract method by default compiler would read it like this abstract void display ();
}
public class Interfaces implements ShowingInterface {
@Override
public void display(){
System.out.println("I am in abstract method");
}
public static void main(String[] args) {
Interfaces obj = new Interfaces();
obj.display(); // calling Abstract method
obj.print(); // calling Default method
ShowingInterface.staticMethod(); // calling static method
}

}

 

Output:

Interface example

 

Abstract Class

It cannot be instantiated and is defined with abstract keyword. Extending classes must provide definition for all the abstract methods, otherwise, it also need to be declared as abstract.
It can have abstract methods, non-abstract methods and static methods.

Java program to demonstrate the working of abstract classes

abstract class showingAbstract{
// variable
int variable;
// constructor
showingAbstract(){
variable=10;
System.out.println("I am in constructor of abstract class right now ");
}
// abstract method
abstract void print();
// static method
static void display(){
System.out.println("I am in static method");
}
// concrete method
void showVariable(){
System.out.println("I am concrete method, Value of variable = "+variable);
}

}
public class AbstractClass extends showingAbstract {

@Override
void print(){
System.out.println("Abstract method is executing ");
}

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

}

 

Output:

abstract class

Key differences

  1. An Abstract class can have abstract methods as well as the non-abstract methods; whereas; an interface can have only abstract methods.
  2.  The concept of Multiple inheritance is not supported by the abstract classes because it causes ambiguity; whereas; for the interfaces, they allow multiple inheritance concept as there is no case ambiguity in their implementation.
  3. The types of variables that are supported by the abstract class are as follows:
  • Static variable
  • Non – static variable
  • Final variable
  • Non – final variable

The types of variables that are supported by the interfaces are as follows:

  • Static variable
  • Final variable
  1. The interesting fact about abstract classes is that the abstract class supports the implementation of the interfaces inside it; whereas; but the vice-versa does not happen, that is, the interfaces do not support the implementation of the abstract classes.
  2. In the case of Abstract class, it can be declared using the “abstract” keyword; whereas; in the case of Interface, if we want to declare an interface then you must be using the “interface” keyword.
  3. When we talk about abstract class, then to extend the abstract class functionality we must use the “extends” keyword; whereas; for the interfaces, if we want to implement the interface we must use the “implements”.

]]>
https://programmerbay.com/difference-between-an-interface-and-abstract-class-in-java/feed/ 0
Static methods & Default Methods in Java 8 https://programmerbay.com/static-methods-default-methods-in-java-8/ https://programmerbay.com/static-methods-default-methods-in-java-8/#respond Sun, 14 Jun 2020 15:50:12 +0000 https://programmerbay.com/?p=6433 An interface describes what a class should look like but not describe how, closely related to class but can’t be instantiated. Before Java 8. Java interface can have abstract method (without method definition) only, but Java 8 onwards, two new features were introduced namely Default method and Static method within interface.

Default Method

-A default method allows to add a new methods in existing interface without affecting the implementing classes.

-It uses default modifier

– It must have a complete method definition. Basically, a dummy body.
– It is not compulsory for implementing class to override a default method but one can.

Reason to introduce it

Prior to Java 8, a class that implements an interface mandatorily had to provide implementation to abstract methods of that particular interface.

However, it was not a big deal, but Java developers noticed that if there is an existing interface which has implemented by quite a large number of classes and there a need arises of adding another method. Then it would need to be overridden by other classes. This would break the code if any of the class omit to implement it.

Therefore, default method came in to picture, now one can add any new feature to existing interface without affecting behaviour of other implementing classes.

Problem:

Before adding new Feature:

No error

After adding new Feature:

error 3

 

Solution

For example:

interface MyInterface {
    default void display() {
        System.out.println("This is dummy definition");
    }
}

public class DefaualtMethodExample implements MyInterface {

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

Output:

This is dummy definition

Static method

– Java 8 also supports static method within an interface.

-Method body should be provided to the static method at the time of its declaration.

-It can’t be overridden in implementing classes

-It uses static modifier

– By default, static methods cannot be available in implementing classes. Therefore one need to call it by InterfaceName.methodName()

 

For example:

interface MyInterface {
    static void display() {
        System.out.println("This is static definition");
    }
}

public class DefaualtMethodExample implements MyInterface {

    public static void main(String[] args) {
        MyInterface.display();
    }
}

Output:

This is static definition

 

]]>
https://programmerbay.com/static-methods-default-methods-in-java-8/feed/ 0
What is Java Interface with examples https://programmerbay.com/what-is-java-interface-with-examples/ https://programmerbay.com/what-is-java-interface-with-examples/#respond Sat, 14 Sep 2019 17:28:58 +0000 https://programmerbay.com/?p=5112 An interface may consist of constants, default methods, abstract methods (without body )and static methods. It explains what a class should do and look like, but not how. It can be extended by other interfaces using ‘ extends’ and implemented by other classes using ‘implements’ keyword.
It cannot be instantiated, further, the class that implements it must provide the body for the implemented abstract method, otherwise, compiler triggers an error. An interface is followed by ‘interface’ keyword.

  • Java doesn’t provide support for multiple inheritance, but it is possible between Interfaces.
  • An interface can have an object, but it can’t be instantiated.
  • It doesn’t allow constructors.
  • It can be extended by other interfaces using ‘extends’ keyword.
  • It can be implemented by other classes using ‘implements’ keyword.

If no modifier is mentioned, then the interface would only be accessed within a package.

Interface having method signatures

In an interface, you can define an abstract method, and that method must be overridden by the class that implements it. In other words, A class which implements an interface must define the body of that abstract method.

Program:

interface Quadilateral{
void area(); // abstract method
}
class rectangle implements Quadilateral{
int length;
int breadth;
@Override
public void area(){
System.out.println("Area of rectangle = "+(length*breadth)+" cm sq.");
}

}
class square{}
public class InterfaceExample {

public static void main(String[] args) {
rectangle obj =new rectangle();
obj.length =10;
obj.breadth=5;
obj.area();
}

}

Output:

interace abstract
void area();

By default, a compiler treats a method within an interface like this:

// abstract returnType methodName();
abstract void area();

Interface having constant

Interface also allow us to declare constants whose declaration and initialization takes place at a single statement.

Program:

interface Pie{
// Compiler would see this statement as public static final double =3.14;
double pie =3.14; // Constant
}
class Showing implements Pie{
void print(){
System.out.println(" I am Printing constant value "+pie);
}
}
public class InterfaceConstant {
public static void main(String[] args) {
Showing obj =new Showing();
obj.print();
}

}

Output:

constant
double pie =3.14;

By default, a compiler treats a variable as constant within an interface like this:

//public static final constantName = value;
public static final pie= 3.14; 

Interface having a static method

A static method doesn’t depend upon the instance of a class. Therefore, it executes independently.

Program:

interface xyz{
static void print(){
System.out.println("Executing static method");
}
}
class abc implements xyz{

}
public class InterfaceStatic implements xyz{

public static void main(String[] args) {
xyz.print();

}

}

Output:

static method

]]>
https://programmerbay.com/what-is-java-interface-with-examples/feed/ 0