I am learning the concepts of object-oriented programming. One of them is abstraction. I understand that any class containing an abstract method should be abstract as well and that an abstract class cannot be instantiated. To use an abstract class I have to inherit it from another.
So far so good. Let's take the following code:
public abstract class Games {
public abstract void start();
public void stop(){
System.out.println("Stopping game in abstract class");
}
}
class GameA extends Games{
public void start(){
System.out.println("Starting Game A");
}
}
class GameB extends Games{
public void start(){
System.out.println("Starting Game B");
}
}
And then we have a class with a main
method:
public class AbstractExample {
public static void main(String[] args){
Games A = new GameA();
Games B = new GameB();
A.start();
A.stop();
B.start();
B.stop();
}
}
But I could have written the following in class Games
:
public void start(){
System.out.print("");
}
Then it wouldn't have to be abstract
, the output would be the same and I would even be able to instantiate the Games
class. So what is the key of making abstract methods and classes?