Abstract Class and Method

Abstract class and method:
  1. A class containing any abstract methods is an abstract class.
  2. Abstract method can not have a body
  3. Abstract method modifier either public, protected or default. (private not allowed here).
  4. In abstract class have general method must with body part (means with definition part).
  5. An abstract class extends another abstract class. And also abstract method defines in any extended class.
  6. When we access abstract method in any another class with the help of extends keyword then it does not mandatory in only public where as in interface implementation it must be public.
  7.  Variables of abstract class can be assigning in inherited class. While in interface can not assign because it treated as final variable.
The following rules apply to the use of the abstract keyword:
  • Constructors cannot be marked as abstract.
  • Static methods cannot be abstract.
  • Private methods cannot be abstract.
  • Abstract methods must be defined in some subclass of the class in which they are declared.
  • A method that overrides a superclass method cannot be abstract.
  • Classes that contains abstract methods and classes that inherit abstract methods without overriding them are considered abstract classes.
  • It is a compile-time error to instantiate an abstract class or attempt to call an abstract method directly.
If Method is abstract, then class must be abstract
class A { int x=90; void methodA()// must have a body {} abstract void methodAbstract(); }
Output

C:\JavaProgram\Abstract>javac A.java
A.java:1: A is not abstract and does not override abstract method methodAbstract
() in A
class A
^
1 error

Solution
abstract class A { int x=90; void methodA() {} abstract void methodAbstract(); }
Abstract Method must have only declaration

Abstract method must have only method declaration not definition part .

Problem:
abstract class A { int x=90; abstract void methodAbstract() // can not have a body { } }
Output

C:\JavaProgram\Abstract>javac A.java
A.java:4: abstract methods cannot have a body
abstract void methodAbstract() // can not have a body
^
1 error
Solution:
abstract class A { int x=90; abstract void methodAbstract(); // can not have a body }
Non-Abstract Method must have body part
abstract class A { int x=90; void methodA();// must have a body abstract void methodAbstract(); }
Output

C:\JavaProgram\Abstract>javac A.java
A.java:4: missing method body, or declare abstract
void methodA();// must have a body
^
1 error

Solution:
abstract class A { int x=90; void methodA()// must have a body {} abstract void methodAbstract(); }
Rules:If abstract class extends another abstract class then abstract method of extended class either define or not be define.
abstract class A { int x=90; void methodA() {} abstract void methodAbstract(); }
Saveas : A.java
abstract class AA extends A { void methodAA() { } abstract void MethodAbstractAA(); }
Saveas : AA.java
 



 

Latest Tutorial


Most Visited Tutorial