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();

Now extend another class and implement the method:

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

THE END!!!

Java – Abstract classes & methods

Read More

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s