0% found this document useful (0 votes)
12 views

Some More Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Some More Java

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Features of Java

1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
3.Platform Independent
Java is platform ndependent because it is different from other languages like C, C++, etc. which are
compiled into platform specific machines while Java is a write once, run anywhere language. A
platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-
based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform
that runs on top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS,
etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-
independent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere
(WORA).

4. Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't
require any implementation

5.Robust
The English mining of Robust is strong. Java is robust because:
o It uses strong memory management.
o There is a lack of pointers that avoids security problems.
o Java provides automatic garbage collection which runs on the Java Virtual Machine to get rid
of objects which are not being used by a Java application anymore.
o There are exception handling and the type checking mechanism in Java. All these points
make Java robust.

6.Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded
on demand.

7. Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:
o No explicit pointer
o Java Programs run inside a virtual machine sandbox
o Classloader: Classloader in Java is a part of the Java Runtime Environment (JRE) which is used
to load Java classes into the Java Virtual Machine dynamically. It adds security by separating
the package for the classes of the local file system from those that are imported from
network sources.
o Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights
to objects.
o Security Manager: It determines what resources a class can access such as reading and
writing to the local disk.
Java language provides these securities by default. Some security can also be provided by an
application developer explicitly through SSL, JAAS, Cryptography, etc.

What is Object Oriented Programming (OOP)?


Object Oriented Programming (OOP) is a programming paradigm that focuses on the use of objects
to represent and manipulate data. In OOP, data is encapsulated within objects, and objects are
defined by their properties (attributes) and behaviors (methods). OOP provides several key concepts
that enable developers to write modular, reusable, and maintainable code.
Definition of OOP Concepts in Java
The main ideas behind Java’s Object-Oriented Programming, OOP concepts
include abstraction, encapsulation, inheritance and polymorphism. Basically, Java OOP concepts let
us create working methods and variables, then re-use all or part of them without compromising
security. Grasping OOP concepts is key to understanding how Java works.
Java defines OOP concepts as follows:
 Abstraction. Using simple things to represent complexity. We all know how to turn the TV
on, but we don’t need to know how it works in order to enjoy it. In Java, abstraction means
simple things like objects, classes and variables represent more complex underlying code
and data. This is important because it lets you avoid repeating the same work multiple times.
 Encapsulation. The practice of keeping fields within a class private, then providing access to
those fields via public methods. Encapsulation is a protective barrier that keeps the data and
code safe within the class itself. We can then reuse objects like code components or variables
without allowing open access to the data system-wide.
 Inheritance. A special feature of Object-Oriented Programming in Java, Inheritance lets
programmers create new classes that share some of the attributes of existing classes. Using
Inheritance lets us build on previous work without reinventing the wheel.
 Polymorphism. Allows programmers to use the same word in Java to mean different things
in different contexts. One form of polymorphism is method overloading. That’s when the
code itself implies different meanings. The other form is method overriding. That’s when the
values of the supplied variables imply different meanings. Let’s delve a little further.
How OOP Concepts in Java Work
OOP concepts in Java work by letting programmers create components that are reusable in different
ways while maintaining security.
How Abstraction Works
Abstraction lets programmers create useful and reusable tools. It enables programmers to create
complex systems by breaking them down into smaller, more manageable components. For example,
a programmer can create several different types of objects, which can be variables, functions or data
structures. Programmers can also create different classes of objects as ways to define the objects.
For instance, a class of variable might be an address. The class might specify that each address object
shall have a name, street, city and zip code. The objects, in this case, might be employee addresses,
customer addresses or supplier addresses. In addition, abstraction provides a mechanism for hiding
the implementation details of a class or method from the outside world and providing a simplified
interface for clients to interact with. In Java, you can achieve abstraction through two main
mechanisms: abstract classes and interfaces.
1. Abstract Classes: An abstract class is a class that you can’t instantiate and can only extend by
subclasses. Abstract classes can have both abstract and non-abstract methods. Abstract
methods do not have a body and you must implement them by any subclass that extends the
abstract class. Non-abstract methods have a body and you can directly call them by the
subclass.
2. Interfaces: An interface is a collection of methods. You can use it to define a set of behaviors
that a class should implement. A class can implement multiple interfaces, and all the
methods defined in an interface must be implemented by any class that implements it.
How Encapsulation Works
Encapsulation lets us reuse functionality without jeopardizing security. It’s a powerful, time-saving
OOP concept in Java. For example, we may create a piece of code that calls specific data from a
database. It may be useful to reuse that code with other databases or processes. Encapsulation lets
us do that while keeping our original data private. It also lets us alter our original code without
breaking it for others who have adopted it in the meantime.
Encapsulation provides several benefits, including:
1. Data hiding: By hiding the implementation details of a class, encapsulation protects the data
from unauthorized access and manipulation.
2. Modularity: Encapsulation helps to break down complex systems into smaller, more
manageable components, making the codebase more modular and easier to maintain.
3. Flexibility: By providing a controlled interface for interacting with a class, encapsulation
allows for changes to the internal implementation without affecting the external interface.
Access Modifiers
In Java, encapsulation is implemented using access modifiers, which control the visibility of variables
and methods within a class.
The three access modifiers in Java are:
1. Public: Public variables and methods can be accessed from anywhere, including outside the
class.
2. Private: Private variables and methods can only be accessed within the class they are
defined in.
3. Protected: Protected variables and methods can be accessed within the same class and its
subclasses.
Encapsulation enables developers to write cleaner, more organized, and more secure code. By
controlling access to variables and methods, encapsulation promotes good software design practices
and helps to manage the complexity of large-scale projects.
How Inheritance Works
Inheritance is another labor-saving Java OOP concept that works by letting a new class adopt the
properties of another. We call the inheriting class a subclass or a child class. The original class is
often called the parent or the superclass. We use the keyword extends to define a new class that
inherits properties from an old class.
The subclass inherits all the public and protected variables and methods of the superclass, and it can
also define its own variables and methods. This makes it possible to create a hierarchy of classes,
where each subclass inherits from its superclass and adds its own unique features.
Benefits of Inheritance
Inheritance provides several benefits, including:
1. Reusability: By inheriting from a superclass, a subclass can reuse the code and functionality
already defined in the superclass, making it easier to write and maintain code.
2. Polymorphism: Inheritance allows for polymorphism, where objects of different subclasses
can be treated as objects of the same superclass, making it easier to write generic code.
3. Flexibility: Inheritance provides a way to add new features to an existing class hierarchy
without modifying the existing code.
Inheritance allows developers to create complex class hierarchies with shared functionality and
unique features. By promoting code reuse, polymorphism, and flexibility, inheritance enables
developers to write more efficient and maintainable code.
How Polymorphism Works
Polymorphism in Java works by using a reference to a parent class to affect an object in the child
class. We might create a class called “horse” by extending the “animal” class. That class might also
implement the “professional racing” class. The “horse” class is “polymorphic,” since it inherits
attributes of both the “animal” and “professional racing” class.
Two more examples of polymorphism in Java are method overriding and method overloading.
In method overriding, the child class can use the OOP polymorphism concept to override a method
of its parent class. That allows a programmer to use one method in different ways depending on
whether it’s invoked by an object of the parent class or an object of the child class.
In method overloading, a single method may perform different functions depending on the context
in which it’s called. This means a single method name might work in different ways depending on
what arguments are passed to it.
Benefits of Polymorphism
Polymorphism provides several benefits, including:
1. Flexibility: Polymorphism allows for more flexible and adaptable code by enabling objects of
different classes to be treated as if they are of the same class.
2. Code reuse: Polymorphism promotes code reuse by allowing classes to inherit functionality
from other classes and to share common methods and properties.
3. Simplification: Polymorphism simplifies code by enabling the use of generic code that can
handle different types of objects.
Polymorphism allows for more flexible and adaptable code. By enabling objects of different classes
to be treated as if they are of the same class, polymorphism promotes code reuse, simplification,
and flexibility, making it an essential component of Object-Oriented Programming.

