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

L06 - Encapsulation and Inheritance

Uploaded by

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

L06 - Encapsulation and Inheritance

Uploaded by

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

L06: Encapsulation &

Inheritance
IT 213 – Object Oriented Programming
Overview
• Overloading • super Keyword
Methods
• Overriding Methods
• Encapsulation
• protected Access
• Inheritance Modifier
• Super Class and Sub • UML Class Diagram
Classes Notation
• extends Keyword
Overloading Methods
• Method overloading
− When methods have the same name, but different
parameters
− When you need the same method functionality for
different types of parameters.
• Overloaded methods have the following properties:
− the same name
− different parameters
− return types can be different or the same
Overloading Methods
1. public void add(int x, int y, int z){
2. int sum = x + y + z;
3. System.out.println(sum);
4. }
5.
6. public void add(double x, double y){
7. double sum = x + y;
8. System.out.println(sum);
9. }
Encapsulation & Inheritance
• Encapsulation
− Means that data and instructions are wrapped up
together and treated as a unit
• Inheritance
− Ability to “inherit”, use or modify existing or
predefined classes
Encapsulation
• The idea behind encapsulation is to ensure that
implementation details are not visible to users. The
variables of one class will be hidden from the other
classes, accessible only through the methods of the
current class.
• To achieve encapsulation in Java, declare the class'
variables as private and provide public setter and getter
methods to modify and view the variables' values.
Encapsulation
• For example:
1.public class BankAccount {
2. private double balance=0;
3. public void deposit(double x) {
4. if(x > 0) {
5. balance += x;
6. }
7. }
8.}

• This implementation hides the balance variable, enabling


access to it only through the deposit method, which
validates the amount to be deposited before modifying
the variable.
Inheritance
• Reusability is a feature of most object-oriented
programming languages.
• In Java, reusability is implemented through inheritance.
• All of java’s APIs inherit from, or are subclassed, from the
Object superclass.
• All classes written in java are allowed to have one direct
superclass, and each superclass has the potential for an
unlimited number of subclasses.
Inheritance
• A superclass is any class above a
Object
specific class in the class hierarchy. It
is sometimes referred to as parent-
class. Class A Class B
• A subclass is any class below a
specific class in the class hierarchy. It Class C Class E
is sometimes referred to as a child-
class. Class D
Inheritance
• A subclass inherits the members of its parent, e.g., its
variables and methods.
• The subclass can use any of the superclass variables and
methods, and can declare its own variables and methods.
• A subclass can have a different implementation of the
superclass methods.
The extends Keyword
• To derive a class, we use the extends keyword.
• In order to illustrate this, let's create a sample parent class.
• Suppose we have a parent class called Employee.
1.public class Employee{
2. protected String name;
3.
4. public Employee(){
5. name = "";
6. }
7. . . .
8.}
The extends Keyword
• Now, we want to create another class named Instructor.
• Since an instructor is also an employee, we decide to just
extend the class Employee, so that we can inherit all the
properties and methods of the existing class Employee.
• To do this, we write,
1.public class Instructor extends Employee{
2. public Instructor(){
3. }
4. . . .
5.}
6.
Class Diagram: Inheritance
SuperClass

SubClass SubClass
Class Diagram: Inheritance
Employee

+id: int
+name: String
+displayType(): void

Instructor Secretary

+dept: String +office: String


The super Keyword
• A subclass can explicitly call a constructor of its immediate
superclass.
• This is done by using the super() constructor call.
• The super() constructor call will invoke the superclass
constructor that matches the parameter list provided in
the constructor call.
The super Keyword
1. public class Instructor extends Employee {
2. public Instructor(String n, String a) {
3. super(n, a);
4. }
5. }
• The example above would invoke the superclass constructor
with 2 parameters
The super Keyword
• The super() constructor call must be the FIRST STATEMENT
in a constructor.
• super() can only be used inside a constructor definition.
• This implies that the this() call and the super() call
CANNOT both be in the same constructor.
The super Keyword
• Another use of super is to refer to members of the
superclass (just like the this reference).
• Example:
1. public Instructor(String n, String a) {
2. super.name = n;
3. super.address = a;
4. }
Overriding Methods
• If for some reason a derived class needs to have a
different implementation of a certain method from that of
the superclass, overriding methods could prove to be very
useful.
• A subclass can override a method defined in its superclass
by providing a new implementation for that method.
Overriding Methods
• Suppose we have the following implementation for the
displayType() method in the Employee superclass,
1. public class Employee {
2. :
3. :
4. public void displayType(){
5. System.out.println("I am an Employee.");
6. }
7. }
Overriding Methods
• To override, the displayType() method of the superclass
Employee, we write in the subclass Instructor,
1. public class Instructor extends Employee{
2. :
3. :
4. @Override
5. public void displayType(){
6. System.out.println("I am an Instructor.");
7. }
8. }
Overriding Methods
• Now, when we invoke the displayType() method of an
object of the subclass Instructor, the overridden
displayType() method would be called, and the output
would be,
I am an Instructor.
The protected access modifier
• protected access
− Allows the class and its sub-classes (and their
methods) to access class variables and methods.
− Keyword: protected
Class Diagram: protected modifier
• Example: number sign(#) denotes protected members
Student
#name: String
#age: int
#address: String
#mathGrade: double
#englishGrade: double
#scienceGrade: double
#Student(n: String)
#Student(n: String, a: int, ad: String)
#print(): void
#computeAverage(): double

You might also like