Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.
Yesterday I saw Mariyaan, a Tamil movie. My wife is a diehard fan of Dhanush, the hero of the film. In his inner self Dhanush is a complex and intense actor. But what you get to see is a neighborhood boy, you get a feel that you know him well for long. It is because of his acting skills, he abstracts his complex self and exposes the apt quantity required for the moment. This is abstraction :-)
When we see a nice car on the road as a casual onlooker, we get to see the whole picture. The car as a one single unit, a vehicle. We do not see the underlying complex mechanical engineering.
Now consider we are going to a showroom to buy a car. What do we see now? We see four wheels, powerful engine, power steering etc. We see the car at high level components. But, there is so much inside it which gives the completeness to the car.
Now consider a mechanic, who is going to service the car. He will see one more level deeper with more level of information.
When we design software, we take the context. In the above example, we ask the question whether we are designing the software for a causal on looker or a buyer or a mechanic? Levels of abstraction is applied on the design accordingly.
In general computer software, when we talk about abstraction the software language itself is an example for the concept of abstraction. When we write a statement as,
a = b + c;
we are adding two values stored in two locations and then storing the result in a new location. We just describe it in an easily human understandable form. What happens beneath? There are registers, instruction sets, program counters, storage units, etc involved. There is PUSH, POP happening. High level language we use abstracts those complex details.
When we say abstraction in Java, we are talking about abstraction in object oriented programming (OOP) and how it is done in Java. Concept of abstraction in OOP, starts right at the moment when a class is getting conceived. I will not say, using Java access modifiers to restrict the properties of an object alone is abstraction. There is lot more to it. Abstraction is applied everywhere in software and OOP.
When a class is conceptualized, what are the properties we can have in it given the context. If we are designing a class Animal in the context of a zoo, it is important that we have an attribute as animalType to describe domestic or wild. This attribute may not make sense when we design the class in a different context.
Similarly, what are the behaviors we are going to have in the class? Abstraction is also applied here. What is necessary to have here and what will be an overdose? Then we cut off some information from the class. This process is applying abstraction.
When we ask for difference between encapsulation and abstraction, I would say, encapsulation uses abstraction as a concept. So then, is it only encapsulation. No, abstraction is even a concept applied as part of inheritance and polymorphism.
We got to look at abstraction at a level higher among the other OOP concepts encapsulation, inheritance and polymorphism.
Let us take inheritance also in this discussion. When we design the hierarchy of classes, we apply abstraction and create multiple layers between each hierarchy. For example, lets have a first level class Cell, next level be LivingBeing and next level be Animal. The hierarchy we create like this based on the context for which we are programming is itself uses abstraction. Then for each levels what are the properties and behaviors we are going to have, again abstraction plays an important role here in deciding that.
What are some common properties that can be exposed and elevated to a higher level, so that lower level classes can inherit it. Some properties need not be kept at higher level. These decision making process is nothing but applying abstraction to come up with different layers of hierarchy. So abstraction is one key aspect in OOP as a concept.
Having read the above section, you might have now come to an idea of how abstraction is done in Java.
in all these areas, we use abstraction as a concept. I think example Java code for all the above is very trivial. If you find it difficult to understand abstraction, pour your question in the comments section, I will be more than happy to answer it.
Comments are closed for "Java Abstraction".
First I thought, what is Dhanush doing in javapapers? But… you have written nicely!
I have 8 years experience in software development. Only now, I understood abstraction. Thanks!
Okay, how was Mariyaan?
Perfect article!
GREAT AS ALWAYS…
Hi,
I have a scenario as below: Please tell me the difference in abstraction and inheritance here..
Scenario:
Inheritance : —
public interface animal
{
public boolean eat();
}
Abstraction : —
public abstract class animal{
public abstract boolean eat();
}
Thanks,
Abhishek
Also let me know which one will I use from design and performance aspect, abstraction or inheritance?
Dear Joe,
As usual you explained abstraction in simple language and example.
Hi,
Tell me the explanation for interface and abstractoin
Hi,
The way you have started this article is very impressive.
And, the post is quite interesting and clear.
Thanks.
Thanks Mahesh
Abhishek, I have discussed this in the article.
You should not see inheritance and abstraction as to comparable concepts. Abstraction is used in inheritance as a concept. interface definition and abstract class, both uses abstraction concept.
For design, you will use both abstraction and inheritance.
Performance of an application is a completely different topic with respect to what we are discussing.
Thanks Pushkar
https://javapapers.com/core-java/abstract-and-interface-core-java-2/difference-between-a-java-interface-and-a-java-abstract-class/
This article may help you to some extend.
PHP Madam, so you are strolling in Javapapers. Thanks for reading :-)
I have started reading your blogs for a quite some time. It is awesome. Keep up the good work Joe.
BTW, This is the first time I am seeing you replying to everyone. :)
Can you please explain the concept of encapsulation with respect to the class, packages and jar ?
Nice explanation with Dhanush on Abstaction. Today i am planning to watch Mariyaan in Skywalk.
Thanks.
Nice!
Good way of presenting the concepts
Awesome explanation… Thank you
Thank you sir ! I was always get confused in encapsulation and abstraction. But after reading the complete article i feel confident to explain the concept to others. I already subscribed you and i am going to share the article.
It was not as much helpful to clear the concept. I want to more elaborate it.
Your efforts are always appriciable.
Hi,
Nice feel to read the content and explanation about Java Abstraction :)
You are contributing a lot to make some ones carrier by clearing concepts.
Thanks a lot.
Thanks Dear
Now i feel i understood it by heart.
But
I want know some real world general example
for abstraction,encapsulation and existential dependency among oops basic principle
Abstraction
Encapsulation
Inheritance
Polymorphism
T
G8 job man..!!!!!!!!
Thanks Karthik.
With the wonderful participation of the community/users like you, I get motivated. My passion for java and javapapers keeps on growing.
Mohd, please check my previous article on encapsulation https://javapapers.com/core-java/java-encapsulation/
Thanks. Let us know about your experience :-)
Thanks a lot Aashutosh. It will help.
Devendra, not happy with the example given for abstraction?
Thanks Kaustubh. Do you have any specific questions on abstraction? let me try to help you out.
Thanks Arvind. Sure, keep watching the blog, lot more is coming up!
Welcome Pareshwar.
Now I have written about encapsulation and abstraction.
I will write soon on other OOPS concepts soon.
No Phani. There is no relation between abstraction and overloading/overriding.
To know about overloading and overriding visit, https://javapapers.com/core-java/overloading-and-overriding/
Thanks Rakesh.
Thanks Mahesh.
sir,
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
in the below program,what is the information that is hiding to the user,can you explain me in this program context and also can u explain in physical sense of hiding the functionality from user plss.
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println(“drawing rectangle”);}
}
class Circle extends Shape{
void draw(){System.out.println(“drawing circle”);}
}
class Test{
public static void main(String args[]){
Shape s=new Circle();
s.draw();
}
}
Naresh, you got to read the article completely. I have discussed about exactly what you have asked in the article.
An abstract class, just gives the contract and there is no implementation details. In that way, it uses abstraction. Similarly with inheritance, there are layers of abstraction applied. You may read the car example I have give for abstraction in the article.
Hai Joe,
Even though I read the explanation of abstraction before, I now came to know that more clear about abstraction and with other OOP concepts. So thank u so much. This helps us a lot.
Julias
sir i need more explanation for this program pls explain in detail
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println(“drawing rectangle”);}
}
class Circle extends Shape{
void draw(){System.out.println(“drawing circle”);}
}
class Test{
public static void main(String args[]){
Shape s=new Circle();
s.draw();
}
}
sir please provide examples for abstraction and encapsulation programatically
thanks
nice tute…with an easy example with dhanush
Nice article. Good way of blogging which makes it easy to understand.
Buddy this is highly appreciated !!!
Havent found such good explaination elsewhere.
nice article…
Can i say that abstraction is a concept and encapsulation is the way to achieve abstraction.
Hi joe,
Is abstract can be declared as final? .
Thanks
Nice arti.
First of all I want to say superb blog! I had a quick question that I’d like to ask if you don’t mind.
I was interested to know how you center yourself and clear your head before writing.
I have had a difficult time clearing my mind in getting my ideas out.
I do take pleasure in writing but it just seems like the first 10
to 15 minutes are wasted just trying to figure out
how to begin. Any suggestions or tips? Thanks!
amazing explanation of topics
thanks sir
Thanks Ilamparithi.
Its been long time we have met.
Good One Joe
Thanks Joe.
Abstraction vs Encapsulation in terms of Java and OOPS is much more clearer than it was an hour ago..
I always thought there is not much difference when we discuss both the topics in terms of Java – both related to hiding the implementation details to the end user.
Now I understood they are related to my understanding but not exactly the same way I thought they were..
Priyanka – you cannot do that.
Simply, consider a class..
If you declare it as final, you cannot extend it.
If you declare it as abstract, it needs to be extended/subclassed.
So using both of them together won’t help at all and even if you try then the compiler will throw an error..
Nice tutorial on basics.
It may be good idea to provide a printable format of the same.
NICE explanation about abstraction .
Hi joe this is kavin can u send me some java technical questions
Hi Joe,
As you mention, that Abstractin is used as conceptual part .
So can we consider abstraction in basic principle of Object Oriented or it is sub part of OOps principle.
Please explain
Regards,
Prashant M
There is no way to compare Abstraction and Inheritance. Abstraction is a concept, which can be achieved via Inheritance or Interface.
if u want all of your animal subclasses to have there specific eat behavior then you go with abstract class
oops! lot of people are asking for example :-(
This makes me sad. You need to spend some time in reading text.
Padmaja, if you read this article line by line, you will see there are examples given inside that.
Code example is not always required to explain a concept. Please read this article completely and let me know if you are not able to understand any line and I will help you out.
roughly you can say like that. Abstraction is part of even inheritance too.
Shiva,
Looks like you are better than me. Sometimes I spend even more than an hour to start. To strike that rhythm we need to spend that time and I will not say that as wasted time.
What to write is the biggest question of all. Once the topic is decided and how to start is the next problem. Once we are into the second paragraph, then it will be like riding a Ferrari.
Welcome Bharath. I feel good when the article serves it purpose.
Yeah sure, that is a good idea.
But by environment loving friends are saying, printing should be reduced as much as possible.
Thanks Praveen
Kavin,
Whole of javapapers is dedicated for such java technical questions.
There is no such categorization as ‘sub part’ or ‘conceptual part’ in OOPs.
One thing is clear, abstraction is an important concept of OOPS.
Thanks Sonu.
Where are you boss?
Nice Post…
Nice article and specially examples by Dhanush :)…
Planning to write any book in coming future :) ???
Nice notes for abstraction.
Thanks,
Udhayakumar G K.
Hi,
Thanks for the explanation.. Its too clear for Abstraction.I thought to ask some questions, but all others comments answers for everything.. Its very good to see you are replying for all comments.. Keep up the good work..
Hi,
In the above example, Shape class, draw method is common for all shapes such as rectangle, triangle, etc. But the implementation(drawing) for Rectangle, Triangle classes will be different. So we declared Shape as abstract class and Rectangle, Triangle are concrete classes. In this example Shape class doesn’t require to draw anything, but if you go for Rectangle class it should draw its shape so the draw method is implemented here. In this hiding implementation of draw in Shape is one level of Abstraction(hiding unnecessary things), implementing draw in its child(Rectangle) class is one level of Abstraction(show only the functionalists required by the class)
Hi Joe,
I am an avid reader of your Java blog. These articles are helpful in updating and keeping in touch with the Java and related technologies. But for the last a month or so I don’t see any new articles. Please keep posting new articles.
Please explain with program
Thanks Sachin. I wish I write a book. Instead of doing a traditional Java book, I am having something in mind like editing this blog and create a book out of it.
Do you play cricket :-)
All the best with your new blog.
Welcome Malathi.
Replying for every comments is a practice I am trying to follow recently. Readers are investing their time to talk and I should be sincere and respect that.
People like you are answering queries for other readers and creating a meaningful conversation. It is of great help for me. Thank you.
Yes Omkar, I didn’t write new articles for a month.
There is a very huge change in my personal life and career happening now and I am trying to fix a few things.
This change is going to be better for me/JavaPapers and I will get more time to write and talk to you.
You will all experience this in a week. There is no change in my aim, “it is to make JavaPapers as the most respected and loved resource for anything in Java”.
if some one ask that what is the basic difference between in abstraction and encapsulation?
And explain with real example and not in form of class and object?
I would like to appreciate your efforts.
I just have one doubt if abstraction is implemented using private and inheritance what does encapsulation do. I mean you just gave an example of encapsulation where you made the variables private and methods used to access them public. can you give a similar java code example on abstraction.
Nikhil,
Please go through the article one more time and read through the comments. I have answered the same question already :-)
Abstraction is more of a concept and implemented in multiple ways. The last section / paragraph talks about how we do abstraction in Java, please read that and you will not need code example.
Welcome Pandey.
Abstraction is at a higher level as a concept than encapsulation.
I feel this one liner gives a simple difference between abstraction and encapsulation.
If you are not able to understand this line, then you have to carefully read both my articles on abstraction and encapsulation.
Wow!!!!
Thanks for sharing Joe… :)
Welcome Amar.
Thank you so much Sir fr d useful stuff.. luking forward fr sum more :)
n hw do i contact u if i hv doubts?
n do u take classes?
Welcome Pragna.
You can reach me through my email joe@javapapers.com
I have plans to take classes in future. Keep watching JavaPapers, I will announce about it.
This can be implemented through interface as well like each implementer class will have it’s own eat behavior. What is the difference while implementing the same through abstract classes.
By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).
hi…can you explain static class,static method with simple example.and also explain diff between static class and abstraction?
Hi,
I wanted to know if a class is declared abstract and there are no abstract method in that class will it be considered as abstract class?
Thanks & regards,
Kalpana
Because nowdays Dhanush is more popular than abstraction.
I am hearing and reading same thing since i completed my engineering, this is good
but why you no give an code example, where how and what is the thing like abstraction in java coding .
Please provide an code example with example.
Hi Joe
Recently I faced one Interview question
Interviewer: “What is abstraction?”
Me: “Hiding the implementation (It may be wrong,don’t consider) and showing only the essential data”.
Interviewer: Anyway your java code will compiled as class file.so it wont be readable.even if source files are accessible, your implementation class also will accessible .Then what is the use of hiding
Me:??????
Can you Explain what is the use of hiding ? where it will be useful? with Example
yes it is considered as a abstract class because in abstract class if we are not declaring any method as a abstract then it is considered as a abstract class and this class providing the base functionality for other classes.
Thanks for the article. But I am still confused, why should we code to the highest level of abstraction? Take the following java code:
ArrayList c = new ArrayList(); //BAD
List c = new ArrayList(); //Better
Collection c = new ArrayList();//Best
please tell me WHY?
But inteface must contain abstract methods as it is 100 5 abtsract class then how can you write non abtstact method eat in interface
[…] Java Abstraction Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context. […]
Really nice post.. have some queries about String also, please do answer it..
when we are initializing String using new keyword and if it is checked for “==” it shows not equal even though, hashcode and value both are same.. can you please explain how??
Hi all,
I have an query that what is bydefault access modifier for abstract methods and what is the difference between abstract and interface.
what is difference between abstraction and encapsulation? and also difference between interface and abstract class?
Hi Joe,
Could you explain principles like what is abstraction, encapsulation, inheritance, operator overloading by taking an example suppose of an Air Conditioner?
Anyways great article please keep up the good work helps a lot of java enthusiast.
Regards,
Gaurav
great article.
fine
hi joe,
i like your explianation but please provide an example with programm
How can we differentiate abstraction from Encapsulation?
How they are different from each other..
Interfaces are like design concepts or ideas that are in a designer’s mind and the designer does not know how it will be implemented. For example you have an idea of a flying bicycle as a concept, that if you start riding a bicycle after sometime the bicycle could fly. This is an idea. For this you can create an interface named FlyCycle and you know that it has to do is to fly, so you will be having an abstract method fly, but you don’t know how it will fly. You leave that to the user of your interface. Whoever takes your idea will have to come up with their own imagination of how it will fly and what speed it should have to make the bicycle to fly. But at the same time Abstract class is a partial implementation of some functionality. Say for example, if you are the designer of the Human Class, you for sure know that humans can eat and sleep and you have provided the implementation of the sleep because for sure you know that for a human to sleep, we will be sleeping by closing the eyes, so you have provided this implementation in your class and the user of your class can override that how you sleep, whether on a bed or a floor or in a chair or so.
nice helped lot
sure
because abstraction is implemented with encapsulation so data hiding concept emerged.
no encapsulation without abstraction .you think correct.
friendly but interface access modifier is public and only public.
interface can’t be instanciated same for abstraction but abstraction is invoked .
normally a lot time both work for derived class but absract can be extends to one super class while interface impemented violeting the rule of multiple inheritance.
abstract can have instance final and non final variable,while interface only final variable.
hello sir i am big fan of your blog. I am in the starting phase of my career in software world as the java developer. I read many of your’s blogs and love the way you explain the technical terms. My all the query’s for the Abstraction is now clear . I also read your blog which is on the axis2 integration with eclipse your blog save my life that time :P . In the end Thank you so much for such a nice blog and such a nice and lovely explanation.
Thank you so much Simmant. Keep reading and sharing.
This is simply nice ! i been practicing in Android since last 4 years and it just gave me hint to enhance the security layer ;)
Thanks Dhams. Great that you doing Android for 4 years. You must be a pro and I will ping you if I need some help ;-)
I am very much thank ful to you for all your explanations….Thank you JOE.I have one doubt,please clarify it. As we are implementing inheritance through extends keyword and we are implementing polymorphism with over-riding and over-loading and we are implementing Encapsulation with CLASS concept. Do we have anything /any keyword to implement ABSTRACTION… is abstract keyword is used to implement abstraction.. as per your article this is not correct.. i understood this article , but still having this doubt…please clarify
Hi,
Thanks for adding so much knowledge within our fraternity.
Well what I wanted to ask is:
can we instantiate an abstract class?
if yes how?
Still m confused between abstraction and encapsulation..
I have read all the comments but could not get the exact differance between both of them :(
No Abstract classes cannot be instantiated.They are designed such that only the classes which extends the abstract class should provide implementation for any of the abstract behaviour. These child classes can further be instantiated and inturn the abstract class behaviour gets transferred/called implicitly.
I would like to quote a real time example.
suppose your are developing a login page for an application.
On the design perspective you would think that a the login class will require only user name and password rather than the adress or a phone number.This design descion is made using abstraction(only to display the relevant information wrt the context rather than overloading the class with irrelavant information).
Abstraction comes into play in case of inheritance as well.
This happens on the design level where we design the base class in such a way that the common behaviour or the static behaviour is defined and only the dynamic behaviour is set as abstract which is implemented based on the child class which implement the super class.
Encapsulation is the binding of data ,object is an example of encapsulation.Encapsulation means hiding of the implementation details from the outside classes.So when say a class c1 uses a method for example “printHelloworld(String)” of class c2 it just calls the method after instantiating the class c2.This way the implementation details of the method “printHelloWorld” is hidden from c1(outside class).This is the essence of encapsulation.
Great explanation!!!!
Thank you
nice article . thanks for providing valuable information
Welcome Kushal.
Nice One I have 5 years of exp in java but now I understand the concept of abstraction but I want to know the clear difference b/w abstraction & encapsulation. Could you please clear ?
Very nice Real Life Example like Car and Programming Example like adding two variables.
Now I am clear about Abstraction.
But generally java says abstraction and encapsulation are two different concept of OOPs. As you described above that encapsulation uses abstraction, then why two concepts arises here, it must be only one that is ABSTRACTION. Please let me know.
Hi I have searched a lot and found many Theoretical Definition about Abstraction
Abstraction – it is something showing required data to user , clients etc .
Many people says that we can achieve this by two ways
1. Abstract Class – 0 to 100%
2. Interface 100%
So please see the example below :
Pack1:
Interface Addable
{
add(int a,int b);
}
Class Addition implements addable
{
public int add(int a,int b)
{
return a+b;
}
}
pack2:
import pack1.Addable;
import pack1.Addition
Class Client
{
Addable a=new Addition;
S.O.O(a.add(5,5)); // Abstraction i can not see the definition of add method
}
I agree that the above Example is for Satisfied for Abstraction but please see the below Example
——————————————————
Pack1:
Class Addition
{
public static int add(int a,int b)
{
return a+b;
}
}
// i am creating .jar file of the above class … “Addition.jar”
pack2:
import pack1.Addition
Class Client
{
S.O.P(Addition.add(5,5));
// here also we can not see the definition if we assume that “Addition” is in .jar file
// Can say that the Second Example is also satisfied for Abstraction
}