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?
Gamesa concrete class, then by all means go ahead and implement that method. Abstraction is for classes that it wouldn't make sense to have instances of.start()method as you propose make any sense, if not, that is why you can mark it abstract: when it makes no sense to provide a dummy implementation, and to make absolutely clear that sub-classes must implement it.