Learn 5 OOP Concepts in Java [With Code Examples]

OOP Concepts has become an important part of software development. Writing Programs using Top down approach of traditional structured programming or Functional Programming method becomes difficult as the complexity of the program increases. This is where Object-Oriented Programming (OOP) comes in handy.

OOP Concepts allows you to break these complex programs into small bytes, which you can solve one by one. OOP is an effective model that allows your program to grow without becoming impossible to maintain. Add to it the ubiquity of languages like Java, and OOP Principles becomes need of the hour.

OOP is a whole new way of addressing problems. Here you create a class, instead of writing a program, which has variables and functions.

Objects are self-contained examples of that class and you have a control where you can make them interact with each other in many ways. This kind of encapsulation makes knowledge of Object-Oriented Programming and its main Principles a must if you are serious about working on big projects and developing software for mobiles.

There are many important features of Object-Oriented Programming. However, before proceeding further, we suggest you read Learn 7 Basics of Java Programming to Start Coding Today which helps you to know all the basic concepts of Java.

Our discussion in this blog follows this structure:

  • What are OOP concepts?

  • What are the best practices of OOP concept?

  • Advantages of Java OOP concepts

  • Differences between object oriented and object-based program.

Let us dive right into the main Principles of Object-Oriented Programming and discuss them with relevant examples.

What are OOP Concepts?

Object Oriented Programming is a method based on the concepts of “objects” which contain data and methods. Its main objective is to bring flexibility in programming while building small and large software applications. Most of the programming language like Java, C++, Python, C#, etc. follow OOP concepts.

Object-oriented concepts in Java

We will address the following OOP Concepts in Java:

  • Object

  • Class

  • What are all the differences between Object and Class?

  • Inheritance

  • Polymorphism

  • Encapsulation

  • Abstraction

  • Object

What is an Object? It is a commonly asked question. An object is an entity which has both state and behavior. An object can be defined as an instance of a class, which contains the address and takes up some space in the memory.

Object is an instance of a class

Some of the key points of objects are:

  • It has a state
  • It has a behavior
  • It has an identity.

Consider an example to understand what is an object:

Consider writing pen as an example. Here, pen is an object and its name is Reynolds, and color is white, known as its state. Pen is used to write is its behavior.

Pen is an example of an Object

A keyword “new” is used to create objects in Java.

Object is created with the following syntax:

className ReferenceVariable = new ClassName();

  • Class:

What is a Class? It is another commonly asked question by the community. A Class can be considered as blueprint using which many numbers of objects can be created. It is a group of objects which have common properties. Class contains variables and methods to describe the behavior of an object.

Consider a house as an example to understand what is class:

House is an example of an Object

Here, the house is the object. All the properties of the house such as floors, doors, window, etc. can be considered as the different classes.

Syntax to declare class is:

class
{
Field;
Methods;
}

A Class in Java contains:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface.

What is the difference between Object and Class?

Object and Class are the main concepts of Objecting Oriented Programming concepts. We will discuss some of the difference between these two concepts:

Object Class
Object is the instance of a class. Class is blueprint or a template from which objects will be created.
An object is self-contained component
which consists methods and properties which help in making a certain type of data useful.
A class is an entity which determines how the object will behave and what it contains.
An object can be created many times as per the requirement. A class is declared only once.
An object allocates memory whenever it is created. A class doesn’t take memory whenever it is created.

Once we have learned the Object and Class, then we will see further OOPs Concepts of Java.

  • Inheritance:

It can be defined as the process where one class acquires the properties of another class using a keyword called ‘extends’. Here, the class which inheritance the properties of other class is called ‘subclass’, and the class whose properties are inherited are called ‘superclass’.

Inheritance is mainly used for code reusability. So, here you are making use of existing classes and further extending on that. Let us look into a programming example of inheritance concept.

Let us look for a sample inheritance program.

package com.inheritance;
class Teamlead{
float salary=60000;
}
class Javaprogrammer extends Teamlead{
public static void main(String args[]){
Javaprogrammer p=new Javaprogrammer();
System.out.println(“Java programmer salary is:”+p.salary);
}
}

Output: Java programmer salary is: 60000.0

Different types of inheritance:

  1. Single inheritance: It is a type of inheritance in which one class inheritance the property of another class.

  2. Let us the see the syntax:

    Class A
    {
    —-
    }
    Class B extends A {
    —-
    }
    Single inheritance in Java


  3. Multilevel inheritance: In this type of inheritance one class is derived from a class which is also derived from another class i.e., a class is having more than one parent class but at different levels.

  4. Let us see the syntax:

    Class A {

    }
    Class B extends A{

    }
    Class C extends B{

    }

    Multiple inheritance in Java
  5. Hierarchical inheritance: It is a type of inheritance in which a class has more than one child class or in other words, more than one child class have the same parent class.

  6. Let us see the syntax:

    Class A {

    }
    Class B extends A {

    }
    Class C extends A {

    }

    Hierarchical inheritance in Java
  7. Hybrid inheritance:

