Object Oriented Programming(OOPs concept) for beginners:

Sneha Sethi
5 min readDec 27, 2020

--

Object Oriented Programming is a programming paradigm that organizes a program around its data(that is objects).An object oriented programming uses objects and classes or organising the program around its data effectively.But the question is how it manages its data?What is the approach to manage the code and data?And why is this so popular? And the answer is an Object Oriented Programming paradigm implements these concepts Polymorphism,Inheritance,Abstraction and Encapsulation.But before going to OOPs concepts we need to know more about classes and objects.

Object Oriented Programming Model

Classes and Objects:

Class is a blueprint from which objects are created.Basically a class defines the structure and behaviour of data and code that is shared by a set of objects.

An Object can be any physical entity or you can say things you can see or feel like pen,paper,car etc all are objects.But what is an object in java? In java objects are called instances of class.An object is a physical entity whereas class is a logical entity.For now you can think of a Class defines a new data type which can be used for creating objects of that type.

In real world objects have some state and behaviour. Eg. dog - A dog has some attributes such as breed,color and also has some certain behavioural aspects such as sleep,breathe etc.object consists of :

1. Identity- Object declared by a unique name by which it interacts with other objects.
2. State- State represents the attributes of the object.
3. Behaviours- Behaviour represents the methods used by the object.

1.Polymorphism:

Polymorphism mean Many forms, it allows us to use one code in different ways.We can define one interface as a general class of actions and have multiple implementations.Let’s understand Polymorphism with the following code:

Overloading(Compile time Polymorphism)
Output of the program

Have you noticed this piece of code.As shown in this there are three functions of same name but with different parameters. Polymorphism has the ability to differentiate between entities with same name efficiently.These methods can be overloaded by specifying the data type of arguments and by changing the number of arguments.And this method is also called Method Overloading which is a type of Polymorphism.

2.Inheritance:

Inheritance is a biological term which defines the transmission of traits or information from one generation of individuals or cells to the next generation.The same concept is also used by OOPs in which a child class can use the properties and methods of it’s parent class by using this concept which is called Inheritance.A class can be inherited by using extends keyword.The aim of implementing this concept is Reusability of code.By inheritance we can use the same methods which we have already written in some other class.These are the basic terms of Inheritance:

1.Super Class:The parent class is called super class whose features are inherited to child class.

2.Sub Class: The child class which inherits the other class is called sub class which can add its own methods also and can also access the methods of super class.

Let’s understand working of Inheritance by the following code:

Output for the above program

As we have noticed in the code is that inheritance provides us multilevel inheritance as I have created the object for parrot class and used the same object for accessing methods of both super classes for parrot class.So that’s how we can achieve multilevel inheritance.If I have inherited only Sparrow class then it will be called singlelevel inheritance.

3.Abstraction:

Abstraction is also an interesting feature of OOPs which allows us to display only essential details to user.

let’s understand this concept with the help of an example of car,As a car user you know how to drive the car when to press the breaks,when to use which gears,but you don’t know about working of gears,about the working of engine or about how all the parts are assembled and these things are not necessary for you to drive a car.It is called abstraction you think of a car is a single object,you don’t think it in terms of it’s complexity.

Abstraction can also be applied on computer programs.With the help of abstraction complex programs can be transformed into the concrete objects.And these concrete objects can be used then accordingly.

Output for the above Program

As we have seen in the program is that we have not declared any object for abstract class because it is not possible to instantiate an abstract class.We can not declare abstract constructors and abstract static methods.As we have seen area method for Class B is working fine because it is subclass of abstract class.Any subclass of an abstract class must either implement all of the abstract methods in the super class or be declared abstract itself.

4.Encapsulation:

Encapsulation is a protective wrapper that prevents our code and data from outer interference.It prevents our code to be accessed by the other code outside the wrapper.We can relate this with the same example of car user,as a car user you can drive the car but you can't change the functionality of gear because gear-shift lever is a well-defined interface to the transmission.further what occurs inside the transmission doesn’t affect other objects outside the transmission.for example shifting gears does not turn on headlights!.

But What is the purpose of Encapsulation?

The purpose of encapsulating a class is hiding the complexity of the implementation inside the class.And also it provides the flexibility to the programmer to change one part of code without affecting the other parts.

Let’s understand how to achieve Encapsulation.

Output for the above program

As we have seen in the program Encapsulation is achieved by:

  1. Declare Class variables as Private.
  2. Provide getter and setter method to access and update the value of private variable.

--

--