Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class or an interface : JAVA? [closed]

Tags:

java

I have one common method which i need to use in several classes with just a single call from the calling class. So what i see is i can call it in two ways.

public abstract class TestAbstractClass {
    void commonMethod(){
        System.out.println("Calling common method : TestAbstractClass");
    }
}

calling class:

public class RunApplication extends TestAbstractClass{

    public void testMethod(){
        commonMethod();
    }
}

[OR]

Using Java 8 feature of default method in interface.

public interface TestInterface {
    default void commonMethod(){
        System.out.println("Calling common method : TestInterface");
    }
}

calling class:

public class RunApplication implements TestInterface{

    public void testMethod(){
        commonMethod();
    }
}

They both works fine for me, But what is better approach, an abstract class with non-abstract method OR Interface with default method.

like image 213
Sarang Avatar asked Sep 25 '22 03:09

Sarang


People also ask

Which is better abstract class or interface in Java?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

Is Java interface an abstract class?

However, the interface offers full abstraction in Java, something that abstract classes cannot do. Like a class, an interface can contain methods and variables, though the declared methods default to abstract. Interfaces cannot be instantiated, though abstract classes that implement interfaces can.

Which is better abstraction or interface?

Multiple Inheritance: Interface supports multiple inheritance; an abstraction does not support multiple inheritance. Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

What is diff between interface and abstract class?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.


1 Answers

Abstract class or an interface

If I were you I will judge the appropriateness by checking whether all those classes are related or not.

Generally, I will use interface if the classes which implements it have little or not relation at all and they just happened to share certain behaviour which can be exposed by the method from the interface.

I will use abstraction when the classes are a subset of the other.

Example:

Using bird and plane as an example. Birds and planes both flies. But other than that, they isn't related at all. You will naturally want to implement an interface instead of using abstract class because neither planes are birds and neither birds are planes.

class Bird implements Flyable
{
    @Override
    public void fly(){

    }
}

class Plane implements Flyable
{
    @Override
    public void fly(){

    }
}

Another issue to consider is multiple inheritance. In Java, multiple inheritance of a class is not allowed. Hence if you use abstraction, your sub-class won't be able to extends to another class.

If your class implements an interface, it can always implements another interface or extends to one other class.

like image 64
user3437460 Avatar answered Oct 11 '22 12:10

user3437460