[go: up one dir, main page]

0% found this document useful (0 votes)
7 views15 pages

Java 2 Group 8

Uploaded by

Roxanne nougoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Java 2 Group 8

Uploaded by

Roxanne nougoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Object

Oriented
Programming
Concepts
Table of content
Object-Oriented-Programming Concepts
I) Introduction.(case study: Java)

1) Access Modifiers, Snippet Example


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

2) Encapsulation, Snippet Example


2.i) Advantages of encapsulation

3) Immutable Classes and Objects, Snippet Example


3.i) How to easily know an immutable class

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.

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 Example;
1.iii) Protected Access Modifier

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

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

Therefore, these Concepts;


•Avoids many of the pitfalls of other languages.
•Bing object-oriented.
•Enables code streamlining.
II. Java Programming II
ICT 2223
GROUP 8
Names and Matriculation Numbers

 MATSASSE NOUGOUM ROXANNE FELIXIA__ICTU20201128


 MBAH ALAIN AZAH__ICTU20201186
 MBAKU JUNIOR JEFF__ICTU20201225
 MBIA AHANDA HONORINE ELIANE__ICTU20201564
 MBOH BLESS PEARL NCHONGBOH__ICTU20201122
 MESSI NGANDI JOSEPH__ICTU20201294
 METCHEHEH TCHOMTE ARTHUR__ICTU1021292
 METILA KAMGA LEATITIA
 MEVOULA BAYIHA DEBORA__ICTU20201303
 MEZOUOGUE MICHELLE LEANDRA__ICTU20201699

You might also like