Friday, December 2, 2016

When to use abstract class and interface in Java

In  this post,we will understand and learn when to use abstract class and interface. This is going to the one of the important tricky interview question in any of the core java interview questions. In this page i am going to cover what is abstract and  interface where do we use in real time with examples.Now let us start with definitions and use of them.

abstract class:

An abstract class is a class which contains at least one abstract method that means a abstract method is a method which doesn't have the any implementation it just have the method signature. In order to say a class is abstract then we need to use a keyword  "abstract".

Use of the abstract class:

By using the abstract class we can achieve the abstraction which is one of the OOPS concept. Here abstraction means which shows functionality by hiding the implementation details is called as abstraction.

When to use abstract class:

If you have any requirement where few set of the functionalities common across all the sub classes and few functionalities vary from sub classes then the common functionality you can define in the abstract class. But an abstract class can not instantiated. It can be only be used as a super class for other classes that extend the abstract class.
Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived class should be implemented.  So, abstract class is a good choice if you have feature expansion.

If you know partial implementation but not completely then abstract class is good choice.
In addition to abstract method we can declare concrete method also in abstract class. For abstract methods there is no restriction to declare any modifiers like private,protected,final etc..





Example:
abstract class Vehicle
{
public abstract int getNoOfWheels();
}
class Bus extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of Bus wheels:"+7);
return 7;
}
}
class Car extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of car wheels:"+4);
return 4;
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
b.getNoOfWheels();
Car c=new Car();
c.getNoOfWheels();
}
}

Output:








interface:
interface is a 100% abstract method. That means it doesn't any implementation. It contains only method declaration. What ever methods declared in interface should be implemented by classes that implement the interface. Remember that a class can implement more than one interface but can extend only one class. Interfaces also can't be create objects like abstract class.


When to use interface:

In java, multiple inheritance problem is solved with a powerful construct called interfaces. interface contains only abstract methods. That is means interface contains only abstract methods there is no concrete methods in interface. interface just specify method declaration but no body of method.

If you use interface you would need to implement all the methods in the class that extends the interface. If you would like to support for future expansion when using interfaces, you will need to extend the interfaces and create new one.

You can use interface if you want a contract on some functionality. You can also use interface to restrict access members of particular type,because all member variables private static and final in interface whether you are declaring or not.

Interfaces are a good choice when you think that the API will not change for a while. Interfaces are also good  when you want to have something similar to multiple inheritance,since you can implement multiple inheritance using interface. 

 Example:

interface one
{
int i=20;
}
class Two implements one
{
public static void main(String args[])
{
int i=30;
System.out.println(i);
}
}

Output:
30

Naming conflict in interfaces:

if two interfaces contains a method with same signature and same return type in the implementation class then only one method implementation is enough.

Example:

interface one
{
void m1();
}
interface two
{
void m1();
}
class Demo implements one, two
{
public void m1()
{
System.out.println("hello java");
}

}

Output:
hello java


Recommended to Read:  Java interface with example


I hope you enjoy this page and comment below with your answers about this topic and share this post through social websites.

1 comment:

  1. An abstract class is a class which contains atleast one abstract method.

    Super site..

    ReplyDelete

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...