Programming Object Oriented Programming OOP | JAVA the Core Concept

Object Oriented Programming OOP | JAVA the Core Concept


Object Oriented Programming OOP - JAVA

Object Oriented Programming OOP

In Object Oriented Programming OOP, programs are split apart into self contained objects. Almost like several mini programs. Each object is responsible for different tasks of the application. Each object has its own properties and methods, and they communicate themselves. Object Oriented Programming generally stands on four core concepts. those are:

⇒ Encapsulation, ⇒ Abstraction, ⇒ Inheritance, ⇒ Polymorphism

Before OOP, the programming language was straight Procedural language which means, the program consists of set of functions and variables. In that case we stored data in the variables and functions worked with the data. So, as the program grow, it takes bunch of functions all over the places. Here the problem is, making change to one function cause several other functions break. This type of code is called spaghetti code. Because there are so much inter-dependencies between all these function which becomes problematic. To solve this problem the concept of Object Oriented Programming OOP comes in place.

Remember that, Object Oriented is not any programming language, rather it is a way or style of programming.

Now lets understand the core concept of Object Oriented Programming OOP:

» Encapsulation

In OOP, we combine groups of related variables and functions into a unit called Object. Variable refers to as property or field of the object and functions are called method.

Object Oriented Programming OOPFor Example, think of a car as an Object. It has properties like model, color, make and functions like start(), move(), stop(). In Object Oriented Programming, grouping related variables and functions into a Class or Object is called Encapsulation. An example of encapsulation is the Account{} Class:

