Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Understanding the Features of Java

In this lesson, you will learn.

  • Features of Java

 

Features of Java

  • Java provides many powerful features that make it a popular and widely used programming language. Some of these features are shown below.

 

 

1. Simple

  • Java is simple and easy to learn as inherits most of its syntax and features from C and C++.
  • Best of all, if you are an experienced C++ programmer, moving to Java will require very little effort.

 

2. Object-Oriented

Object-Oriented Programming(OOP) is the core feature of Java incorporating the characteristics of OOPs such as Object, Class, Abstraction, Encapsulation, Inheritance, and polymorphism.

 

 

2.1 Object

  • An object is a real-time entity having some state and behavior.
  • An object is an instance of the class having variables like the state of the object and the methods as the behavior of the object.
  • Each object has a specific identity, characteristics, and behavior.

 

Real World Example: Object

  1. Ginger(dog name) is an Object. A dog has state (name, color, breed, hunger) and behavior (barking, fetching, wagging tail).

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).

 

 

2. Orange is an object.

Orange has a state (shape, color, etc.) and behavior (sweet and sour).

 

3. Bicycles is an Object.

It also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

 

2.2 Class

  • A class is a group of objects which have common properties.
  • It is a template or blueprint from which objects are created.
  • A grouping that represents a set of objects that share common characteristics and behavior.

 

Class vs Object

  • A class is a blueprint and an object is an instance.
  • A class is a logical entity and an object is a real-world or physical entity.

 

Examples

  1. Fruit is a class. Apple, Banana, and Kiwi are objects.
  2. A person is a class. Person names like James, Daisy, and Allen are objects of a class Person.

 

A class is a template for an object and is represented by a UML Diagram.

 

https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/

 

UML Diagram

UML stands for Unified Modeling Langauge. It is a graphical notation used to construct and visualize object-oriented systems.

 

UMLclassandobject

 

2.3 Abstraction

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.

 

Examples

1. While driving a car, you only focus on essential features like

  • Gear handling
  • Steering handling
  • Applying brakes
  • Acceleration and applying the clutch

 

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.

 

Source: https://in.pinterest.com/pin/10203536642764885/

 

2.4 Encapsulation

  • Binding up of data and methods (operates on data) into a single unit (class).
  • It keeps safe from outside reference and misuse.
  • Works like a protective wrapper that prevents data and methods.
  • Restricts direct access to some components of an object.

 

 

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.

 

Difference between Abstraction and Encapsulation

 

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.

 

2.5 Polymorphism

  • The word “polymorphism” comes from Greek and which means “many forms”.
  • Polymorphism is the ability for a message or data to be processed in more than one form.
  • This means that we can define or perform a single action in multiple ways.

 

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.

 

Source: edureka

 

2.6 Inheritance

  • Inheritance is the process in which one object or class acquires the properties and behavior of another class or object.
  • It supports the concept of classification.
  • Inheritance – IS-A relationship between a superclass and its subclasses.

 

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.

 

3. Robust

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.

 

4. Platform Independent

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.

 

5. Multithreaded

Java supports multithreaded programming, which allows you to write programs that do many things simultaneously.

 

6. Portable

The ability of a program to run on any platform without changing the source code of the program.

 

7. Distributed

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.

 

8. Secure

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.

 

9. Dynamic

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++.

 



 

End of the lesson….enjoy learning

 

Student Ratings and Reviews

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

Layer 1
Login Categories