Java 2 Group 8
Java 2 Group 8
Oriented
Programming
Concepts
Table of content
Object-Oriented-Programming Concepts
I) Introduction.(case study: Java)
4) Conclusion
II) Group 8 Members
Object-Oriented-Programming Concepts
I. Introduction
Object-oriented programming combines a group of data attributes with
functions or methods into a unit called an "object." Typically, OOP languages are
class-based, which means that a class defines the data attributes and functions as
a blueprint for creating objects, which are instances of the class. Popular class-
based OOP languages include Java, Python, and C++.
Case Study: Java
Java is an object-oriented programming language, which means it is
essentially structured into objects and classes. An object (an instance of a class),
is the entity of Java that is physical as well as logical. A class, on the other hand,
is only logical.
Thus object-oriented programming (java) has many concepts but our
focus is on three of them, which are; - Access Modifiers,
- Encapsulation and
- Immutable Classes and Objects
1) Access Modifiers
Access modifiers are used in Java to control the scope, accessibility, and
visibility of objects such as classes, interfaces, variables, methods, constructors, data members,
and setter methods.
Snippet Example;
In our example here at our left, we have declared 2 methods:
method1() and method2(). Here,
• method1 is public - This means it can be accessed by other
classes.
• method2 is private - This means it cannot be accessed by
other classes.
The words public and private are access modifiers in Java. They are
also known as visibility modifiers.
Note that, You cannot set the access modifier of getters methods.
Snippet Example;
Run Code
Output:
I am an animal
1.iv) Public Access Modifier
When methods, variables, classes, and so on are declared public, then we can access
them from anywhere. The public access modifier has no scope restriction.
Snippet Example;
In this case the leg count as a variable has been specified as a public
variable which could give any class access to the variable also the
method display can have ease of access due to the method being
handed by an access modifier as a public.
2) Encapsulation
Encapsulation is one of the four fundamental OOP concepts.
The other three are inheritance, polymorphism, and abstraction.
Encapsulation in java is a mechanism of wrapping the data
(variables)and code acting on the data (methods) together as a
single unit.
In encapsulation, the variables of a class will be hidden from
other classes, and can be accessed only through the methods of
their current class
Therefore, it is also known as data hiding
• Increase flexibility: we can make the variables of the class read-only or write-only
depending on our requirement. If we wish to make the variable read-only then we have to omit
the setter methods like setName(), setAge(), etc. from the above program or if we wish to make
the variables as write-only then we have to omit the get methods like getName(), getAge(), etc.
from the above program.
• Reusability: Encapsulation also improves the re-usability and is easy to change with new
requirements.
• Testing code is easy: Encapsulation code is easy to test for unit testing.
3) Immutable Classes and Objects
Immutable classes in java mean that once a class is created its content cannot
be changed in java.
In Java, all the wrapper classes such as Integer, Boolean, Byte, Short, and the String
class are immutable. We can create our own custom immutable class as well.
Code
continues
4) Conclusion
To conclude,
-access modifiers are mainly used for encapsulation. It can help us to control what part of a program
can access the members of a class. So that misuse of data can be prevented.
-Encapsulation is an OOP concept where data will be wrapped, hiding all the implementation details.
It can be achieved by using private variables and methods such as to get, set to access the variables
Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them without
compromising security. Grasping these concepts is key to understanding how Java works
These concepts are also as great as the developers that implement them. To make your work better, you need
productivity tools to improve your Java programming.