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.
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…
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…
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.
This is the Diagrammatic illustration of the class’s characteristics explained with an example.
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.
This is a figure to describe Abstraction explained with an example.
In Java, abstraction can be achieved with the help of,
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.
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.
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:
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.
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-
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:
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.
The basic gist for not supporting multiple inheritances is to avoid ambiguity caused by it.
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.
There are two different types of polymorphism, namely:
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.
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.
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.
Thank you so much.
Your explanation is so clear to understand the basic concepts of object-oriented programing.