Jay and Jane are college students and Jane is assigned as the programming mentor for some of the first year students by the Academics department. Jay has just joined the college and is struggling with his programming course, one day Jane finds Jay with frustration and starts a conversation.
Jack - Hey! Why do you seem so frustrated, what's going wrong?
Jay- I am having difficulty understanding programming concepts and the classes are too confusing. Over and above the pace of the course is too fast.
Jack - Ok, I also had some challenges during my initial time at the college. But here is a piece of advice: just try to articulate the big and fancy things in your own words and create some real life metaphors. It will help you a lot in learning, computer science being a recent discipline has a lot of human touch. So create your own metaphors and learn by doing the things! What difficulty are you facing?
Jay - I am facing difficulty in understanding the OOPS concepts. I know the basic definition but don't know how it adds value to writing a good code.
Jane - Okay! No worries let's start with Inheritance. You know from biology that a child inherits physical properties from parents or ancestors, right?
Jay - Yeah
Jane - similarly the child class inherits properties, behaviour from the parent class. Now tell me if I have to write code even for child behaviour which is the same as parent, won’t that increase lines of redundant code?
Jay - Yeah! So, to enforce reusability of code we use inheritance?
Jane - Exactly. Polymorphism is even more interesting. You may have studied that carbon exhibits polymorphism.
Jay - Yeah, under different temperature and pressure, it exists in two different forms as graphite and diamond.
Jane - Similarly, in programming certain structures exhibit polymorphism, meaning they coexist in two different forms, also the capability of an object to stand for the parent object comes under the concept. It brings flexibility as you can modify the code for the child class according to your need.
Jay - Really, I missed you in the second part, can you help me understand with an example?
Jane - Here is an example
class Airplane {
int speed;
private void setSpeed(int speed){
this.speed = speed
}
public int getSpeed(){
return this.speed
}
}
Class Jet extends Airplane {
private void setSpeed(int speed) {
this.speed = 2*speed;
}
}
Jet jt = new Airplane();
The ability of Jet class to stand for its parent class also comes under polymorphism.
Jay - Oh, In class we were told function overloading as example of polymorphism. But function overriding also comes under the polymorphism.
Jane - True! Encapsulation, you may have eaten those capsules right when you fall sick.
Jay - Yeah it encapsulates many types of mixture within that one capsule.
Jane - Exactly, we group things to prevent the wrong usage.
Jay - Prevent its wrong usage? What does that mean?
Jane - Ok, What do you think should be the output of the program?
Class Jet extends Airplane {
public void setSpeed(int speed) {
this.speed = 2*speed;
}
public void getSpeed() {
return this.speed;
}
}
Jet jt1 = new Jet()
jt1.speed = 500;
system.out.println(jt1.getSpeed())
Jet jt2 = new Jet()
jet2.setSpeed(500)
system.out.println(jt2.getSpeed())
Jay - It should be something similar to this…
500
1000
Jane - Correct! But according to you what is the right way to use the method.
Jay - Obviously it should be the way that we have done for jt2
. So, encapsulation is all about making the data members private so that someone cannot use them in a wrong way!
Jane - No No No… it's only a part but encapsulation is logically separating your application so that it is easy to change the application. You must know that a good software is flexible to the changes. It is also used to achieve information hiding.
Jay - Oh when my professor was explaining the concept of information hiding I was in a confusion that from whom are we hiding the information but now I think its the developer, they should use the methods instead of directly manipulating the properties of the objects.
Jane - Yea that correct. Hope this short conversation was helpful! Happy Learning.