JAVA OOPS
- ADVANTAGES AND DISADVANTAGES
- BEST PRACTICES IN JAVA OOPS CONCEPT
- CONCEPTS IN OOPS
- WHY JAVA IS NOT PURELY OBJECT ORIENTED PROGRAMMING
Programming languages that largely rely on objects to carry out the logic provided in their source code are referred to as object-oriented programming languages, or OOPs. From the perspective of the user or observer, objects perform the roles assigned to them. Object-oriented programming eyes to use real-world programming constructs such as inheritance, hiding, polymorphism, etc. To guarantee a close relationship between data and the functions that handle it, object-oriented programming (OOP) was developed. This data is accessible to other parts of the code in addition to the problematic function.
Compared
to procedural programming, object-oriented programming has the following
advantages:
OOP is
slower and more difficult to execute than OOP.
Innovative
thinking provides programs with a strong foundation.
Object-oriented
programming (OOP) promotes the "Don't repeat yourself" idea, which
makes it easier to maintain, edit, and debug Java code.
Object-oriented
programming (OOP) facilitates the completion of the building of reusable
applications by minimizing code and development time.
Suggestion:
Follow the "Don't Repeat Yourself" guideline to lessen the quantity
of repetitive code. Instead of repeating the application-wide code, it would be
more effective to gather it into one place and then utilize it again.
Advantages and
disadvantages of oops in Java
Advantages:
If
object-oriented programming in Java is new to you, you should learn about all
of its advantages:
Reusability
Reusable code is defined as code that is written just once but is used again. As long as it is classified, a particular piece of code can be used as much as necessary.
Data Redundancy
One of the key advantages of Java OOP concepts is data redundancy. The same data may be produced in a data warehouse by storing it in two different places. By building on preexisting class definitions, you may use similar functions in other classes and create common class definitions.
Code Maintenance
The
concept behind Java OOPs is to make it easier to maintain current code by
allowing new objects to be formed with just little modifications from old ones.
It enables users to continuously update and change outdated applications.
Security
Java
OOP concepts like data hiding and abstraction provide limited discovery
filtering. As a consequence, OOP functions only display the data that is
necessary for security.
Benefits
of Design
Java
OOPs provide designers more creative flexibility. This leads to better models.
The program eventually reaches its critical limits. It's now easy to program
each non-operation separately.
Easy Debugging
The
Java OOPs concept enables the use of self-contained encapsulation objects.
Thus, engineers can promptly fix problems that keep coming up.
Furthermore, the OOP features prevent the creation of duplicate code.
Disadvantage:
Performance
Because
Java needs to be interpreted at runtime, it is slower than languages like C and
C++. All operating systems are compatible with it, though. Conversely, C++
applications execute more quickly since they are built directly into binary
format for every operating system.
Memory consumption
Java
programs need more memory when they are running in a Java virtual machine.
Price
Java
is a somewhat expensive programming language because it needs more processing
power and also has high memory requirements. More reliable hardware is needed
for the Java program to run.
Less machine interaction
Java
is not a good fit for applications that need to run quickly and interact
directly with the hardware because of its difficulty in doing so and its lack
of explicit references.
Garbage Collection
Java
features automatic trash collection, over which the developer has no control.
It is devoid of memory-freeing functions like delete() and free().
Best Practices in
Java OOP Concepts
The
Java OOP paradigm aims to save time without compromising security or usability.
You can get there by adhering to these object-oriented programming guidelines:
The
Java philosophy of "Don't Repeat Yourself," or SEKA, is fundamental.
Therefore, it's not a good idea to utilize the same code again. Try not to use
more than one strategy in any one of your apps.
The
first step in making your Java code future-proof is to encapsulate it by
designating all methods and variables as private. As appropriate, access to
"protected" can be progressively expanded. But do keep it a secret.
One of
the best practices for Java object-oriented programming is to use single
responsibility. In particular, you must confirm that the class has just one
function. Consequently, the class may be extended for use in various settings
or used independently. This helps to detangle several processes.
The
open-closed design pattern is one of the best ways to implement the Java OOP
concept. The classes and methods must remain open to add extensions,
but they must be closed to edit. We can be confident that the
tried-and-true Java OOP implementation will remain unchanged in this way. But
they might also be modified to do different functions.
Let's
go over the prerequisites, which involve declaring methods and passing messages
before we get started. It has six parts, which start with the description of
the method:
Access
Modifier: describes the context in which the method can be called, or the
access type of the method. There are 4 main types of access definitions in
Java: This is public information that is accessible to all classes in your
application.
"Protected"
denotes that its access is restricted to the package that contains its
definition and any subclasses that are listed inside. You can only access this
property within the class that specifies it.
Proven
in the identical class and package as its definition: default (defined/declared
in an unmodified manner).
The
method's value, or void if the function returns null, is to the left of the
return type.
Procedure
identifier: The guidelines for field names are the same as those for method
names, notwithstanding the modest differences in procedure.
In a
parameter list, which is divided by commas, input parameters are defined in
parenthesis before their data type. If there are no arguments given, use empty
parentheses. List of exclusions: Exceptions are expected by this function. You
can define these exclusions.
You
must run the code block included in parentheses, or the method body, to accomplish the intended result.
Message
passing: The sending and receiving of messages allows objects to share data.
Since the message is an instruction to do an action, calling an action on the
receiving object produces the expected results. Objects’ names, functions’
names, and the definition of the information are all contained in a message.
After discussing the fundamentals, let's examine the four pillars of object-oriented programming (OOP). That being said, let's start by being acquainted with the features of an OO language.
The concepts of
OOPS are:
1)
Class
2)
Object
3)
Method and method passing
4)
Abstraction
5)
Encapsulation
6)
Inheritance
7)
Polymorphism
Class
A
class is a collection of objects. That is a logical entity.
It may
also be viewed as a template that can be utilized to create a single object.
There isn't a specific location needed for the class.
In class
declarations, the following components are typically present in the following
order:
*
Adjectives Further details on establishing default permissions or making the
class public may be found here.
Modifiers:
As per custom, the class name needs to start with a capital letter and be
appropriately capitalized. Potential superclass: A keyword is extended if
the class name includes both the keyword and its superclass. A class cannot
have more than one superclass as a descendant.
A list
of all interfaces that the class implements, separated by commas and preceded
by the word implements, should be included in the elements section. A single
class can implement many user interfaces.
The
class body, {}, is enclosed in braces {}.
Object
Any
entities that can be described by their states and actions are considered
objects. Items such as a table, chair, laptop, bike, pen, etc. It might be
physical or logical.
Objects
can be defined using class instances. An item has two properties: an address
and a memory location. For objects to communicate, it is not required for them
to know one other's data or code. All that has to be done is provide the
message type and the response object type.
A dog
is an excellent illustration of an item with characteristics like the dog’s
color and its breed as well as actions like waving its hand. the tail,
squealing, eating, and so on.
To
begin testing out the class and object methods, let's build an application in
Java:
Now we will
discuss the most important pillars of the OOPS concept
1 Abstraction
The
user only sees the information pertinent to their requirements when using data
abstraction. Units that are unnecessary or inconsequential are not shown to the
user. For example, a vehicle is defined as a car, not as a group of pieces.
Data
abstraction may also be thought of as a means of separating away any
unnecessary information from an object's required attributes. A thing's
characteristics and behaviors, which also distinguish it from other things of
its sort, can be used to classify and organize it.
See a
real-world example of how to operate a car. The driver just understands that
applying the brakes and pressing the gas pedal are controls; he has no
understanding of what makes the automobile go faster or slower. He's not familiar
with the location of the accelerator, brakes, or other controls on the
automobile, nor with its internal workings. It isn't specific enough.
Java
allows abstraction to be achieved through the use of abstract classes and user
interfaces. We can use interfaces to achieve complete abstraction.
An
abstract method lacks an implementation and only contains the specification of
the method.
Introduction
to an abstract class.
Encapsulation
The
definition is putting information together into a single unit. It serves as a
connection between the data and the code it processes. Another way to imagine encapsulation is as a barrier that keeps programs from accessing data that
isn't inside its borders of protection.
Technically
speaking, encapsulation means that only the member functions of a class have
access to its variables and data; other classes cannot view them. Similar to
data concealing, encapsulation prevents other classes from accessing the data
of one class. In this sense, "encapsulation" and "data
hiding" are synonymous.
Encapsulation
may be implemented by a class by declaring public methods to access and set the
values of all of its private variables.
Introduction
Encapsulation.
Inheritance
Inheritance
is one of the fundamental features of object-oriented programming, or OOP. The
Java programming language has an attribute called inheritance that allows one
class to utilize the fields and functions of another. The inheritance process
is carried out via the extends keyword. Inheritance is one term for a
"is-a" relationship.
Here
are some terms that we should review that are frequently used:
A
class that has its properties passed down across generations of classes is also
called a superclass, root class, or inheritable class.
Classes
that inherit from another type are among the many various sorts of classes that
might be referred to as "subclasses." In addition to adding some of
its own, a subclass can inherit the attributes and methods of its parent class.
Inheritance
supports the concept of "reusability" by enabling us to create new
classes when there are already existing ones, from which we may derive our own
class, which already has part of the functionality we want. By doing this, we
may once more utilize the fields and functionalities of an existing class.
Introduction
to inheritance.
Polymorphism
Variations
in an organism's ability to perform a certain function are referred to as
polymorphism in biology. Create a shape (rectangle, square, etc.) or convince
the client to reconsider.
Polymorphism
can be achieved in Java using method overloading and overriding.
Speaking
is another thing that springs to mind. A dog could bark and a cat might meow,
for example.
Let us
understand polymorphism in a Java program
Few more concepts related to oops
Coupling
"Relation" refers to a distinct class or dependence on information or knowledge. When classes are
mutually aware, this happens. When one class knows another class, there is a strong relationship
between them. The modifiers private, protected, and public can be used in Java to specify the
visibility of a class, method, and field. Interfaces can be utilized for looser coupling as they don't have
a setup.
Cohesion
Cohesion is defined as the ability of a component to perform a single, designated function with
effectiveness. There is a pretty consistent way to complete a single task. We use the weakly connected
technique to divide the task into smaller, more manageable portions. The input/output classes,
interface, and other elements of the java.io package make it incredibly well-structured. In contrast,
the java. util package only has a weak connection because it contains different and unrelated classes
and interfaces.
Association
A relationship shows the relationship that exists between two or more elements. In this
scenario, one item can be linked to an arbitrary number of other objects. There are four
types of associations that objects can have:
One-to-one
One-to-many
Many-to-one and
Many-to-many
Here are some actual cases that assist us in understanding the connection. A nation with one primeminister and several ministries answering to him or her is an example of a one-to-many relationship.Additionally, a prime minister can have many ministries, many ministers, and many members ofparliament with one prime minister (many-to-one). You can establish a two-way or one-way connection.
Merging
The whole point of merging is to combine. Aggregation represents the connection in which one item
incorporates other objects into its state. The relationships between the items it shows are only
shaky at best. In Java, this is also known as the has connection. Take inheritance as an example to
demonstrate the is-relationship. This is an additional technique for material reuse.
Composition
Arranging is another type of combination. A composition is an entity that, in its condition, consists of other
things. The contained object and the dependent item are closely connected. This is the circumstance in which
the things mentioned therein do not exist on their own. All of a parent object's offspring will be deleted along
with it.
Why java is not purely object-oriented programming?
A
programming language is referred to as pure or complete object-oriented if it
supports or has functions that treat each component of a program as an object.
Basic data types like int, char, float, bool, etc. are incompatible with it.
They are:
Data
Encapsulation/Hiding-Inheritance
Polymorphism
Abstraction
All predefined
types are objects
All user-defined
types are objects
All operations on
objects must be performed only by methods visible on objects.
Example: Smalltalk
\ n
Java
does not support functions 5 or 7, however, it does support functions 1, 2, 3,
4, and 6. Due to these characteristics, Java is more than just an
object-oriented language:
Simple
data types as objects include char, bool, float, int, and long. Smalltalk is an
object-oriented language that is "pure" compared to Java and C++
since it does not differentiate between object values and primitive type
values. Even the most fundamental data types in Smalltalk—characters, Booleans,
and integers—are objects. Java has a collection of predefined kinds known as
basic types in addition to objects.
Int a= 5;
System.out.print(A);
Static
keyword: When a class is marked as static, using a Java object is not required.
A static function or variable's object-oriented attribute cannot be overridden
by invoking it with a dot (.) or a class object. Class of application: A
wrapper class makes it feasible to convert an object from a primitive and vice
versa. primitive Java lets you use int, float, etc. instead of using integer
and float. To interact with an object, you don't always need to call its
methods. while utilizing mathematical operations, for example.
String s1 = “ABC”
+ “A”;
Even
with Wrapper classes, Java is not a true OOP language since it depends on
built-in capabilities like Unboxing and Autoboxing. When you construct an
integer rather than an int and then do math operations on it, Java will revert
to utilizing the old-fashioned int type.
No comments:
Post a Comment