In this lesson, you will learn.
Object-Oriented Programming(OOP) is the core feature of Java incorporating the characteristics of OOPs such as Object, Class, Abstraction, Encapsulation, Inheritance, and polymorphism.
Apart from the Dog, Chair, Bike, Marker, Pens, Table, Car, Book, Apple, Bag, etc examples of object.
Objects can be physical or logical (tangible and intangible).
Orange has a state (shape, color, etc.) and behavior (sweet and sour).
It also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).
UML stands for Unified Modeling Langauge. It is a graphical notation used to construct and visualize object-oriented systems.
Abstraction means hiding lower-level details and exposing only the essential and relevant details to the users.
Abstraction refers to the act of representing essential features without including background details or explanations.
1. While driving a car, you only focus on essential features like
Rather than knowing the internal reality or details like the working of the engine, internal wiring, etc.
2. The second example is an ATM Machine.
We perform different operations like Cash withdrawal, money transfer, deposit money, printing mini-statements, etc but you don’t know the internal process.
Note: In encapsulation, we declare a field as private in the class to prevent other class access and encapsulated data can be accessed using the public setter/getter method.
Feature | Abstraction | Encapsulation |
---|---|---|
Definition | It hides the complex reality while showing the necessary parts. It focuses on what the object does. | It bundles the data (variables) and code acting on the data (methods) together as a single unit or class. It restricts direct access to some of the object’s components. |
Purpose | To hide the complexity by showing only the necessary details to the user. | To protect the integrity of an object’s data by making attributes private and providing public methods to access and modify them. |
Implementation | Achieved in Java through abstract classes and interfaces. | Achieved using access modifiers (private, protected, public) with classes, methods, and attributes. |
Example | In a Shape class with a draw() method. The specific details of drawing a shape are hidden, while the interface (draw method) is exposed. |
A Person class where attributes like name and age are private but can be accessed and modified through public methods like getName() and setName(). |
Use Cases | Useful in situations where the implementation may change, and these changes should not affect the other parts of the software. | Useful for controlling how data is accessed and modified, and keeping the internal representation of an object hidden from the outside. |
Note: Abstraction solves the problem at the design level whereas Encapsulation solves the problem at the implementation level.
Example
Imagine a person who can assume different roles depending on the situation, much like an object in Java can take on many forms.
Think of a person named Alex. Alex is a person, but at different times, he can be a father, an employee, a teacher, a driver, or a cook. Each of these roles exhibits different behaviors and skills.
Example
inheritance in Java is like this vehicle analogy where a Vehicle is defined and then extended into more specific categories (Cars, Motorcycles, etc.) with added characteristics or modified behaviors, while still retaining the essence of the original concept.
Java provides features like memory management and exception handling etc., which ensure that Java applications don’t crash if minor errors are encountered.
Memory management is a difficult and tedious task. When a program creates an object, it needs to allocate memory and destroy the object after use, Java will complete the task automatically.
Java provides automatic garbage collection for unused objects. In other programming languages like C and C++, garbage collection is done by programmers not automatically which is a time-consuming task.
Exceptions are runtime errors and often arise in situations such as “division by zero” or “file not found,” due to the program terminating abruptly and providing the wrong results.
Java helps in this area by providing an object-oriented exception-handling mechanism.
Platform independence in Java means “write once, run anywhere” which notifies that you can run Java code in any operating system (Windows, Mac, Unix/Linux, or Android) that supports Java without recompilation.
In Java, the compiler converts a program into platform-independent code called byte-code, which can run on any computer installed with a JVM.
Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM).
To run a Java program in different environments the JVM needs to be implemented for each platform.
Remember, although the details of the JVM will differ from platform to platform, all understand the same Java bytecode.
Java supports multithreaded programming, which allows you to write programs that do many things simultaneously.
The ability of a program to run on any platform without changing the source code of the program.
Java applications can be distributed on multiple machines in a network, such as the Internet because it handles TCP/IP protocols.
These applications can be used by multiple users from multiple machines in a network.
Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network.
Java program executed by the JVM also helps to make it secure. Because the JVM is in control, it can contain the program and prevent it from unauthorized access.
It can adapt to an evolving environment that supports dynamic memory allocation due to which memory wastage is reduced and the performance of the application is increased.
In the next lesson, you will learn about the difference between Java and C++.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.