Showing posts with label what is abstract class in Java with examples. Show all posts
Showing posts with label what is abstract class in Java with examples. Show all posts

Wednesday, 16 January 2019

Abstract Class in Java - Online Help

Abstract Class in Java:

An abstract class is a class that is declared as abstract. It may or may not include abstract methods. Abstract classes cannot be instantiated, but they may be a subclass.


Abstract class in Java is used to declare functionality that is implemented by one or more subclasses. Abstract classes cannot be instantiated (if a class is declared as abstract, no object of that class can be created). To declare a class abstract, use the abstract keyword in front of the class keyword at the beginning of the class declaration.

The general format is:
abstract class classname
{
//abstract method
-----------------
-----------------
}

Here,
abstract and class are keywords.
classname is the name of the class.
abstract method is a method with no body.

Here is a simple example of a class with an abstract method:

abstract class student
{
abstract int numstudents();
}

In the above example, the class students declare one abstract method called numstudents (). Therefore, the class itself must also be declared abstract.

Abstract Methods:

An abstract method is a method that should be defined in the derived class. The method in an abstract class often has empty bodies. When the keyword abstract is prefixed before a method declaration, it becomes abstract.

To declare an abstract method as follows:
abstract returntype methodname (parameter list);

Note: The method in an abstract class often has empty bodies.


Sample Program:

abstract class students
{
abstract int numstudents ()
}
class bca extends students
{
int numstudents ()
{
return 38;
}
}
class bsc extends students
{
Int numstudents ()
{
return 42;
}
}
class finalstudents
{
public static void main (String args [])
{
System.out.println(new bca.numstudents());
System.out.println (new bsc.numstudents());
}
}

The output of sample example of abstract class in Java is:
38
42

Explanation of sample example of abstract class in Java is:

In the above example, the class students declare one abstract method named numstudents(). Therefore, the class itself must also be declared abstract. There are two concrete derived class named bca and bsc .Each of these provides a different implementation of the numstudents () method. The main () method instantiated each of these classes and invokes its numstudents () method.

       I hope you guys understand what is abstract class in Java in brief. If you guys have any queries or you guys have any suggestion how do I improve my blog please comment me below.


Reccommended post: