0

I want to know the usage of abstract methods in Java. I understand the concept of abstract methods in Java. but i'm still confused in what scenarios I should use it? I understand that if we define our methods as abstract the class needs to be abstract too .

But could anybody tell me that what the real usage of abstract method is? In which platform (EG Website , Application ) Where we can use it ? Could anybody give me a real life example of the usage of abstract?

tijn167
  • 547
  • 2
  • 21
  • 1
    the use of 'abstract' is to make something 'abstract'. To declare something 'not concrete enough' to handle. – Stultuske Feb 08 '19 at 12:00
  • 2
    Possible duplicate of [Abstract class in Java](https://stackoverflow.com/questions/1320745/abstract-class-in-java) – David Feb 08 '19 at 12:01

2 Answers2

1

The big difference between interfaces and abstract classes is that abstract classes can have a body. While interfaces are just a contract or a definition, an abstract class can have things like common functionality. Like the toString method below. Since java 8 interfaces can also have implementations the following quote is a good one for abstract classes:

JDK 8 brings arguably the abstract class's greatest advantage over the interface to the interface. The implication of this is that a large number of abstract classes used today can likely be replaced and a large number of future work that would have been abstract classes will now instead be interfaces with default methods.

Here is a real life example:

//abstract class
public abstract class Person {

    private String name;
    private String gender;

    public Person(String nm, String gen){
        this.name=nm;
        this.gender=gen;
    }

    //abstract method
    public abstract void work();

    @Override
    public String toString(){
        return "Name="+this.name+"::Gender="+this.gender;
    }

    public void changeName(String newName) {
        this.name = newName;
    }   
}



public class Employee extends Person {

    private int empId;

    public Employee(String nm, String gen, int id) {
        super(nm, gen);
        this.empId=id;
    }

    @Override
    public void work() {
        if(empId == 0){
            System.out.println("Not working");
        }else{
            System.out.println("Working as employee!!");
        }
    }

    public static void main(String args[]){
        //coding in terms of abstract classes
        Person student = new Employee("Dove","Female",0);
        Person employee = new Employee("Pankaj","Male",123);
        student.work();
        employee.work();
        //using method implemented in abstract class - inheritance
        employee.changeName("Pankaj Kumar");
        System.out.println(employee.toString());
    }

}
tijn167
  • 547
  • 2
  • 21
  • your explanation became out of date with Java 8, which allows default methods (with implementation) in interfaces – Stultuske Feb 08 '19 at 12:18
  • 1
    Thanks for letting me know, but the idea of abstract classes is still the same – tijn167 Feb 08 '19 at 12:19
  • in that case, you might want to put in your answer what you consider the idea of abstract classes, since what you mention in your answer goes for both abstract classes and interfaces – Stultuske Feb 08 '19 at 12:22
  • Edited it with a nice quote about abstracts and interfaces – tijn167 Feb 08 '19 at 12:25
  • I want to know in which platform we can use the java ? such as ecommerce , web application , game development ? i want to know where we can use this abstract ? – amazing coder Feb 09 '19 at 07:09
  • If you want to know the usages for Java you should google, it’s too much te describe here. And for abstract classes i would want link you here for use cases: https://stackoverflow.com/questions/3344816/when-and-why-to-use-abstract-classes-methods – tijn167 Feb 09 '19 at 08:17
0

I have for my database entities an AbstractEntity

public abstract class AbstractEntity {

    private Date dateOfCreation;    
    private Date dateOfLastModification = new Date();    
    private Long id;
// getters/setters ommitted
}

Every entity in my database should have those values and I don't want to write them in each entity. So instead of writing:

public class MyUser {
    private Date dateOfCreation;   
    private Date dateOfLastModification = new Date();    
    private Long id;

    private String name;
    private Date dateOfBirthDate;
}

I just write:

public class MyUser extends AbstractEntity {

    private String name;
    private Date dateOfBirthDate;
}

The three fields of the abstract class are here also available. The AbstractEntity itself has no use on its own. It would make no sense to create an instance of it, that's the reason it is abstract. If you are having just one or two entities there might be no huge benefit. But if you are considering that you are usually having more entities and that you can also write common methods (e.g. getDaySinceLastModification) into the abstract entity which is accessible for every entity, you should see the advantage.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • "The AbstractEntity itself has no use on its own". I assume the OP's confusion is coming from the fact that an abstract class can contain (only) concrete implemented methods, without any abstract ones. Your explanation is valid for your situation, but it might not be for his – Stultuske Feb 08 '19 at 12:16
  • Joachim Rohde So it's not neccessary to put abstract keyword in private variable ?? – amazing coder Feb 09 '19 at 07:11