• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Object Oriented Concepts in Java [Example and Code]

Pranati Paidipati/45114/3
CodeJAVA

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:

  • Object-Oriented Programming
    • Object
    • Class
  • Principle Object Oriented Concepts in Java
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism

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.


« Java TutorialClass and Object in Java »

JavaJava Opps Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    pranit
    July 25, 2018 at 10:14 am

    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.

    • Reply
      Pranati
      July 25, 2018 at 11:58 am

      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.

  • Reply
    Tayyaba Fatima
    July 5, 2019 at 4:11 am

    Thank you so much.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Exception Handling
  11. Java- ‘throw’ vs ‘throws’ Keyword
  12. Java- RegEx
  13. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- HashSet
  5. Java- HashMap vs HashSet
  6. Java- Reverse Linked List

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard