Abstract class and method in Java.

Abstract Class:
          An abstract class is something which is incomplete and you can not create an instance of the abstract class. If you want to use it you need to make it complete or concrete by extending it. It is created using abstract keyword. This class can contain abstract and non abstract methods. Before learning about abstract class and abstract I recommend you to first learn about abstraction concept in Java.

Few important things to remember about abstract class:

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.

Example of Abstract class with abstract method:

abstract class Animal{  
  abstract void run();  
}  

class Horse extends Animal{  
void run()
{
    System.out.println("Runs fast");
}  

public static void main(String args[]){  
Animal obj = new Horse();  
 obj.run();  
}


Abstract Method:

            A method can also be declared as abstract, it does not have any implementation details.

Example: 

abstract void demoAbstract();//empty body, use abstract keyword


*Above program is example of abstract class with abstract method.

Note: By the way, Java has concept of abstract classes and abstract methods but a variable in Java can not be abstract.

Comments

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.