0% found this document useful (0 votes)
14 views

java lec 2

Uploaded by

mengeshaawoke663
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

java lec 2

Uploaded by

mengeshaawoke663
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Outline

 Encapsulating and sub classing


 Controlling Access to Members of a Class
 Data abstraction and sub classing
 Constructors and Encapsulation
 Inheritance
 Polymorphism
 Polymorphism using Overridden methods
 Polymorphism using Overloading methods
 Final keyword
 Abstract class and method
 Interface
OOP Interpreted By:
Asaminew G.
12/03/202
4
1
Encapsulating and sub classing
 1. Controlling Access to Members of a Class
 Access control :- implementation hiding
 determine whether other classes can use a
particular field or invoke a particular method
 If modifier is not specified for a class, fields,
methods, and etc. the default modifier package
private is assigned
 There are two levels of access control:
At the top level (class)—public, or package-private
Atthe member level (fields and method)—
public, private, protected, or package-private
OOP Interpreted By:
Asaminew G.
12/03/202
4
2
Cont. …
 Public :-
 is visible everywhere
 can be assigned for a class or its member(fields, methods)
 Package-private (no modifier):-
 is a default modifier
 assigned to a class or its member if modifier is not
specified
 is visible only within its own package.
 Private :-
 Assigned to member of class not to class and interface
 member can only be accessed in its own class
 Protected :-
 the member can only be accessed within its own package
OOP and a subclass By:in another package
of its class
Interpreted
Asaminew G.
12/03/202
4
3
Access level
 Column 1 :- class always has access to its own
members
 Column 2 :- classes in the same package have access to
the member
 Column 3 :- subclasses outside of the package have access to
the member
 Column 4 :- all classes have access to the member

OOP Interpreted By:


Asaminew G.
12/03/202
4
4
Encapsulation cont..
 2. Data abstraction and sub classing
 Information hiding :- implementation detail is not visible to the
client (user)
 Client know only the function of program, not how that function
is implemented. This concept is called data abstraction
 subclass (specific class):-
 called a derived class, extended class, or child class
 is a class that is derived from another class
 inherit all the members (fields, methods, and nested class)
 constructor is not member, i.e. it can’t be inherited, but it can be
invoked by subclass
 the keyword extends is used to create a subclass
OOP Interpreted By:
Asaminew G.
12/03/202
4
5
Cont..
Super class (general class) :-
the class from which the subclass is derived
called base class, parent class
knows nothing of the classes that inherit
from it
all members that have no private modifier is
inherited
constructor is invoked from subclass using
super()
OOP Interpreted By:
Asaminew G.
12/03/202
4
6
Encapsulation cont..
 3. constructor and encapsulation
 Objects are constructed in java
 At least one constructor is invoked if an object is
created
 Every class has its own constructor
 Constructors
 can't be marked static
 can't be marked final or abstract(can’t overridden)
 is either legal or illegal
have all of the normal access modifiers
OOP Interpreted By:
Asaminew G.
12/03/202
4
7
Cont..
 Example :-
Public class Student {
// legal constructor
Student () {}
Student (int id) {…}
Student (int id, string name) {…}

//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

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
1
Inheritance
 allows code defined in one class to be reused in other
classes
 is used to avoid redundant coding
 is simple but powerful
 when you want to create a new class and there is already
a class that includes some of the code that you want, you
can derive your new class from the existing class in which
new class is inherit feature of existing class
 all of the non-private fields and methods from the
superclass are part of the subclass i.e. they can be inherit
 subclass declares its own fields, constructor, and methods

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
2

 The super() construct is used in a subclass
constructor to invoke a constructor in the
immediate superclass.
a super() call in the constructor of a subclass will
result in the execution of the relevant constructor
from the superclass, based on the signature of
the call.
 since the superclass name is known in the
subclass declaration, the compiler can determine
the superclass constructor invoked from the
signature of the parameter list
OOP Interpreted By:
Asaminew G.
12/03/202
4
1
3
Cont..
 Example :-
memberOfBit

super class
inherit

Student Staff

inherit
CE EE
subclass

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
4
Implementation
… Common fields
to all class,
inherit this
class

Constructor
definition used
for this class
only

Common
methods to all
class, inherit
this class

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
5
Cont..
… A keyword used to
create subclass

Setter
method

getter
method
(accessors)
OOP Interpreted By:
Asaminew G.
12/03/202
4
1
6
Testing inheritance
…

Creating new
instance of student
class

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
7
Cont..
 Output

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
8
Cont. ...
 Summary
 Encapsulation is used to protect client from modifying data
 Itis hide both information and implementation; but give
service properly which is called data abstraction
 Access control are used to hide class member
 Subclass can be created using extends keyword
 Itinherit all class member with no private modifier and can
call super class constructor using super()
 Super class know nothing about subclass
 Constructor can’t be final, static, and abstract
 If object is constructed at least one constructor can be called
 Inheritance is allow user to reuse code and avoid redundancy

OOP Interpreted By:


Asaminew G.
12/03/202
4
1
9
Thank
OOP
you
Asaminew G.
Interpreted By:
12/03/202
4
2
0

You might also like