Wednesday, November 15, 2017

Basic OOPs Concepts

“Object Oriented” is a very popular world in programing languages. Many time it’s confusing because of it’s overuse or misuse. You might heard, Java is an Object Oriented Programming language. So before you learn Java, why don’t we understand Object Oriented methodology and its terminology?
Object:Object is an entity or an instance which has some well-defined attributes and behaviour. I said well-defined. So what I mean by this well-defined, One same type instance can be created  multiple context and in each context an object might require specific attributes which might not be necessary in other context. For example, let’s talk about a Person. One person can have a name, age, weight, height and many more attributes. This person can go to two different places.
1.      A Gym: If that person goes to Gym for registering himself/herself, he/she may need name, weight, height, and age.
2.      A Bank: If that person goes to a Bank for opening his/her account, he would not need height, weight etc. Bank will need only other attributes like name, age, address, spouse name etc.

So, in such case will create instance with properties only we are interested and will ignore others. Attributes required for a particular use case or context are well-defined attributes of an object.
Object can be anything tangible/intangible. Like Person object is a tangible object and his/her Bank account is intangible object.

Class:Class is way or concept on which objects can be classified. For example we can have a very large list on objects, person, animal, addresses, accounts, and so on.
All these object can be divided into different class where, person will be an object of class Person, and animal will be an object of class Animal etc.
We can specify object’s well defined behaviour and attributes in a class, and all object of same type will have class’s behaviour and attributes. Consider Person class. Person class can have multiple object like me, you, tom, krish, sharah, mike etc. and all these objects will be of same type and will have same attributes and behaviour. Yes of course values for these attributes can be different. Basically you can think class is a blueprint of Object.

Inheritance is a mechanism by which children inherit properties from its parents. In any programing language which support object oriented features, inheritance is an important feature for liskov substitution principle (Object Oriented Principle: we will discuss it in future posts) and code reusability.
Suppose we have a class B, created by extending class A. You can understand this like A is parent class and B is A’s child class. Some of the properties A has will be available to class B.
Inheritance can be supported in multiple ways.
Single Inheritance: When child class inherits from only a single class, then it’s single inheritance.

Multiple Inheritance: When child class inherits from more than one class, then its multiple inheritance.

Multi-Level Inheritance: When one child class B inherits from one or more class and some other child class inherits from class B, then its multi-level inheritance. You can understand this with the help of following image.

Java does not support all the above types of inheritances. Only Single and Multi-Level inheritance is available in Java. We will discuss why Java does not support Multiple Inheritance in few minutes.

What child class can inherit from a parent class? We are writing this tutorial for Java only, so we will restrict discussion to Java only. In java there are some rules that applied while inheritance is implemented. A child class can access or inherit only specific properties form its parent class. Java comes with access and non-access modifier for class’s properties. These modifiers help to control the accessibility of our code. We will discuss these modifier in our upcoming posts. For now you can just stick to their names.
A class can have members with private, public, protected access modifiers and default. If A is parent class of B, then only public, protected, and default members will be inherited.
public class A {
       private int a;
       public int b;
       protected int c;
       int d;

public class B extends A {

       public String toString() {
              return b + " " + c + " " + d;

Dirty Diamond Problem: If you have been a C++ developer, you may know that C++ support multiple inheritance. Means, one class can inherit from multiple classes. But there is a problem with multiple inheritance.

If you have a close look at above image. Class D is inheriting from B and C. B and C both are inheriting from A. Imagine A has a public method say display(). B and C both can provide different implementation for display() as per their requirement. Nothing in bad till now. But when we come to class D. D is inheriting from B and C. D will be able to receive implementation of display() from B and C both. When display() will be called on object of D, it will not be able to identify which class’s implementation should be called. To avoid this type of ambiguity Java decided not to go with multiple inheritance.
Note: Of course C++ or any other method has a work around to identify display() version to be called, but Java decided not to create this ambiguity.
Encapsulation: Encapsulation means binding all related things together and wrap it to keep it safe from external interaction. If we understand it in terms of Java. Class is an example. While creating a class, we keep all information related to a particular class and wrap it into one single Class. Whenever needed we get it from that class through an interface provided by that class. But only wrapping data into a class in not good practice for encapsulation.
public class A {
       private int id;
       public double salary;
       protected int age;
       double height;
Here in above code, we have public, protected, and default members. And these member can be accessed directly, like below.
public class C {
       public static void main(String[] agrs) {
              A a = new A();
              a.salary = 2000.00;
              a.age = -30;
              a.height = -40;
Any unrelated values can be re-assign to variables of class A from some third class C. Unrelated means, look at age and height values in above code. These are negative values. Neither age nor height can be negative. In class there no way to apply this rule.
Solution for this problem is,
a.       Keep all data members private, so that it can’t be accessed directly.
b.      Provide setter/getter for them to update and get values. Here in getter/setter you can apply any rule before setting values or before returning values to client. Look at below example.
public class MA {
       private int id;
       private double salary;
       private int age;

       private double height;

       public int getId() {
              return id;

       public void setId(int id) {
     = id;

       public double getSalary() {
              return salary;

       public void setSalary(double salary) {
              this.salary = salary;

       public int getAge() {
              return age;

       public void setAge(int age) {
              this.age = age;

       public double getHeight() {
              return height;

       public void setHeight(double height) {
              this.height = height;
Note: For a good encapsulated class, keep all your data members private and provide getter/setter for them.


Post a Comment