Differences between OOP and other programming styles


Object-Oriented Programming (OOP) has become widely popular due to its many advantages over
other programming styles such as Procedural Programming and Functional Programming.
Procedural Programming
Procedural Programming is a programming style that is based on a set of procedures or functions,
where each function is a sequence of instructions that performs a specific task. It focuses on the
sequence of must-follow steps that to accomplish a specific task. In contrast, OOP focuses on the
objects and their interactions to solve problems.
Functional Programming
Functional Programming is a programming style that focuses on the use of functions that produce
output based on their input, without modifying any external state. It is based on mathematical
functions and is characterized by immutability and statelessness. In contrast, OOP is based on
objects and their states, and it is designed to manage complex, stateful systems.
Benefits of OOP over other programming
Here are some key differences between OOP and other programming styles:
1. Data and behavior: OOP is based on the idea of encapsulating data and behavior within
objects, whereas procedural programming separates data and behavior into different
functions or procedures. Functional programming, on the other hand, treats data and
behavior as separate entities altogether.
2. Inheritance and code reuse: OOP uses inheritance to reuse code and build complex systems.
Procedural programming and functional programming do not have inheritance concepts built
into them.
3. Flexibility: OOP is more flexible than procedural programming because it allows for changes
to be made to the underlying data structures and objects without changing the entire
system. In contrast, procedural programming requires a complete restructuring of the
program if any changes are made.
OOP enables encapsulation, inheritance, code reusability, and flexibility, making it a powerful tool
for building complex, stateful systems.

Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the
customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we
don't know the internal processing. In Java, we use abstract class and interface to achieve
abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the
data members are private here.

Coupling
Coupling refers to the knowledge or information or dependency of another class. It arises when
classes are aware of each other. If a class has the details information of another class, there is strong
coupling. In Java, we use private, protected, and public modifiers to display the visibility level of a
class, method, and field. You can use interfaces for the weaker coupling because there is no concrete
implementation.

Cohesion
Cohesion refers to the level of a component which performs a single well-defined task. A single well-
defined task is done by a highly cohesive method. The weakly cohesive method will split the task
into separate parts. The java.io package is a highly cohesive package because it has I/O related
classes and interface. However, the java.util package is a weakly cohesive package because it has
unrelated classes and interfaces.

Association
Association represents the relationship between the objects. Here, one object can be associated
with one object or many objects. There can be four types of association between the objects:
o One to One
o One to Many
o Many to One, and
o Many to Many
Let's understand the relationship with real-time examples. For example, One country can have one
prime minister (one to one), and a prime minister can have many ministers (one to many). Also,
many MP's can have one prime minister (many to one), and many ministers can have many
departments (many to many).
Association can be undirectional or bidirectional.

Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship where one
object contains other objects as a part of its state. It represents the weak relationship between
objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-
a relationship. It is another way to reuse objects.

Composition
The composition is also a way to achieve Association. The composition represents the relationship
where one object contains other objects as a part of its state. There is a strong relationship between
the containing object and the dependent object. It is the state where containing objects do not have
an independent existence. If you delete the parent object, all the child objects will be deleted
automatically.

You might also like