- GETTING STARTED
- BASIC JAVA
- JAVA STRINGS
- EXCEPTION HANDLING
- JAVA 9
- EFFECTIVE JAVA
In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change).
Information hiding is the ability to prevent certain aspects of a class or software component from being accessible to its clients, using programming language features. Information hiding is a powerful programming technique because it reduces complexity.
Information hiding is one of the most important principles of OOP. By Information Hiding we mean "Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world."
Lets see an simple example of Information Hiding:
Your name and other personal information is stored in your brain we can't access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you would like to share with us.
In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding.
In the perspective of Object Oriented Programming Information Hiding is,
"Hiding the object details (state and behavior) from the users"
Here by users we mean "an object" of another class.
Information Hiding is achieved in Object Oriented Programming using the following principles,
We can achieve information hiding using Encapsulation and Abstraction, so lets see these two concepts in detail.
Abstraction is the process of hiding out the working style of an object and showing only the required information of the object in understandable manner.
A radio has a tuner, an antenna, a volume control, and an on/off switch. To use it, you don't need to know that the antenna captures radio frequency signals, converts them to electrical signals, and then boosts their strength via a high-frequency amplification circuit. Nor do you need to know how the resulting current is filtered, boosted, and finally converted into sound. You merely turn on the radio, tune in the desired station, and listen. The intrinsic details are invisible. This feature is great because now everyone can use a radio, not just people with technical know-how. Hiring a consultant to come to your home every time you wanted to listen to the radio would become awfully expensive. In other words, you can say that the radio is an object that was designed to hide its complexity.
Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking, Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.
To me abstraction is the way we see things for example in my car I never know that how many devices are used in my engine, how petrol burns, how gear system changes the speed of my car, I only knows that when I press accelator my car moves fast and when I press breaks my car slows down and I can adjust speed of my car using gears.
Similarly in object oriented world, we always try to hide the complexity of a system from its end user e.g. I have a chat system which interacts with another chat client. Then i will try to create a abstraction layer which will hide all the complexity of TCP/IP protocols and give user simple functions like SendMessage or RecieveMessage.
One of the fundamental reasons for engaging in the task of abstraction in software analysis, design and development is to reduce the complexity to a certain level so that the relevant aspects of the requirements, design and development may be easily articulated and understood.
Following are the different definations of abstraction you could use if asked for in your interview:
My favorite one is : Abstraction lets you focus on what the object does instead of how it does it.
 You know that you need to put on the keys, change the gear and accelerate in order to drive a car but do you actually know what happens when you change the gear and accelerate, or say how the car engine works? You don't, and that's simple because you don't need to know all that complexity. Your Job is to just drive the car. That's what we call Abstraction.
 Lets take a person as example and see how that person is abstracted in various situations
 Monitor ON/OFF
The user doesn't need to know much about all the chips functioning that happens when Monitor is switched ON or OFF..All he needs to know is On Function ON-Monitor is On and on function OFF-Monitor is off.
An abstraction layer is a mechanism that separates two complex systems. In computing, an abstraction layer or abstraction level is a way of hiding the implementation details of a particular set of functionality, allowing the separation of concerns to facilitate interoperability and platform independence or to modularize a software system for later expansion.
A typical vision of a computer architecture as a series of abstraction layers: hardware,firmware, assembler, kernel,operating system and applications.
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.
Having read the above section, you might have now come to an idea of how abstraction is done in Java
when You know something needs to be there but not sure how exactly it should look like.
e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons, the same concept apply to interface in Java also.
so implementation of those start() and stop() methods should be left to their concrete implementation e.g. Scooter , MotorBike , Car etc.
Java Interface is an another way of providing abstraction, Interfaces are by default abstract
you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop()method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class. Interface like Runnable are good example of abstraction in Java which is used to abstract task executed by multiple thread.