You are on page 1of 8

ABSTRACT CLASSES

WHAT IS AN ABSTRACT METHOD?


There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.
Syntax of abstract method

abstract

return-type

methodName(ParameterListrf)

Ex : public abstract void eat();

ABSTRACT EXAMPLE
abstract class AbstractMethods{ public void test ();// Compile error need to use abstract keyword

abstract int test (int a); abstract boolean test ();// can be overloaded abstract Object display(); }

FEATURES OF ABSTRACT METHODS:

1.abstract methods need to use abstract keyword 2.abstract methods can not have a method body 3.abstract methods should end with a semi colon ;

ABSTRACT CLASS

When a class is having at least one abstract method the class itself must be abstract. But if a class is abstract it does not require to have abstrac methods. Source Code

abstract class AbstractMethods{ abstract int test (int a); public Object display(){} }

ABSTRACT CLASS
Abstract classes cant make objects.
abstract class AbstractMethods{//Compile error public static void main(String args[]){ AbstractMethods a=new AbstractMethods();//Compile error } }

IMPLEMENTING ABSTRACT METHODS


Abstract Methods can be implement to the subclass of the abstract class. When implement the abstract method to the subclass , it will override to the concrete class without the abstract modifier.

abstract class Animal{ public void eat(); public void walk();//Must Implement } class Cat extends Animal{ public void eat(){ System.out.print(Eating fish and rice) } }

END

You might also like