Skip to main content

OOPs Principles

Java Abstraction

   Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.

Abstraction Layers

    
    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.

Abstraction in OOP


    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.

Abstraction and Encapsulation


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 animal type 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 the 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.

Abstraction is nothing but showing only what is necessary and Encapsulation says hide the Complexity

Abstraction and Inheritance


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.

Abstraction in Java


Having read the above section, you might have now come to an idea of how abstraction is done in Java.
  • When we conceptualize a class
  • When we write an ‘interface’
  • When we write an ‘abstract’ class, method
  • When we write ‘extends’
  • When we apply modifiers like ‘private’, …
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.

Author : Joe
Reference : Java Papers

Comments

Popular posts from this blog

OOPS concepts Tutorial

OOPS concepts Tutorial Summary: By the end of this tutorial "OOPS concepts Tutorial", you will understand the meaning of OOPS concepts. Three basic concepts of OOPS Abstraction Encapsulation Polymorphism 1. Abstraction     Abstraction means using the equipment (or code) without knowing the details of working. For example, you are using your mobile phone without knowing how a mobile operates internally. Just a click on a button connects to your friend. This is abstraction. That is, the details of mobile mechanism are abstracted. Similarly we use the code of somebody to get the functionality without knowing how the code is written or working. For example, you are using printf() function to write to the DOS prompt without the awareness of what the code of printf(). One of the ways of achieving abstraction is inheritance. Through inheritance a subclass can use the super class methods as if they belong to it without caring their implementation details. 2. Encapsula...

Basic Definitions

Basic Definitions Class :     A class is a blueprint or prototype from which objects are created.                                                            (or) A Class is a template or Blue print is used to bind or group the related data members along with its related functionalities                                                            (or)     A class is a prototype that defines the variables and the methods common to all objects of a certain kind. Member Functions operate upon the member variables of the class. Object :  Objects is instance of a class it contains state and behaviors, State is nothing but variables and beha...
Struts Actions Forward Action :          In Struts MVC model, you have to go through the Action Controller to get a new view page. In some cases, you really just need to get a specified JSP page only, it's so stupid to create an action controller class which just forward the pate to you.        ForwardAction acts as bridge from the current view and pages it link to.ForwardAction uses the RequestDispatcher to forward to a specified web resource.This allow you to link to an action instead of directly to a jsp.  Used of Forward Action :- The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions.  ForwardAction used to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. Include Action :         Includ...