top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Object-Oriented Programming Concepts

Introduction

  • Java is an object-oriented programming (OOP) and is useful in handling real world entity using a programming language.

  • The goal of OOP principles is to increase the readability and reuse of Java code by outlining the best ways to organize it.

Procedural V/s Object-Oriented Programming

  • Procedural programming contains a group of functions and data on which these function work.

  • As the complexity increases, there will be interdependency between these functions and becomes problematic.

  • Object Oriented Programming solves this problem.

Procedural programming

Object Oriented Programming

Top down approach

Bottom up approach

Does not have Access Modifiers

Has Access Modifiers

Divided into functions

Divided into Objects

Data can move freely from function to function

Objects can move and communication with each other through member functions.

Less Secure

More Secure


What are OOP concepts in Java?

The focus of object-oriented programming is on objects made up of both data fields and code properties or attributes. Instead of writing each operation from scratch every time, programmers can use OOP to construct classes that iteratively reuse code by sending the same set of "instructions" to several objects. This works especially well for sizable team projects when states change frequently.

Together with other well-known languages like Python and C++, Java offers object-oriented programming. OOP principles in Java enable us to design specific interactions between Java objects. They enable code reuse without putting security at risk or degrading performance and readability.


Pillers of OOPs Concepts

Object-oriented Java programming is based on four principles.

  • Class

  • Object

  • Encapsulation

  • Inheritance

  • Abstraction

  • Polymorphism

Abstraction


Abstraction is the process of hiding the implementation details and showing only functionality to the user.

Abstraction lets you focus on what the object does instead of how it does it. For example, if you’re driving a car, you don’t need to know about its internal workings.

The same is true of Java classes. You can hide internal implementation details using abstract classes or interfaces.


Abstraction using Abstract Class

  • Abstract Class in Java is a mechanism to achieve Abstraction.

  • It is declared with 'abstract' Keyword.

  • It can have abstract and non-abstract methods (method with body)

  • It cannot be instantiated.

  • It can have constructors and static methods.

  • partial abstraction (0-100%) can be achieved with abstract classes.


In the code example below, we create an abstract class called Shape with one abstract method.


Abstraction using an Interface.

  • An Interface in Java is a blueprint of a Class.

  • It is declared by using the 'interface ' keyword.

  • An interface is a 100% abstract class.

  • It can only have abstract methods and cannot be instantiated.

  • It provides total Abstraction-all methods are abstract, and all the fields are public, static and final by default.

  • A class that implements an interface must implement all the methods declared in the Interface.

  • Java interface represents the Is-A relationship.

  • It used to achieve Abstraction and multiple inheritance.

In the code example below, we create an Interface called Drawable with one abstract method.




2. Encapsulation

Encapsulation is the process of wrapping/binding data with methods. Encapsulation is a way to achieve 'Data Hiding'. As the name suggests, it safeguards the inner contents of a class like a capsule. We can implement encapsulation in java by means of making the class variables private l and getting access to them via their public getter and setter methods.


Encapsulation in Java:

  • Restricts direct access to data members of a class.

  • Fields are set to private.

  • Every subject has a getter and setter method.

  • Getter methods go back the field.

  • setter strategies allow us to exchange the value of the filed.



3. Inheritance

Inheritance refers to the process by which an Object of one class acquires properties of an Object of other Class. . The child class can override the values and methods of the parent class, but it’s not necessary. Moreover, child can add new methods and fields in your current class also.

The Class whose properties are extended is known as super Class, Base Class or Parent class. The Class that inherits the properties is known as the child class. Java uses the extends keyword to implement the principle of inheritance in code.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).

  • For Code Reusability.

Terms used in Inheritance.

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.

  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.

  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.

  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

  • Multi-level inheritance is allowed in Java (a child class can have its own child class as well)

  • Multiple inheritances are not allowed in Java (a class can’t extend more than one class)

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.


Types of inheritance in java

There can be three types of inheritance in java: single, multilevel and hierarchical.

Single Inheritance

When a class inherits another class, it is known as a single inheritance.

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance.

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance.










In Java programming, multiple and hybrid inheritance is supported through interface only. When one class inherits multiple classes, it is known as multiple inheritance.












4. Polymorphism

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

Polymorphism in Java:

  • The same method name is used several times.

  • Different methods of the same name can be called from an object.

  • All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of the Object class)

  • Static polymorphism in Java is implemented by method overloading.

  • Dynamic polymorphism in Java is implemented by method overriding.

Static Polymorphism in Java

Method overloading means that you can have several methods with the same name within a class. However, the number, names, or types of their parameters need to be different.




Dynamic polymorphism (method overriding)

Using the method overriding feature of Java, you can override the methods of a parent class from its child class.



Conclusion

In this blog, we have learned about the basics of OOPs concepts in java. Object-oriented programming is a model that provides different types of concepts, such as inheritance, abstraction, polymorphism, etc.

OOP concepts in Java help you to structure your program more efficiently.

Thanks for reading!


127 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page