Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Object

Oriented
Programming
Concepts
Table of content
Object-Oriented-Programming Concepts

I) Introduction.(case study: Java)

1) Access Modifiers, Snippet Examples


1.i) Default Access Modifier, Snippet Examples
1.ii) Private Access Modifier, Snippet Examples
1.iii) Protected Access Modifier, Snippet Examples
1.iv) Public Access Modifier, Snippet Examples
1.v) Conclusion

2) Encapsulation, Snippet Examples


2.i) Advantages of encapsulation

3) Immutable Classes and Objects, Snippet Examples


3.i) How to easily know an immutable class
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 Examples;
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.

There are four types of access modifiers. They include; Default,


Private,
Protected and
Public.
1.i) Default Access Modifier
If you don't specify a modifier, it will be treated as default. The default modifier is
only available within the package. It is not accessible from the outside of the packaging. It
allows for more access than private. However, it is more restricted than protected and open
Snippet Example;

1.ii) Private Access Modifier


When variables and methods are declared private, they cannot be accessed outside
of the class
Note that, We cannot declare classes and interfaces private in Java. However, the nested classes
can be declared private
Snippet Examples;
1.iii) Protected Access Modifier

Snippet Examples;

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 Examples;
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.

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

To achieve encapsulation in java,


• Declare the variables of a class as private
• Provide public setter and getter methods to
modify and view the variables values.
Snippet Examples;
This example on our right demonstrates how to achieve
encapsulation in java
File name: EncapTest.java
Advantages of encapsulation
• Data hiding: The user will have no idea about the inner implementation of the class. It
will not be visible to the user how the class is storing values in the variables. The user will only
know that we are passing the values to a setter method and variables are getting initialized with
that value.

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

3.i) How to easily know an immutable class


• The class must be declared as final so that child classes can’t be created.
• Data members in the class must be declared private so that direct access is not
allowed.
• Data members in the class must be declared as final so that we can’t change the
value of it after object creation.
• A parameterized constructor should initialize all the fields performing a deep copy
so that data members can’t be modified with an object reference.
• Deep Copy of objects should be performed in the getter methods to return a copy
rather than returning the actual object reference)
Snippet Example of Immutable Classes and Objects;

Code
continues
Java Programming II
ICT 2223

GROUP 8
Names and Matriculation Numbers
 MATSASSE NOUGOUM ROXANNE FELIXIA
 MBAH ALAIN AZAH
 MBAKU JUNIOR JEFF
 MBIA AHANDA HONORINE ELIANE
 MBOH BLESS PEARL NCHONGBOH
 MESSI NGANDI JOSEPH
 METCHEHEH TCHOMTE ARTHUR ICTU1021292
 METILA KAMGA LEATITIA
 MEVOULA BAYIHA DEBORA
 MEZOUOGUE MICHELLE LEANDRA

You might also like