java lec 2
java lec 2
//illegal constructor
Void Student () {} // it is method not constructor
StudentReg (int id) {…} // neither method nor constructor
Student (int id, string name); // look like an abstract method
Static Student (int id) {…}
Final Student (int id, string name) {…} can’t be static, final,
& abstract
abstract Student (string name) {…}
} OOP Interpreted By:
Asaminew G.
12/03/202
4
8
Cont..
Encapsulation :-
Protect user from modifying the value of fields (data)
Wrapping data and methods
Acquired through access control
fields
are hidden from the user of the object ->
private
method is give service, but hide implementation
Aslong as the services do not change, the
implementation can be modified without impacting
the user
OOP Interpreted By:
Asaminew G.
12/03/202
4
9
Cont..
Example :- let we need to create a model of typical
student with data (id, name, year of study, GPA)
and allowed operation, only change year of study,
set GPA. After Student object is created, we don’t
need to allow to users from changing Name, and Id.
Implement the problem ?
Solution
Data is encapsulated using private modifier
Implement only the required setter methods
Createa constructor that takes all of the fields as
arguments, b/c not setter method for other fields that
are not allowed to be
OOP modified
Interpreted By:
12/03/202 1
Asaminew G.
4
0
Cont. .. Hiding data or
fields by making
Implementation :- private
Replace no argument
constructor with
arguments
Using appropriate
setter method
only
super class
inherit
Student Staff
inherit
CE EE
subclass
Constructor
definition used
for this class
only
Common
methods to all
class, inherit
this class
Setter
method
getter
method
(accessors)
OOP Interpreted By:
Asaminew G.
12/03/202
4
1
6
Testing inheritance
…
Creating new
instance of student
class