what is "abstract class" in JAVA?
explain abstract class in JAVA?
Answers:
If a class doesnt define a method and the area between { and } is empty of a function, then this function is termed as abstract, if any class has an abstract method that class is also abstract, no object is created of an abstract class, instead you can inherit a class from an abstract class, but the inherited class must implement abstract method, otherwise it will be abstract too.
Other answers:
And, you can use an abstract class when you know in advance that atleast one of its methods will be overridden in all the child classes.
An abstract class is used to collect common operations into a class that will have many subclasses, while at the same time leaving some operations to be defined by the subclass (and requiring them to be defined).
The methods that are undefined are only declared in the abstract class, and have an "abstract" keyword, and the class declaration also has the abstract keyword. Example:
public abstract class AbstractClass
{
// non-abstract, shared method among all derived classes
public int myNonAbstractMethod()
{
return 0;
}
// abstract methods; all subclasses must define this
public abstract myAbstractMethod();
}
If a class derives from an abstract class, it MUST define the abstract method, or else must itself be abstract.
A rudimentary example would be an abstract Animal class which has an abstract speak() method; the subclasses would all implement speak for their individual animal subclasses: Cat, Dog, Lion, Giraffe, etc. The abstract Animal class could have a non-abstract greeting() method that calls speak().
A class that has contains at least one abstract method, and, therefore, can never be instantiated. Abstract classes are created so that other classes can inherit them and implement their abstract methods.