Abstract class
A method that is declared as abstract and does not have implementation is known as abstract method.
Syntax to define the abstract method
abstract return_type <method_name>();//no braces{}
Example:
public abstract class Shape(){
int area;
public abstract void area();
}
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class Rectangle {
int length,breadth;
abstract void draw();
}
Abstract Class & Method Important Points:
- A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
- An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed(inherited).
- Syntax to declare the abstract class:
- When an abstract class is subclassed(inherited), the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
- An abstract class can have data member, abstract method, method body, constructor and even main() method.
- If there is any abstract method in a class, that class must be abstract.
- If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract.
A method that is declared as abstract and does not have implementation is known as abstract method.
Syntax to define the abstract method
abstract return_type <method_name>();//no braces{}
Example:
public abstract class Shape(){
int area;
public abstract void area();
}
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class Rectangle {
int length,breadth;
abstract void draw();
}
Abstract Class & Method Important Points:
- An abstract class may also have concrete (complete) methods.
- For design purpose, a class can be declared abstract even if it does not contain any abstract methods.
- Reference of an abstract class can point to objects of its sub-classes thereby achieving run-time polymorphism Ex: Shape obj = new Rectangle();
- A class must be compulsorily labelled abstract , if it has one or more abstract methods.
No comments:
Post a Comment