Java – Abstract classes & methods

Abstract class is a class where its implementation is incomplete or non-existent. Abstract class cannot be instantiated but can be extended. A subclass that implements an abstract class can be instantiated.

Abstract class is similar to interface but also have some differences. Both cannot be instantiated and both can contain methods without implementation. The differences are:

  • a class can extend only one class but can implement many interfaces separating them with “,”
  • An abstract class can define fields that are not static nor final. An interface all fields must be public, static and final
  • An abstract class can define public, protected and private methods but an interface must have not implemented and public methods.

syntax:

public abstract class ClassName{
  --- fields and methods  ---
}

An abstract method is a method declared without implementation, no body. An abstract class may or may not have abstract methods. Sub classes must override and implement the abstract methods.

syntax:

public abstract returnType methodName();
Abstract classes and methods, <span>Java – Abstract classes & methods</span>, KandZ

Now extend another class and implement the method:

Abstract classes and methods, <span>Java – Abstract classes & methods</span>, KandZ

If you use IntelliJ press Alt + Enter , select Implement methods and click OK

Abstract classes and methods, <span>Java – Abstract classes & methods</span>, KandZ
Abstract classes and methods, <span>Java – Abstract classes & methods</span>, KandZ

THE END!!!

89210cookie-checkJava – Abstract classes & methodsJava – Abstract classes & methods

Related posts

One Thought to “Java – Abstract classes & methods

Comments are closed.