Object Oriented Concepts in Java [Example and Code]

Object Oriented Concepts in Java [Example and Code]

As we have learned earlier, Java is an object-oriented programming (OOP) language and adheres to few main concepts of OOP. In this article, we will check out all the Object Oriented Concepts in Java explained with code and examples.

Table of Content:

In each topic, we are also covering some of the basic questions about Oops concepts.

Object-oriented concepts come with the main features of Java programming. Before we start learning these concepts, let us recap a little about OOP.

What is Object-Oriented Programming and Concepts?

Object-Oriented Programming is a method of programming where programmers define the type of data as well the operations that the data can perform.

In a nutshell, Object-Oriented Programming is a simple engineering advance to build software systems which models real-world entities using classes and objects.

So going further, the next question is…

What is an Object?

An object in terms of software programming is nothing but a real-world entity or an abstract concept which has an identity, state, and behavior.

For example: Let us consider a real-life entity Car.

Characteristics Examples
Identity Model Number, Model Name, License Plate/ Vehicle Number
State Speed, Gear, Engine temperature, Fuel level
Behaviour Fast, Slow, Change Of Gears, Break Down, Go Reverse, Etc.

Next…

What is a Class?

A class defines the structure of objects which include the state and behavior (operation).

For example: Let us consider a class named Bank_Account, whose state can be represented by variables like account_number, account_type. And the behavior can be represented by methods like amount_withdraw, amount_deposit, account_closure.

oops class concept example

This is the Diagrammatic illustration of the class’s characteristics explained with an example.

Principle Object Oriented Concepts in Java:

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

1. Abstraction

The process of highlighting the necessary and most concerned characteristics and hiding others is known as abstraction.

For instance, we can consider different kinds of cars and bus which have different engine type, shape, and purpose which make them distinctive from each other. Although all of them have some general features, i.e., gearbox, engine, tire, steering, brakes, etc.

So the general concept between them is that they are Vehicles. Therefore, we can display the feature common to both and exclude characteristics that are in particular concerned with car or bus.

Object-Oriented Abstraction Concepts

This is a figure to describe Abstraction explained with an example.

In Java, abstraction can be achieved with the help of,

  1. Abstract Class
  2. Interface

What is Abstract Class in Java?

A class when declared using the keyword abstract is an abstract class in java. An abstract class is extended by other classes and the methods should be implemented.

For example: From the above figure, the vehicle is an abstract class which is extended by SUV, Sedan, etc.

Sample code:

abstract class Vehicle{
  public void changeGear(){
    System.out.println(“Changing the gear”);
  }
}

class SUV extends Vehicle{
  public void changeGear(){
    System.out.println(“Gear has been changed.”);
  }

public static void main(String args[]){
  Vehicle vehicle = new SUV();
  vehicle.changeGear();
}

Output:
Gear has been changed.

What is the interface in Java?

An interface, in simple words, is a draft of a class which is used to achieve abstraction. Since an interface is similar to a class, it has variables and methods(which are by default abstract).

For more detail about this topic, do read  Java Program using Interface.

2. Encapsulation:

The practice of combining data and operations into a single entity is defined as encapsulation. In Java, encapsulation can be illustrated by making the data member of class private and declare getter and setter methods to make it accessible.

Encapsulation example:

Encapsulation example

3. Inheritance:

The process of reusing the properties of an existing class is defined as an inheritance.

The class whose properties are used inherited is considered the parent or superclass. And the inheriting class is the child or subclass.

Inheritance, in general, depicts the parent-child relationship in object-oriented programming, which is also called as the IS-A relationship.

How do we use inheritance in JAVA?

The keyword extends is used to display inheritance in Java.

The syntax to depict inheritance is as follows:

class Parent {
....
....
}

class Child extends Parent {
....
....
}

For example:

class Gadget {
.....
}

class Laptop extends Gadget {
.....
}

class Tablet extends Gadget {
.....
}

class Smartphone extends Gadget{
....
}

From the above example, we can say that-

  • Laptop IS-A Gadget, i.e., Gadget is the parent class and Laptop is the child class.
  • Tablet IS-A Gadget, i.e., Gadget is the parent class and Tablet is the child class.
  • Smartphone IS-A Gadget, i.e., Gadget is the parent class and Smartphone is the child class.

What are the different types of inheritance we can use in JAVA?

This is the most common question and asked in many placement interviews.

There are 3 types of inheritance in Java. The varied types of inheritance that can be used in Java are:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

If you have knowing object-oriented concepts in C++, Multiple Inheritance is missing in the list. This is simply because Java does not support it.

Why are multiple inheritances not supported by classes in Java?

The basic gist for not supporting multiple inheritances is to avoid ambiguity caused by it.

4. Polymorphism:

The word polymorphism is derived from 2 Greek words namely – ‘poly’, which means many and ‘morph’ which means forms.

Therefore, the word ‘Polymorphism’ means a single operation with multiple forms.

For example:

Let us consider we have a class called Coffee that has a method called ingredients(). The class Coffee is inherited by 2 subclasses Latte and Cappuccino which vary in using the generic method ingredients ().

So, here the action performed by ingredients() method is based on the type of object which uses it.

What are the types of polymorphism?

There are two different types of polymorphism, namely:

  1. Runtime Polymorphism: also known as dynamic method dispatch. The best example of this is method overriding.
  2. Compile-time Polymorphism: also known as static polymorphism. The best example of this is method overloading.

This is all about Object Oriented Concepts in Java. If you want to master Java programming, you should be knowing all Oops concepts in Java. Even if you are appearing for interview for Java development profile, you can find this article more useful. If you have any question, feel free to ask in the comment section.



4 Comments

  1. Well being a beginner in java its great to know about OOPs concept in detail. I was looking for each and every basic concept and so found your article very good. Thanks a lot for sharing this.

    1. Thank you so much ! Very pleased to know that the article could help you understand the basic object oriented programming concepts in a more better way.

Leave a Reply

Your email address will not be published. Required fields are marked *