Object-Oriented Programming in Java
In this article, you will learn about the basics of Object-Oriented Programming in Java. Please read our previous section where we discussed the basics of Java, like Data Types, Literals, Operators, Variables, Keywords, Decision Making Statements, Looping Statements, Branching Statements, Methods, etc. At the end of this article, I hope you will understand the basics concepts of Object-Oriented Programming in Java.
Object-Oriented Programming in Java:
The main aim of object-oriented programming is to implement real-world entities. For example, object, classes, abstraction, encapsulation, inheritance, polymorphism, etc. Object-Oriented Programming is also popularly known as OOPs. The popular object-oriented languages are Java, C#, C++, etc.
Definition of OOPs :
OOPs, concepts in Java are the main ideas behind Java’s Object-Oriented Programming. They are Abstraction, Encapsulation, Inheritance, and Polymorphism. Grasping them is key to understanding how Java works. Basically, the Java OOPs concept lets us create working methods and variables, then re-use all or part of them without compromising security.
What is the Object-Oriented Programming Model?
The object-oriented programming model revolves around the concept of Object and Class.
What is an Object?
An object is an instance of a Class. Objects have states and behaviors. Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating. It contains properties and functions to perform some action. They are like real-world objects.
What is a Class?
The Class defines the blueprint of Objects from which you can create an individual object. For example, Laptop is a class and your laptop is an instance of it. It is a logical entity.
Difference Between Object-Oriented Programming (OOPs) and Procedural Oriented Programming (POP)
|In OOP, programs are divided into parts known as Objects.||In POP, programs are divided into Functions.|
|The main focus of OOP is on the data.||The main focus of POP is on the procedures.|
|It follows the bottom-up approach.||It follows the top-down approach.|
|It has access specifiers such as public, private, and protected.||In this most functions use global data.|
|It provides data hiding, data associated with the program. So data security is provided.||POP does not provide any data security.|
|Modification is easy.||Modification is difficult.|
|Examples: Java, Perl, C#||Examples: C, COBOL, etc.|
What are the OOPs Principles in Java?
OOPs, provide 4 principles. They are
The main aim of OOPs is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. These are some core OOPs Concepts in Java:
A class can be considered as a blueprint using which you can create as many objects as you like. The class is a group of similar entities. It is only a logical component and not a physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc.
The object is a bundle of data and its behavior (often known as methods). An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An object contains both the data and the function, which operates on the data. For example, chair, bike, marker, pen, table, car, etc.
Abstraction is the art of hiding implementation details from the user and provides the user with what they want. Abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics) while hiding away the implementation details. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like the steering wheel, Gears, accelerator, etc.
Wrapping data and methods within classes in combination with implementation hiding (through access control) is often called encapsulation. The result is a data type with characteristics and behaviors. Encapsulation essentially has both i.e. information hiding and implementation hiding. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding. For example, a capsule is wrapped with different medicines.
This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables. For example, to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes. It offers a robust and natural mechanism for organizing and structure any software. It provides code reusability. It is used to achieve runtime polymorphism. For example, let’s say we have an Employee class. Employee class has all common attributes and methods which all employees must have within the organization. There can be other specialized employees as well e.g. Manager. Managers are regular employees of the organization but, additionally, they have few more attributes over other employees e.g. they have reported or subordinates.
Note: Class and Objects are not OOPs concepts, rather class and objects are used to implement the OOPs concept in Java.
Applications of Object-Oriented Programming:
Applications of OOP are beginning to gain importance in many areas. The most popular application of object-oriented programming, up to now, has been in the area of user interface design such as the window.
- Simulations and Modeling: Simulation is the technique of representing real-world entities with the help of a computer
- Scripting: In recent years, OOP has also been used for developing HTML, XHTML, and XML documents for the Internet. Python, Ruby, and Java are the scripting languages based on object-oriented principles that are used for scripting.
- Real-Time system: Real-time systems inherent complexities that make it difficult to build them. Object-oriented techniques make it easier to handle those complexities by providing an integrated framework that includes schedulability analysis and behavioral specifications.
- Object-Oriented Database: These days OOP concepts have been introduced in database systems that are used to store the data directly in the form of objects.
- Automation Systems: These include formal as well as informal electronic systems such as Email, Web Calendars, etc. primarily concerned with information sharing and communication to and from people inside as well as outside the organization.
OOPs, SOLID Design Principles:
Design principles are generalized pieces of advice or proven good coding practices that are used as rules of thumb when making design choices. Some of the most important design principles in the object-oriented paradigm are :
Single Responsibility Principle (SRP):
It states that a class should have one and only one reason to be changed. If you put more than one functionality in one class in Java, it introduces coupling between two functionality, and even if you change one feature, there is a chance you broke coupled functionality, which requires another round of testing to avoid any surprise on the production environment.
Open Closed Design Principle:
It states that Software components should be open for extension, but closed for modification. It prevents someone from changing already tried and tested code. Ideally, if you are adding new functionality only then your code should be tested and that’s the goal of the Open Closed Design principle.
Liskov Substitution Principle (LSP):
According to the Liskov Substitution Principle(LSP), derived classes should be able to substitute their base classes without the behavior of your code changes. It means the methods or functions which use the superclass type must be able to work with the object of the subclass without any issue.
Interface Segregation Principle (ISP):
It states that the client should never be forced to depend on an interface they aren’t using in its entirety. This means that an interface should have a minimum set of methods necessary for the functionality it ensures and should be limited to only one functionality.
Don’t Repeat Yourself (DRY):
It means don’t write duplicate code, instead use Abstraction to abstract common things in one place. if you use a hardcoded value more than one time consider making it public final constant, if you have a block of code in more than two places consider making it a separate method.
Advantages of OOPs in Java:
- OOP provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly defined interface.
- OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
- OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.
- It is a secured development technique.
- Everything is treated as objects.
- It offers the reusability of classes. That is already created without writing them again and again.
- The programmers are written in the OOPS method. So easier to test, manage as well as maintain.
Disadvantages of OOPs in Java:
- Creating a program in the OOPS concept is a little bit difficult.
- The size of applications developed with OOPS is larger than the procedural way.
- The programmer should have proper planning before creating a program using the OOPS concept.
- If a programmer does not have extreme knowledge about programming, then it becomes difficult for them to code and manages.
- Due to the large size of the programs its execution speed becomes slow. Even many coding instructions make the execution of the program slower and affect its efficiency.
In the next article, I am going to discuss Class and Objects in Java with real-time examples. Here, in this article, I try to give you an overview of Object-Oriented Programming in Java. I hope you enjoy this article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.