ABSTRACT CLASS IN JAVA
ABSTRACT CLASS
An abstract class is a class that is declared abstract — it may or may not include
abstract methods.
If a class contain any abstract method then the class is declared as abstract class.
An abstract class may have static fields and static methods.
An abstract class is never instantiated. It is used to provide abstraction.
Although it does not provide 100% abstraction because it can also have concrete
method.
SYNTAX
abstract class class_name { }
EXAMPLE:
abstract class A{
……………………..
}
ABSTRACT METHOD:
Method that are declared without any body within an abstract class are called
abstract method.
The method body will be defined by its subclass.
Abstract method can never be final and static.
Any class that extends an abstract class must implement all the abstract methods
declared by the super class.
SYNTAX:
abstract return_type function_name ();
EXAMPLE:
abstract class A {
abstract void call();
}
CONCRETE CLASS AND METHOD
A concrete class is a class that we can create an instance of, using the new
keyword.
In other words, it's a full implementation of its blueprint.
Concrete classes don't have any unimplemented methods.
Whether the implementations are inherited or not, so long as each method
has an implementation, the class is concrete.
CONCRETE METHOD:
A method which is not abstract i.e. if a methods definition is given in the
same class its declared is called concrete.
EXAMPLE FOR CONCRETE CLASS
abstract class Vehicle
{
abstract void engine();
}
class Car extends Vehicle {
public void engine()
{
System.out.println("Car engine");
}
public static void main(String[] args)
{
Car v = new Car();
v.engine();
}
}
EXAMPLE FOR CONCRETE METHOD
abstract class animal
{
abstract void eat(); // abstract method
void talk(){
System.out.println("Dog brks");
} //concrete method
}
ABSTRACT CLASS AND ABSTRACT METHOD
When to use Abstract Methods & Abstract Class?
Abstract methods are usually declared where two or more subclasses are
expected to do a similar thing in different ways through different
implementations. These subclasses extend the same Abstract class and
provide different implementations for the abstract methods.
Abstract classes are used to define generic types of behaviors at the top of an
object-oriented programming class hierarchy, and use its subclasses to provide
implementation details of the abstract class.
EXAMPLE
abstract class A {
abstract void callme();
}
class B extends A{
void callme() {
System.out.println(“what about you");
}
}
class Main{
public static void main(String[] args) {
B b = new B();
b.callme();
}
}
Abstract class with concrete(normal) method
abstract class A{
abstract void callme();
public void normal(){
System.out.println(“how are you");
}
}
class B extends A{
void callme(){
System.out.println(“fine");
}
}
class Main{
public static void main(String[] args){
B b = new B();
b.callme();
b.normal();
}
}
EXAMPLE
abstract class Vehicle
{
abstract void engine();
}
class Car extends Vehicle {
public void engine()
{
System.out.println("Car engine");
}
}
class Main{
public static void main(String[] args)
{
Vehicle v = new Car();
v.engine();
}
}