Basic Concepts of Object Oriented Programming (JAVA)

Mathangi Krishnathasan
Analytics Vidhya
Published in
4 min readJan 5, 2021

As we all know , Computer can only understand binary numbers 0 and 1. But human couldn’t able to code using binary numbers since it is very difficult to remember binary format of each word. Therefore high-level languages are used by programmers to communicate with computers. These high level languages are translated into machine language using compilers.

High level languages are majorly divided into three categories. Such as, procedural language , functional language and object oriented language.

Procedural Programming Vs Object Oriented Programming

Procedural programming vs Object oriented programming

Procedural programming uses systematic order of statements, function and commands to perform operations on data . In other hand Object oriented programming uses class and objects which consists of properties and methods to perform operations.

In this article, we are going to discuss about basic concept of object oriented programming(OOP) using java.

Classes and objects are considered as major aspects of OOP. Classes are the blue prints where objects are created. Properties and methods are defined within the classes.

Define attributes and constructor

Moderators (Getter) and Accessors (Setter) methods are used to access private attributes for security purpose in industries as shown below. It is a fine example of data hiding (Encapsulation) will discussed later in this article .

Moderators (Getter) and Accessors (Setter) methods

Objects are created from the class using “New” keyword and assign values for the defined attributes using getter and setter method.

Creating objects from the class

So far, We learnt about the basic concepts of OOP and now we can go deeper into the OOP. Object Oriented Programming is built on four pillars. Such as Inheritance , Abstraction, Encapsulation and Polymorphism.

Inheritance

Inheritance is a class( child class ) inherits from another class (parent class).All the attributes and methods in the parent class can be accessed by child class . “extends” keyword is used to inherit child class from the parent class as shown below.

Usage of Extends keyword in inheritance

Encapsulation

Encapsulation is all about wrapping up data and methods in a single unit. Class in java is a simplest example of encapsulation.

Encapsulation

Abstraction

Abstraction is hiding the complex implementation from the user and provide user with simple interfaces to perform their operations.

Abstraction Vs Encapsulation

There is always a confusion about the difference between Abstraction and Encapsulation. Abstraction is used to hide complexity of the program from users and Encapsulation is used to hide the data member partially or fully by restricting the access to properties and methods using access modifiers. Access modifiers control the access to the data members.

Define variables in the class in private and use public getter and setter methods to access those variables is an example of encapsulation.

Polymorphism

Polymorphism is an object take different forms or shapes. There are two types of polymorphisms namely static polymorphism and dynamic polymorphism.

Types of Polymorphism

Static polymorphism is also called as compile time polymorphism. In this type of polymorphism, java calls the method at compile time by checking the signature of the method. Dynamic polymorphism is known as runtime polymorphism where overridden method is called at run time instead of compile time.

Method Overloading (Static polymorphism)

Method overloading means the different method having same method name and different parameters within the same class. Example code segment is shown here.

Overloading Example

Method Overriding (Dynamic polymorphism)

Method overriding means the different method having same method signature and different logic within the different class. Parent class method is override by child class. Example code segment is shown here using totalSalary method which consists of different logic in both classes .

In this article, we discussed about the fundamental concepts of OOP using Java.

Until Next Time, Happy Coding …

--

--

Mathangi Krishnathasan
Analytics Vidhya

BSc.(Hons.) in Information Technology,University of Moratuwa