Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "abstract like" method with body

there are the following classes:

public abstract class Super
{
    public abstract void run();
}

public class Sub1 extends Super
{
    @Override
    public void run ()
    {
        System.out.println("Method called");
        System.out.println("Sub1 called");
    }
}

public class Sub2 extends Super
{
    @Override
    public void run ()
    {
        System.out.println("Method called");
        System.out.println("Sub2 called");
    }
}

how can I avoid that I have to write the "System.out.println("Method called");" two times?

Thank you for answers

CalibeR.50

like image 853
CalibeR.50 Avatar asked Jul 06 '13 19:07

CalibeR.50


People also ask

Why abstract method has no body?

An abstract method has no body. (It has no statements.) It declares an access modifier, return type, and method signature followed by a semicolon. A non-abstract child class inherits the abstract method and must define a non-abstract method that matches the abstract method.

Can abstract method have body Java?

Abstract methods cannot have body. Abstract class can have static fields and static method, like other classes. An abstract class cannot be declared as final. Only abstract class can have abstract methods.

Can abstract method have object?

We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class.

Can we write body in abstract method?

We can use abstract keyword to create an abstract method, an abstract method doesn't have body. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.


2 Answers

Bring the common functionality to the superclass, and define another abstract method, for which the subclasses will define their own implementation.

public abstract class Super {
    public void run() {
        System.out.println("Method called");
        printMessage();
    }
    abstract void printMessage ();
}

public class Sub1 extends Super {
    @Override
    void printMessage() {
        System.out.println("Sub1 called");
    }
}

public class Sub2 extends Super {
    @Override
    void printMessage () {
        System.out.println("Sub2 called");
    }
}

This way you can avoid the duplication of calling the common method of the superclass twice.

like image 122
user000001 Avatar answered Sep 20 '22 16:09

user000001


You can put run() implementation into the abstract class:

// Super is still an abstract class
public abstract class Super
{
    // While method run is not an abstract method:
    public void run() 
    {
        System.out.println("Method called");
    }
}

public class Sub1 extends Super
{
  // There's no need of declaring run() here unless you want to change its behaviour
}

public class Sub2 extends Super
{
}

In the edited version of your question, you can just use an inherited run realization

// Super is still abstract
public abstract class Super
{
    // But method run is not abstract
    public void run() 
    {
        System.out.println("Method called");
    }
}

public class Sub1 extends Super
{
  @Override
  public void run()
  {
      super.run(); // <- call Super.run() that prints "Method called"
      System.out.println("Sub1 called");
  }
}

public class Sub2 extends Super
{
  @Override
  public void run()
  {
      super.run();
      System.out.println("Sub2 called");
  }
}
like image 30
Dmitry Bychenko Avatar answered Sep 21 '22 16:09

Dmitry Bychenko