Abstract class

4 0 0
                                    

Abstract class is like an interface but with an option to not create an abstract method. In interface there is an abstract method. It is a method without a body. The abstract keyword is implicitly defined in that method. So, like an interface, abstract class cannot be instantiated or shall we say, we cannot create an object but we can subclass this abstract class. Once it is subclass, you need to implement the abstract method of the abstract class or else you need to declare your subclass as abstract too.

However, the difference is abstract class can implement  fields and methods that are either instance or static, final or not, and any modifier from public to private. Unlike in interface where fields  are automatically public, static and final while its default methods are automatically public. Also, abstract class can only be extend once while interface can be implemented more than once.

===================

class AbstractDemo extends SlamDunk{
public static void main(String[] args) {
System.out.println("====abstract class=====");
AbstractDemo demo = new AbstractDemo();
System.out.println("Favourite player: " + demo.yourPlayer("Hananbishi Sakuragi"));
demo.whoIsTheCoach();
demo.goatPlayer("Michael Jordan");
}

@Override
String yourPlayer(String name) {
return name;
}

@Override
public void goatPlayer(String name) {
System.out.println("NBA: " + name);
}
}
abstract class SlamDunk implements NBA{
abstract String yourPlayer(String name);

protected void whoIsTheCoach(){
System.out.println("Coach Ansai DonotKnowTheSpelling");
}
}
interface NBA{
void goatPlayer(String name);
}

====================

Results:

====abstract class=====Favourite player: Hananbishi SakuragiCoach Ansai DonotKnowTheSpellingNBA: Michael Jordan


Note:

Possible scenario is an abstract class with an implementing interface. You are not required to implement whatever is in the interface since your class is abstract but you need to make sure that the subclass of your abstract class will implement the abstract method of your interface.

Java ProgrammingWhere stories live. Discover now