Java OOPs Concepts With Examples

In this article, we will learn about basics of Java OOPs Concepts With Examples. Here we introduce you to Object, Classes, Encapsulation, Abstraction, Inheritance, and Polymorphism.

What do you mean by OOPS?

Object-oriented programming (OOPs) is a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attribute and code, in the form of procedures, often known as methods. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.

Examples of object-oriented languages are Python, C++, Objective-C, Smalltalk, Delphi, Java, Swift, C#, Perl, Ruby, and PHP.

Java OOPs Concepts With Examples

Object

Object means a real word entity that has state and behavior. Object are key to understand in object-oriented technology and the real world examples are your dog, your desk, your television set, your bicycle. Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Real-world objects share two characteristics: Dogs have state (name, color, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence) and behavior (changing gear, changing pedal cadence, applying brakes).

Classes

A class is nothing but prototype (Blueprint) from which objects are created.

Class Template.
  • When we create a class in java the first step is keyword class and the name of the class or identifier we can say
  • Next is a class body which starts with curly braces {} and between this, all things related to that class means their property and method will come here.

A class can contain any of the variables.

  • Local Variables
  • Instance Variables
  • Class Variables

Encapsulation

Encapsulation is one of the Java OOPs Concepts. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation. A primary benefit of encapsulation is better maintainability.

When we say data hiding, we all know about “access specifiers” in Java. Hiding an attribute or method is easier from the outer world. Just use the access specifier ‘private’. It should not be misunderstood that encapsulation is all about data hiding only. When we say encapsulation, emphasis should be on grouping or packaging or be bundling related data and behavior together.

Abstraction

Abstraction is one of the important Java OOPs concepts. Abstraction is a process of hiding the implementation and showing only functionality to the user. Abstraction can be achieved in two ways, these are an Abstract class (0 to 100%) and interface (100%). Abstraction lets you focus on what the object does instead of how it does it.

The best example for abstraction is sending SMS. When we are sending an SMS we only type the text and click the button to send but we don’t the internal processing of sending the SMS.

Inheritance

Inheritance is one of the important Java OOPs concepts. Inheritance is one of the key features of object-oriented programming. Inheritance is nothing but acquiring property on one class to another class. In Java, two important relation exists those are “IS-A” and “HAS-A” relation. Inheritance follows “IS-A” relation between superclass and its subclass.

Inheritance can be achieved by using Extends and Implements keywords. In Java, multiple inheritance is not possible. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language.

Polymorphism

Polymorphism is widely used in Java and other programming languages. As Wikipedia definition “Polymorphism is a provision of a single interface to entities of a different type.”

In Simple words, Polymorphism is nothing but changing the behavior of object throughout the execution of the program. In other words, Polymorphism means many forms.

Types of Polymorphism.

  • Compile time polymorphism(Method overloading)
  • Runtime polymorphism(Method overriding)

We can achieve Polymorphism by method overloading and method overriding.

The key features of object-oriented programming are:
  • Compile time polymorphism(Method overloading) -The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • Information-hiding – By interacting only with an object’s methods, the details of its internal implementation remain hidden from the outside world.
  • Code re-use – If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
  • Pluggability and debugging ease – If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine

Related Posts:-

Leave a Reply

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

Show Buttons
Hide Buttons