Master java skills

Object Oriented Programming

Java is an Object Oriented Programming language. OOP (an abbreviation for Object Oriented Programming) is a concept which is built around objects. All the programming is done around objects and the actual logic processing is done by the objects.

Java follows this concept of Object Oriented programming. Sometimes, it is asked whether Java is a fully Object Oriented Programming language or not. To answer this question, first, we need to understand what is a fully Object Oriented Programming language. A language which represents all kinds of data only using objects is a fully Object Oriented language. The answer to the question – whether Java is fully object oriented or not – is therefore ‘NO’, because some of the data is also represented by primitive variables. Primitive data types like int, boolean are not objects, and that’s why Java is not fully OOP language.

OOP concepts

There are four concepts of Object Oriented Programming:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

Encapsulation

The literal meaning of encapsulation is encasing something in (as if in a capsule). Like medicines are encased in a capsule so that it remain intact and protected. Similarly, in Object oriented programming, data needs to be protected and encased within an entity. That entity, in Java, is known as class. Class helps us implement encapsulation in Java.

Abstraction

Abstraction means hiding the complexity and giving a rather easy interface to the end user of a product. Let’s take an example of an ATM machine. It’s a huge machine with a lot of processing that goes inside it. But for the end user, there is only a small interface and that is the screen of the ATM. User just need to insert his/her card, enter the correct pin and then choose from the available services. What algorithm is applied inside the machine and what all processing happens inside it, the user is not really bothered. He is only concerned with the output of his transaction. If he has chosen withdrawl of some money, he should get it.

This is an example of abstration. The entire working of the machine has been abstracted from the user and an easy interface (touch screen) has been provided to him to do his transactions. In java, abstraction is achieved by two ways: Interfaces and abstract classes.

Inheritance

Inheritance is the action of inheriting some or all the properties from one’s ancestors. Human beings inherit properties from their forefathers, like one may inherit basic nature of his/her father. Another person may inherit features of his mother. Some people might inherit some other properties from their father’s father and so on. This is inheritance.

In java, inheritance is achieved using parent-child class relationship and interface-(implementing class) relationship. Below it is explained using diagrams:

Inheritance examples

In the first example, Class A is the parent class of class B. In the second example, Class A is the parent class of class B, and class B is parent class of class C. Class A is also parent class of class C, though indirectly.

In the third example, class B, C, and D are the child classes of class A. Using the inheritance principle, all the child classes will inherit all public properties and behaviour of the parent class. Thus, in the seond example, class C will inheirt public properties of its parent class B as well as indirect parent class A.

We will learn about inheritance in detail in separate chapter.

Polymorphism

Polymorphism means occurence of many forms in a compound or in a family of living beings. For example, diamond and graphite are two forms of carbon. Therefore, diamond and graphite are called polymorphs of carbon. And this phenomenon is known as polymorphism.

Another real world example is, an idividual can have multiple forms in her life. Such as a woman is a mother at home, she takes form of an employee in an organiztion, she takes form of a responsible citizen of the country at public places and so on. So, the individual is same but in different conditions she takes different forms.

In java, polymorphism is implemented using two ways: Method Overloading and Method Overriding.

To learn more about Method Overloading and Method Overriding, click here.