It is a combination of multiple inheritance and multilevel inheritance.

Hybrid inheritance in Java
  • Polymorphism:

It is an Object-Oriented Programming feature which allows the programmer to perform a single action in many ways. The common use of polymorphism is when a parent class reference is used to refer a child class object.

Different types of polymorphism:

  • Static polymorphism: This is a type of polymorphism which gets resolved during compilation. It is also called compile time polymorphism.
  • Dynamic polymorphism: This is a type of polymorphism in which a call to an overridden method is resolved at runtime. So, it is called runtime polymorphism.
  • Encapsulation:

Encapsulation means binding object state and behavior together. It is a mechanism of wrapping the data (variable) and code acting on the data (methods) together as a single unit. It is a called as data hiding as variables of a class will be hidden from other classes and can be accessed only through the methods of their current class.

An encapsulated class can be created by making all the data members of the class private.

  • Abstraction:

Abstraction is a process of hiding the implementation details from the user, here only the functionality will be provided to the user. Abstract class contains ‘abstract’ keyword. Here, you show only relevant data and hide unnecessary details of an object from the user.

Let us discuss Abstraction with an example:

Consider an example wherein you are using your bank account online, to login you will enter your user_id and password and then press the login button, after pressing the login button you will not know how the input data is sent to the server and how it gets verified as all this information is abstracted away from you.

What are the best practices of OOP Concept?

The overall vision of implementing Object-Oriented Program is to develop a secure and more reliable software application. This requires following some proper guidelines while writing the programs which we will discuss below:

  • Single responsibility: One of the important best practices that every developer needs to follow is the “Single Responsibility Principle”. Here, in Object Oriented Programming, the class should always have one functionality so that it is called or extended whenever new usage arises without causing coupling between different functionalities

  • Open closed design: This principle says that keep all the methods and classes need to be closed for modifications but open for extensions. It means to create software entities whose behavior can be changed without editing and recompiling the code itself

  • Liskov Substitution Principle: This principle states that subtypes may be substitutable for their base types

  • Interface Segregation Principle: This principle states that a client should not implement an interface if it doesn’t use that

  • Dependency Inversion Principle: This principle states that high-level modules, which provides complex logic, should be easily reusable and they need to be unaffected by the changes in low level modules, which provides utility features. Abstraction can be used which decouples high-level and low-level modules from each other.

Advantages of Java OOP Concepts:

  • OOP Concepts reduces the complexity in coding and presents a good program structure
  • As per the requirements adding new features or responding to changing operating environments can be easily done by introducing a few new objects and precisely modifying the existing code without affecting the present code.
  • OOP introduces the reusability function as objects can be reused across applications which helps in faster development
  • OOP provides modular structure for programs wherein abstract datatypes can be defined which helps to hide the implementation details.

Differences between object oriented and object-based program:

There are few differences exists between Object-Oriented Programming and Object-Based Programming Language.

Object-Oriented Programming Concept Object-Based Programming Concept
In this type, all the principles of Object-Oriented Programming Concept are supported. In this type, some of the principles of Object-Oriented Concept such as inheritance and polymorphism are not supported.
This type of programming languages does not have a built-in object. This type of programming languages have built-in objects.

Conclusion:

In this Java blog, we have discussed some of the OOPs concepts like Object, Class, Inheritance, Polymorphism, Encapsulation, and Abstraction. Along with this, we have also seen the advantages of OOP Concept and many other topics.

Hope this helped our readers to understand why we need OOP Concepts. Let us know with which concept you will start your learning roadmap.

Here at Simpliv you can find vast library of free and paid online Java courses which can help you in this journey. Either way, let me know by leaving a comment right below and do share the post on social media if you have enjoyed reading it.

Recommended blogs for you

4 COMMENTS

  1. An fascinating discussion is value comment. I feel that you need to write extra on this matter, it might not be a taboo topic however usually persons are not sufficient to speak on such topics. To the next. Cheers

  2. Im not sure where you’re getting your info, but good topic. I must spend a while studying much more or understanding more. Thanks for fantastic info I was in search of this info for my mission.

  3. Hiya, I’m really glad I’ve found this info. Today bloggers publish only about gossip and internet stuff and this is actually irritating. A good website with interesting content, this is what I need. Thank you for making this web site, and I’ll be visiting again. Do you do newsletters by email?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Pin It on Pinterest