public class Account {
private int account_number;
private int account_balance;

public void userData(){
//user data will go here

public void deposit(int a){
//code goes here.

Suppose a hacker is able to gain access to the code, and tries to deposit invalid amount.

class Hacker{
Account a = new Account();
//he also tries the method bellow

As fields of Account class declared private, therefore these fields are only accessible within the Account class itself, and his a.deposit(-100) method is also invalid for the code below in deposit() method:

public void deposit(int a){
//through an error

Thus the Application data will never expose to external party. Hence grouping fields and methods like in a capsule is called encapsulation.

Instance Variables are the fields that an Object knows about itself. It represent an Object state (the data), and can have unique values for each Object of that type. Methods are functions an Object can do.

So, Objects have instance variables and Methods, but those instance variables and methods are designed as part of the Class. Lets get to know a little about Classes.

Class vs Object

In Object Oriented Programming OOP, an Application consists of multiple classes, each responsible for particular behavior in Application. It is a blueprint or logical definition. It tells JVM how to make an Object of that particular type, A Class is not an Object but it is used to construct them. A class mainly has two parts, field or attributes and methods or functions. Let see in Action, How a Class is built in JAVA and how an Object is constructed from Class.

Object Oriented Programming - JAVA Class

public class biCycle {
// the biCycle class has
// three fields
public String name;
public int gear;
public int speed;
// the Bicycle class has
// three methods
public void setGear() {
gear = newValue;
public void applyBrake() {
speed -= decrement;
public void speedUp(int increment) {
speed += increment;

To run the Bicycle Class we need to build another Class that holds the main function, and in that main function we will create an Object to access its variables or fields and methods.

//new Class called TestCode
public class TestCode {
public static void main (String[] args) {
biCycle MybiCycle = new biCycle();//Object is declared named MybiCycle. = "Trek Bicycle";

» Inheritance

Inheritance is another cool characteristics of Object Oriented Programming OOP. It means a sub-Class or child-class inherits properties  and behaviors from parent-class or super-class. The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a super-class (also a base class or a parent class).

Suppose we have a parent-class called Doctor{}

public class Doctor{
boolean workAtHospital; //has one instance variable
//first method of Doctor class
void treatPatient(){
//doing checkups and prescribe

A class declaration for a FamilyDoctor class is a sub-class or child class of Doctor might look like this:

public class FamilyDoctor extends Doctor {
boolean makesHouseCalls; // accepts new instance variable
//has new method
void giveAdvice(){
//give general advice

Here FamilyDoctor{} is a sub-class or child class of Doctor{} class, and inherits all fields and methods from Doctor{} class beside it has its own fields and methods.

Object Oriented Programming OOP - Class Type

Another sub-class Sergeon{} of parent Doctor{} class:

public class Surgeon extends Doctor {
//overrides the inherited treatPatient() method
void treatPatient(){
//performs surgery
//has a new method
void makeIncision(){
// make incision

» Polymorphism

Poly means many and morphism means form, so Polymorphism mean many forms. This principle can also be applied to Object Oriented Programming and languages like the Java. Sub-classes of a super-class can define their own unique behaviors and yet share some of the same functionality of the parent-class. In above example of inheritance, both FamilyDoctor{} and Surgeon{} are the sub-classes of the parent Doctor{} class.

To understand the Polymorphism, we need to look at the way we normally declare a reference variable and create an Object.

[java]Surgeon refVar = new Surgeon();[/java]

Here, refVar is a reference variable which data-type is Surgeon type. new Surgeon() is an Object for the Surgeon{} class, which is assign to the reference Variable refVar.

As we can see that the reference type and the object type are the same, both are Surgeon type. The reference variable type is declared as Surgeon and Object is created as new Surgeon();

But with Polymorphism, the reference type can be super-class of actual Object:

[java]Doctor refVar = new Surgeon();[/java]

In other words, anythings that extends the declared reference variable type can be assign to the reference variable. This lets you do things like make polymorphic arrays. Consider the example bellow:

[java]public class TestCode{
public static void main(String[] args){
Doctor[] refVar = new Doctor[2];
refVar [0] = new FamilyDoctor();
refVar [1] = new Surgeon();
for(int i = 0; i<refVar.length; i++){

So, when ‘i’ is 0, treatPatient() will work for FamilyDoctor’s treatPatient() method, and when i is 1, it will work for Surgeon’s treatPatient() method. Here reference Variable refVar has many forms.

So Polymorphism is, JVM is smart enough to find any method or field both exists in super-class and sub-class and implements the method or field which exists in super-class and for over riding method it implements the new one. But JVM can not find or implement any method or field which is only in the sub-class but not in super-class.

» Abstraction

One of the core concept of Object Oriented Programming OOP, It is the concept of hiding or abstracting the implementation details of the abstract method and keeps only the declaration. In JAVA Programming, abstraction includes interfaces, abstract methods and abstract classes.

An abstract class is a class that is declared using the keyword “abstract”—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub-classed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

[java]public abstract listPrice();//this is a abstract method, no implementation[/java]

If a class includes abstract methods, then the class itself must be declared abstract, as in:

[java]public abstract class Sellable {
// declare fields
// declare nonabstract methods
public abstract int listPrice(); //no body or implementation for abstract method
public abstract int lowestPrice();

Each non-abstract sub-class of Sellable{}, must provide implementations for the listPrice() and lowestPrice() methods:

[java]public class Photograph extends Sellable {
private String descript;
private int price;

public int listPrice(){
return price; }
public int lowestPrice(){
return price/2; }


are similar to Abstract classes. We cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, we can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Definition of Interface:

[java]public interface Sellable {
public String description();
public int listPrice();
public int lowestPrice();

Photograph{} Class implementing the Sellable interface.

[java]public class Photograph implements Sellable {
private String descript;
private int price; 
private boolean color; // true if photo is in color

public Photograph(String desc, int p, boolean c) {   // constructor
descript = desc;
price = p;
color = c;}

public String description() { return descript; }
public int listPrice() { return price; }
public int lowestPrice() { return price/2; }
public boolean isColor() { return color; }

Don’t Forget to subscribe our site, which will reach you the recent article first!


Please enter your comment!
Please enter your name here

Latest Articles

Property Decorator | Getters Setters and Deleters in Python

In this article, we will talk about the Property Decorator in Python. It enables the class functionality...

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

You might also likeRELATED
Recommended to you