Abstract Class in Java 2
Abstract Class in Java 2
Abstract Class in Java 2
A class which is declared with the abstract keyword is known as an abstract class in Java. It
can have abstract and non-abstract methods (method with the body).
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only essential things to the user and hides the internal details, for
example, sending SMS where you type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
So when we know that all the animal child classes will and should override this method, then there
is no point to implement this method in parent class. Thus, making this method abstract would be
the good choice as by making this method abstract we force all the sub classes to implement this
method( otherwise you will get compilation error), also we need not to give any implementation
to this method in parent class.
Woof
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body of the
method.
Rules
Note 1: As we seen in the above example, there are cases when it is difficult or often unnecessary
to implement all the methods in parent class. In these cases, we can declare the parent class as
abstract, which makes it a special class which is not complete on its own.
A class derived from the abstract class must implement all those methods that are declared as
abstract in the parent class.
Note 2: Abstract class cannot be instantiated which means you cannot create the object of it. To
use this class, you need to create another class that extends this this class and provides the
implementation of abstract methods, then you can use the object of that child class to call non-
abstract methods of parent class as well as implemented methods(those that were abstract in parent
but implemented in child class).
Note 3: If a child does not implement all the abstract methods of abstract parent class, then the
child class must need to be declared abstract as well.
Do you know? Since abstract class allows concrete methods as well, it does not provide 100%
abstraction. You can say that it provides partial abstraction. Abstraction is a process where you
show only “relevant” data and “hide” unnecessary details of an object from the user.
Interfaces on the other hand are used for 100% abstraction (See more about abstraction here).
You may also want to read this: Difference between abstract class and Interface in Java
Because these classes are incomplete, they have abstract methods that have no body so if java
allows you to create object of this class then if someone calls the abstract method using that object
then What would happen?There would be no actual implementation of the method to invoke.
Also because an object is concrete. An abstract class is like a template, so you have to extend it
and build on it before you can use it.
For now lets just see some basics and example of abstract method.
1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same
way abstract method must be overridden.
4) A class has to be declared abstract to have abstract methods.
Like C++, in Java, an instance of an abstract class cannot be created, we can have
references of abstract class type though.
abstract class Base {
abstract void fun();
}
class Derived extends Base {
void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
Like C++, an abstract class can contain constructors in Java. And a constructor of abstract
class is called when an instance of a inherited class is created. For example, the following is
a valid Java program.
// An abstract class with constructor
abstract class Base {
Base() { System.out.println("Base Constructor Called"); }
abstract void fun();
}
class Derived extends Base {
Derived() { System.out.println("Derived Constructor Called"); }
void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
Derived d = new Derived();
}
}
Output:
3) In Java, we can have an abstract class without any abstract method. This allows us to
create classes that cannot be instantiated, but can only be inherited.
// An abstract class without any abstract method
abstract class Base {
void fun() { System.out.println("Base fun() called"); }
}
class Main {
public static void main(String args[]) {
Derived d = new Derived();
d.fun();
}
}
Run on IDE
Output:
4) Abstract classes can also have final methods (methods that cannot be overridden). For
example, the following program compiles and runs fine.
// An abstract class with a final method
abstract class Base {
final void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
Base b = new Derived();
b.fun();
}
}
Output:
Mostly, we don't know about the implementation class (which is hidden to the end user),
and an object of the implementation class is provided by the factory method.
A factory method is a method that returns the instance of the class. We will learn about
the factory method later.
In this example, if you create the instance of Rectangle class, draw() method of Rectangle
class will be invoked.
File: TestAbstraction1.java
9. class TestBank{
10. public static void main(String args[]){
11. Bank b;
12. b=new SBI();
13. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
14. b=new PNB();
15. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
16. }}
Rate of Interest is: 7 %
Rate of Interest is: 8 %
File: TestAbstraction2.java
1. class Bike12{
2. abstract void run();
3. }
compile time error
Rule: If you are extending an abstract class that has an abstract method, you must either provide the
implementation of the method or make this class abstract.
//Regular method
public void disp(){
System.out.println("Method of class Sum");
}
}
//Regular class extends abstract class
class Demo extends Sum{
10
26
Method of class Sum