Object-Oriented Programming in a Nutshell
“Tell me the Basic Concepts of OOPS Programming”
After getting this question asked in Viva- Voice, numerous Interviews, and Exams. I almost got my answered readily almost as if I remembered it by heart.
But, One day in an interview, someone asked the same question but with a twist.
“Explain to me Object-Oriented Programming with a single example with as fewer jargon as possible”.
He told me to take some time and passed me a marker.
“I will be a bit unorthodox with my answer, you wouldn't mind do you ?”. The interviewer nodded with a smile. I wrote.
How a Computer Understands Real-World Objects
I drew a rough figure of a dog with a Name under it “RUFUS”. Under it, I wrote.
“OOPS is the which computer can identify and perceive data in terms of a real-world object”.
Data in the real world is very complex. In our example, It is a dog. A dog can’t be put as a number, a word, or as a boolean value(True/False). Hence, in order to understand the complex data, we introduce a new Datatype called Object.
On a basic definition “Object is ..”. My interviewer interrupted me. He pointed his finger to the top of the board.
I started again, “The dog is a representation of the real-world things. In order to make the computer understand all the complexities of its being, we use the concept of Objects. Furthermore, we can have more dogs like Rufus. Hence we create a data-object that can produce Dogs and call it a class.
Class Dogs(); Dog Rufus:
Now the dog has different characteristics like it has a name, color of his fur, he can bark, he is trained to sit on our cue.
Hence we need to dive deeper into the OOPs concept in order to know how we can have all his characteristics known to the computer.”
I wrote another four points on the boards.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
I continued to explain each of the points with respect to our Dog Rufus. “As of now, the computer just knows the dog as a single data object but has no details about it. There are four characteristics of Rufus with which I can explain with each of the above four principles”.
Abstraction:
Rufus sits down on whistling once
Imagine the computer wants to make Rufus sit on the floor. It knows that when you whistle he will sit. The computer in-order to make him sit won’t go through the process of training but in turn, just whistle. The computer shouldn't know unnecessary details. Knowing the time to train or favorite is irrelevant in our case.
Likewise, if you want to hide certain attributes that you might want to put a curtain on for security reasons or just for simplicity. You can achieve this by abstracting the data objects.
Note -in Java, we have this by implementing abstract classes and interfaces.
abstract Class Dog
{
public boolean Sitdown();
}
Encapsulation:
Rufus wants every body to know his name
Rufus is a very sweet dog until you call him by a different name. Hence for that, it was decided to his name tag as a buckle. Our computer is also a polite machine, hence he calls Rufus by his name. He knows as the name tag is printed and is known to the public. This information is available for the public view, but a certain private affair like the number of shoes he chewed is hidden.
Encapsulation is a concept where you decide what is to be viewed by the outside and what is to be hidden.
By defining A certain attribute as Private and Public you can achieve Encapsulation.
public String name;
private int chewed_shoes;
Inheritance:
Rufus is a Golden Retriever
Now the computer wants to know the breed of Rufus. For that, it looks for what its parents were. It finds that Rufus is pure Breed Golden Retriever and he has the properties of one.
Inheritance is the ability in which you can have other classes attribute of other classes. Golden Retriever is supposedly very friendly but not all dogs are. That means not all Dogs are supposed to have this characteristic but only Golden Retriever.
Class Dog extends Breed
Polymorphism:
Rufus is a dog as well non-veg
The computer knows Rufus as a Dog. But the computer now needs to know that it’s a Non-Veg Creature as well. He has a characteristic of both of these.
Hence the computer needs to understand that a single object can appear as different objects. In this case, Polymorphism comes into the picture. There can be a class that has some relation to Rufus. It can be some of his extension to other Objects such as Mammals, Non-Vegetarians, etc.
Class Dog implements Non-Vegetarians
A very basic Java code that portrays all the four implemented with two Classes
Class Dog extends Dog implements Non-Vegetarians
{public String name;
private int chewed_shoes;
private Dog()
{
name="Rufus";
chewed_shoes=5;}public boolean sitdown( int whistle_count){
if(whistle_count==1)
return true;
else
return false;}
public String breed()
breed_show();
public bool isNonveg()
return true;}abstract Class Dog
{
public String breed_show()
System.out.pritln("Golden Retreiver");
public boolean Sitdown();}interface Non-Vegeterian()
{
public boolean isNonveg();
}
I tried my best to explain each of the points in the OOPS concepts with as ease as possible. The addition of a Golden Retriever must have earned me some brownie points from the interviewer.
Hope you enjoyed this little article. And Responded if you want to see more concepts explained in lay man’s term.