Last Updated: 12 November, 2023
Object-oriented programming (OOP) is a methodology of programming. Its main purpose is to deal with real-world entities using a programming language.
Object-oriented programming follows a bottom-up approach. It is highly secure because it supports OOPS (object-oriented programming) security features like access specifiers and data abstraction.
Object-oriented programming (OOP) is a widely used approach to programming, and many popular programming languages, such as Java, Python, and C++, support OOP.
The characteristics of object-oriented programming (OOP):
Class
Object
Polymorphism
Abstraction
Encapsulation
Inheritance
A class is a user-defined data type in Java from which objects are created. It represents the set of properties, methods, etc. that are common to all objects of one type. A single Java class can have one or more objects.
A class is a logical entity that does not occupy any memory. Memory is only allocated if we create the object of a class.
To create a class, use the keyword class.
Java Class Syntax:
The object is an instance of a class in Java. The object is a real-world entity that has states and behavior. When a class is instantiated (i.e., an object is created), memory is allocated for that object.
In Java, we can usually create an object using a new keyword.
An object has the following characteristics:
Object creation syntax:
For example, assume Student is a class name, and we are creating an object of the Student class.
Example: Java class and object creation
Output
Product Information
Product class - Default Constructor
Product Name: Pen
Price: 20.5
Polymorphism is one of the strong core features of object-oriented programming (OOP) that allows us to perform a single action in different ways.
Polymorphism is a combination of the two Greek words Poly and Morphs where "Ploy" means many and “Morphs” means forms. Therefore, polymorphism means “many forms”.
There are two types of Polymorphism in Java:
Compile-Time Polymorphism
We can achieve compile-time polymorphism using method overloading in Java, which means in the same Java class we write two or more methods with the same method name but different parameters.
This polymorphism is resolved during compilation time. We can achieve compile-time polymorphism through method overloading.
Compile-time polymorphism is also known as static polymorphism in Java.
Run-Time Polymorphism
We can achieve run-time polymorphism using method overriding in Java. Method overriding can be performed only in inheritance (the IS-A relationship), which means we declare the same method in both parent and child classes with the same signature.
"Method overriding" is the process of redefining a method in a child class that is already defined in the parent class. When both parent and child classes have the same method, then that method is said to be the overriding method. The method overriding enables the child class to change the implementation of the method acquired from the parent class according to its requirements.
This polymorphism is resolved during runtime and is achieved through method overriding. It is also known as "dynamic method dispatch."
Abstraction is one of the strong concepts of object-oriented programming (OOP).
Abstraction is a mechanism of hiding the implementation details and showing only functionality to the user. For example, we use mobile phones every day, but we know only the functionality of the phones but not their internal details.
There are two ways to achieve abstraction in Java:
📝 Using the Interface we can achieve 100% abstraction (complete abstraction) whereas through the abstract class we can achieve 0-100% abstraction (partial or complete abstraction) depending on the implementation of the abstract class.
Advantage of Abstraction:
Object-Oriented Programming (OOP) is based on four core concepts: abstraction, encapsulation, inheritance, and polymorphism.
In this article, we are going to discuss all about encapsulation in Java with examples.
Encapsulation is the mechanism of wrapping data (variables) and behavior (methods) together in a single unit (class).
The data of an encapsulated class will be hidden from other classes and cannot be accessed directly outside of it. It can only be accessed outside of the class through the public methods of its current class. Encapsulation helps achieve data hiding in Java.
An example of a fully encapsulated class is the Java Bean class.
Declare the variables of a class as private.
Other classes cannot directly access private variables.
Provide public setter and getter methods.
Using these public methods, private variables can be accessed or modified outside of the class.
Inheritance is a strong pillar of Object-Oriented Programming (OOP), and it provides a mechanism in Java for allowing access (to inherit) to all properties such as variables and methods (non-private properties only) from one class to another.
The extends keyword is used to implement the concept of inheritance in Java.
Inheritance Syntax in Java
There are five types of inheritance supported by Java.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com