You are on page 1of 6

Abstraction:

~~~~~~~~~~
Definition:
~~~~~~~~
Abstraction is one of the object oriented programming principle,
it is the process of retrieving the essential details or necessary
details without having background details

otherwise in this abstraction,


we are retrieving only relevant details and we are
hiding irrelevant details.

Ex:

A car is viewed as a car rather than its individual


components.

Data Abstraction may also be defined as the process of


identifying only the required characteristics of an
object ignoring the irrelevant details.

The properties and behaviors of an object differentiate it


from other objects of similar type and also help in
classifying/grouping the objects.

Consider a real-life example of a man driving a car.

The man only knows that pressing the accelerators will


increase the speed of car or applying brakes will stop the
car but he does not know about how on pressing the
accelerator the speed is actually increasing, he does not
know about the inner mechanism of the car or the
implementation of accelerator, brakes etc in the car.
This is what abstraction is.

In java, abstraction is achieved by


interfaces and
abstract classes.

We can achieve 100% abstraction using interfaces.

abstract method vs concrete method:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abstract method is a methood which doens't contains
any logic or we can say it contains only method heading
not method body.

ex:
~~
abstract void m1();

where m1() is abstract method, since it contains only method


heading not having method body.

note:
~~~~
in java, we are making methods as an abstract methods we
are using one java keyword i.e 'abstract' before the method
declaration.

abstract methods are also known as un-implemented/un-defined


methods

concrete method/implemented method/defined method:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is a method which contains both method heading and method
body means logic

ex:
~~
void m1()
{
//logic
}
where m1() is an concrete method since it contains both
heading and body.

Abstract class vs concrete class


~~~~~~~~~~~~~~~~~~~~~~~~~~~

in java, mainly we have two types of classes, those are

-concrete class/implemented class/defined class

-abstract class/un-implemented class/un-defined class

Concrete class is a class which contains only defined


methods
ex:
~~ class Test1
{
void m1()
{
//logic
}
void m2()
{
//logic
}
}

-Abstract class is a class which contains both un-defined


methods and defined methods.

ex:
~~~
abstract class Test1
{
abstract void m1();
void m2()
{
}
}

note:
~~~~
If a class having atleast one abstract method the corresponding
class will become abstract class

to make the method as an abstract method and to make


a normal class as an abstract class in java we are using
a keyword i.e 'abstract'

whenever we have any abstract class, we can't able to


instantiate directly since this class is not having fully
defined methods.

As a java programmer, whatever the abstract methods


which you declared in abstract class, it is our responsibility
to define or to provide the logic for those abstract methods
in some other class which is extending from
the abstract class.
note:
~~~
we can't create an object of abstract class directly but
we can create the object of abstract class indirectly

object of abstract class is nothing but object of a class


which is extending that abstract class

//write a java program on abstract class instantiation directly

abstract class Test1


{

abstract void m1();


}

class AbDemo2 extends Test1


{
void m1()
{
System.out.println("m1-Test2");
}

public static void main(String[] args)


{

//create the object of abstract class(Test1)

Test1 obj = new Test1();


obj.m1();

/*
javac AbDemo2.java

compile time error:


~~~~~~~~~~~~~~~
can not instantiated since obj is of Test1 type

*/

//assignment-1

write a java program for finding the memory sizes of


each and every fundamental datatypes

//assignment-2

write a java program for defining the constructor in


abstract class and exectute that ?

You might also like