<< Previous Chapter | Next Chapter >> |
Abstraction in Java:
Abstraction is one of the most important features of object oriented
programming. Abstraction is used to define standards. The abstraction says that
show functionality and hide complexity. Let’s understand it by taking some real
examples, a straight forward example of it is just eat mango, do not count the
tree. Other example include that going through an agent to get driving license,
we just pay them and give the documents and finally get driving license from
them, we don’t know what happened internally where they went, what they did to
get the DL ready. So, here the complexity is completely hidden, we just access
the functionality.
Now, let’s try to understand how we implement abstraction in computer
world, let’s see this example:
public class Bird {
abstract void fly();
}
In above example whenever we declare a method as abstract, we need not
to write its body, just terminate it with semicolon. The class which will extend
this class will have to define its actual code. Users just need to call this
function to execute the fly functionality, no need to understand how it is
actually implemented.
class Parrot extends Bird
{
void fly()
{
System.out.println("Fly is
finally implemented..");
}
}
In
multithreading concept (which we will discuss in coming chapters), user just
need to call the run method without bothering about how it is coded.
Let’s
go in depth of the abstraction, there are 2 types of abstraction:
1.
Using
abstract class.
2.
By
using interface
Rule 1: abstract class can’t be
instantiated, we cannot create an object of the abstract class.
Rule 2: only abstract class can
have abstract methods. The reason of this rule is to restrict the programmer that they should not be able to call it directly, first they should provide its implementation.
Let’s take this example:
abstract class Base {
int x, y;
void show()
{
System.out.println("x =
" + x + ", y = " + y);
}
}
class Child extends Base
{
void get (int x, int y)
{
this.x = x;
this.y = y;
}
public static void main(String
args[])
{
Child
c = new Child();
c.get(10,
20);
c.show();
}
}
Output:
x = 10, y = 20
Above example shows that, it is not mandatory for an abstract class to have an abstract method, also if we will try to create object
of Base class, compiler will give error.
Lets modify above program and add display() method as an abstract.
abstract class Base {
int x, y;
void show()
{
System.out.println("x =
" + x + ", y = " + y);
}
abstract void display();
}
class Child extends Base
{
void get (int x, int y)
{
this.x = x;
this.y = y;
}
void display()
{
System.out.println("Display
method is implemented here..");
}
public static void main(String
args[])
{
Child
c = new Child();
c.get(10,
20);
c.show();
c.display();
}
}
Output:
Here we have declared the display method as abstract, it means it is
must for the sub class (Child) to give definition of display method otherwise
compiler will give error.
The other option is to declare
the Child class as abstract, by doing so, Child class need not to define the
display method. Same rule will apply for Child class as well, means if any
class is extending Child class then it must either give implementation of display
method or declare itself as abstract.
This is allowed in java (OOPs). In above line of code, we are creating a
reference variable (not object) of Base class and assigning the object of Child
class in it.
Dynamic Binding:
The abstraction is based on dynamic binding. The dynamic binding says
that, a parent class reference variable can hold child class object, it is also
called as up casting. Dynamic binding is also known as run time polymorphism.
Base base = new Child();
Quick Question – can we
keep a constructor in abstract class? Because object will never be created then
what is the use of constructor in it?
Answer – yes, we can create the
constructors in an abstract class. Because
- No class can exist without any constructor.
- If there is no constructor in parent then we cannot create child object.
Quick Question – can we
make a static method as abstract?
Answer – never, because we cannot
perform dynamic binding with static method.
<< Previous Chapter | Next Chapter >> |
No comments:
Post a Comment