Abstract class in java

By | January 4, 2019
Spread the love

In this toturial I will introduce you to the abstract class in java.

What is Abstract Class in Java?

Abstract class is almost similar to the interface but the implementation of method is different.

Abstract class in Java
Abstract class in Java

How to Create Abstract Class?

Abstract keyword is used to create abstract class. An Abstract Class cannot have an object.An abstract class is incomplete without a subclass.

When we create Abstract class in Java then we have to write abstract method in that class.

In abstract class we create method with abstract keyword with out it’s body.

In general other class we write public keyword,class keyword ,class name and then we write it’s body. But here we write a public keyword, then a class keyword, then abstract keyword, class name and then in parenthesis we write the rest of the body of our abstract class.

The abstract class body must contain abstract class with out it’s body.

Syntax to Create Abstract Class in Java:

Following are the syntax to create an Abstract class and Abstract method :

In order to create abstract class you must write the abstract class according to the following procedure.

public abstract class ClassName{

instance variables;

public abstract methodName( );

}

for example to Create method in abstract class we write:

public abstract void add ( );

Example of Abstract Class:

Now I am going to you example that how to create abstract class step by step.

So open your IDE like NetBeans,Intellij Idea or any other you want. I am using Net beans.

I have created two classes one is Super class which is Animal class.

see in pic below.

Another class which I have created is Cat class which is inherited from super Animal Class.

See in pic below:

abstract class in java
Example of Abstract Class method body

Example of an abstract class body:

As I mension above that I have created two classes one is super and another is child class.

But I mean two create two classes here is two understand the abstract consept through example.

As you see in above example that When I created a class Animal with abstract keyword then you will have generated idea in your mind that whenever you create abstract class you have to write abstract keyword before it.

Another idea is that as I have given in above picture that when we create methods in abstract class we do not it’s body.

When we want to create abstract method body then we have to create in child classes just like I have just declare the setName( ) method in super class which is Animal class and the body is declare in child class which is animal class.

Important point to remember for creating Abstract class:

Following are the important points to remember for creating abstract class and methods in Java.

  • Abstract class cannot have it’s an object.
  • A subclass is necessary for the abstract class. I mean to say that inheritance is necessary for abstract classes.
  • The abstract methods of the abstract class must be implemented in all the classes.
  • It’s necessary to create an abstract method for the abstract class.
  • It’s not necessary for all abstract classes to create abstract methods for it.
  • You cannot create abstract methods in other classes which is not abstract classes.
  • Methods having abstract keywords can only be created within abstract classes.

I hope you understand about about abstract class.Share with your friends.If you have any problem regarding this just comment below or you can contact me through my facebook page.

Watch this picture for demonstration purpose:

abstract class in java
abstract class in java

  •  
    76
    Shares
  • 76
  •  
  •  
  •  
  •  
  •  
  •  
  •   
  •   
  •   
  •   
  •  

One thought on “Abstract class in java

  1. Pingback: Object-Oriented Programming in JAVA | Free Courses Hub

Leave a Reply

Your email address will not be published. Required fields are marked *