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
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.
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.
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.
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.
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.
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With