In Java, the following some important observations about abstract classes are as follows:
- An instance of an abstract class can not be created.
- Constructors are allowed.
- We can have an abstract class without any abstract method.
- There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final or in simpler terms final method can not be abstract itself as it will yield an error: “Illegal combination of modifiers: abstract and final”
- We can define static methods in an abstract class
- We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract
- If a class contains at least one abstract method then compulsory should declare a class as abstract
- If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method
// Java Program to implement Abstract Class having constructor, data member, and methods
import java.io.*;
abstract class Subject {
Subject() {
System.out.println("Learning Subject");
}
abstract void syllabus();
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus(){
System.out.println("C , Java , C++");
}
}
class absdemo {
public static void main(String[] args) {
Subject x=new IT();
x.syllabus();
x.Learn();
}
}
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి