0% found this document useful (0 votes)
53 views42 pages

Module 6 Encapsulation and Inheritance

1) Object Oriented Programming (OOP) concepts like encapsulation and inheritance are explained. Encapsulation hides sensitive data by making class variables private and providing public getters and setters. Inheritance allows classes to inherit attributes and behaviors from superclasses. 2) Inheritance provides benefits like code reuse, standardization, and interchangeability. Subclasses inherit properties from superclasses but can override methods. Java does not support multiple inheritance but provides similar functionality using interfaces. 3) The this keyword refers to the current object and super refers to the parent class, allowing access to inherited members. Constructors of superclasses are implicitly called by subclasses.

Uploaded by

Eustus Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
53 views42 pages

Module 6 Encapsulation and Inheritance

1) Object Oriented Programming (OOP) concepts like encapsulation and inheritance are explained. Encapsulation hides sensitive data by making class variables private and providing public getters and setters. Inheritance allows classes to inherit attributes and behaviors from superclasses. 2) Inheritance provides benefits like code reuse, standardization, and interchangeability. Subclasses inherit properties from superclasses but can override methods. Java does not support multiple inheritance but provides similar functionality using interfaces. 3) The this keyword refers to the current object and super refers to the parent class, allowing access to inherited members. Constructors of superclasses are implicitly called by subclasses.

Uploaded by

Eustus Holmes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 42

Object Oriented Programming

Object Oriented Programming (Java)


Module 6
Encapsulation and Inheritance
Understand the importance of using Encapsulation in
creating Java Programs

Create multi-level inheritance and defined different


restriction and rules
Apply the knowledge of object oriented programming in
writing Java programs
Encapsulation

6.1

Encapsulation and Inheritance


Encapsulation

ØThe meaning of Encapsulation, is to make sure that


"sensitive" data is hidden from users. To achieve
this, you must:
• declare class variables/attributes as private (only
accessible within the same class)
• provide public setter and getter methods to
access and update the value of a private variable

Encapsulation and Inheritance


Get and Set
ØPrivate variables can only be accessed within the
same class (an outside class has no access to it)
ØHowever, it is possible to access them if we provide
public getter (accessor) and setter (mutator)
methods.
ØThe get method returns the variable value, and
the set method sets the value.

Encapsulation and Inheritance


Get and Set
public class Person {
private String name; // private = restricted access The get method returns the value
of the variable name.
// Getter
public String getName() {
return name; The set method takes a parameter
} (newName) and assigns it to
// Setter the name variable.
public void setName(String newName) {
this.name = newName;
} The this keyword is used to refer to
} the current object.

Encapsulation and Inheritance


Get and Set

public class MyClass { However, as the name variable is


public static void main(String[] args) {
Person myObj = new Person(); declared as private,
myObj.name = "John"; // error we cannot access it from outside
System.out.println(myObj.name); // error this class.
}
}

Encapsulation and Inheritance


Get and Set
If the variable was declared as public, we would expect the following output:

However, as we try to access a private variable, we get an error:

Encapsulation and Inheritance


Get and Set

public class MyClass {


public static void main(String[] args) {
Person myObj = new Person(); Instead, we use
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName()); the getName() and setName()
}
} methods to acccess and
update the variable.
// Outputs "John"

Encapsulation and Inheritance


Why Encapsulation?
ØBetter control of class attributes and methods
ØClass variables can be made read-only (if you omit
the set method), or write-only (if you omit
the get method)
ØFlexible: the programmer can change one part of
the code without affecting other parts
ØIncreased security of data

Encapsulation and Inheritance


6.2

Encapsulation and Inheritance


Inheritance
Inheritance

ØThe technique of deriving new class definitions


from an existing class definition.

Encapsulation and Inheritance


Inheritance

ØThe following are the benefits of using class


inheritance in OOP:
•Re-use of predefined and well-tested classes
•Standardization of behaviors across a group of
classes
•Ability to use members of a family of classes
interchangeably in methods

Encapsulation and Inheritance


Inheritance

Superclasses and Subclasses

ØSuperclass is the class from which another class


inherits properties. This is a class that is on top of a
hierarchy.

Encapsulation and Inheritance


Inheritance

Superclasses and Subclasses

ØSubclass is a class that inherits all the non-private


attributes and methods, except constructors from a
superclass. This class has the ability to override
methods of the superclass.

Encapsulation and Inheritance


Inheritance

Vehicle Class Hierarchy

Vehicle

Water Vehicle Land Vehicle Air Vehicle

Truck Car

Family Car Luxury Car Sports Car

Encapsulation and Inheritance


Inheritance

Syntax for Implementing Inheritance:

Encapsulation and Inheritance


Inheritance

Types of Inheritance:

Encapsulation and Inheritance


Inheritance

Types of Inheritance:

Encapsulation and Inheritance


Inheritance

Take Note:

Java does not support multiple inheritance,


however, Java provides same effects and benefits of
multiple inheritance through the use of interface.

Encapsulation and Inheritance


Inheritance

The this and the super Keywords:

Øthis – contains a reference to the current object


being constructed. It represents an instance of the
class in which it appears. It can be used to access
class variables and methods.

Encapsulation and Inheritance


Inheritance
The this and super Keywords:

Øsuper – contains a reference to the parent class


(superclass) object. It is used to access
members of a class inherited by the class in which it
appears. It explicitly call a constructor of its
immediate superclass. A super constructor call in the
constructor of a subclass will result in the execution
of relevant constructor from the superclass, based on
the arguments passed.

Encapsulation and Inheritance


Inheritance
Defining Superclasses
public class Person
{
protected String name;
protected String address;
/* Default constructor*/
public Person(){
System.out.println(“Inside person:Constructor”);
name = "";
address = "";
}
Encapsulation and Inheritance
Inheritance
/*Constructor with 2 parameters*/
public Person( String name, String address ){
this.name = name;
this.address = address;
}

Encapsulation and Inheritance


Inheritance
/*Accessor and Mutator methods*/
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public void setName( String name ){
this.name = name;
}
public void setAddress( String add ){
this.address = add;
}}

Encapsulation and Inheritance


Inheritance
Defining Subclasses

public class Student extends Person


{
public Student(){
System.out.println(“Inside Student:Constructor”);
//some code here
}
// some code here
}

Encapsulation and Inheritance


Inheritance
Defining Subclasses
When a Student object is instantiated, the default constructor of its superclass is
invoked implicitly to do the necessary initializations. After that, the statements
inside the subclass are executed.

public static void main( String[] args ){


Student anna = new Student();
}

Encapsulation and Inheritance


Inheritance
Defining Subclasses

In the code, we create an object of class Student. The output


of the program is,

Inside Person:Constructor
Inside Student:Constructor

Encapsulation and Inheritance


Inheritance
Defining Subclasses

In the code, we create an object of class Student. The output


of the program is,

Inside Person:Constructor
Inside Student:Constructor

Encapsulation and Inheritance


Inheritance
Program Flow

Encapsulation and Inheritance


Inheritance
super keyword

public Student(){
super( "SomeName", "SomeAddress" );
System.out.println("Inside Student:Constructor");
}

This code calls the second constructor of its immediate


superclass (which is Person) and
executes it.

Encapsulation and Inheritance


Inheritance
super keyword

Another sample code shown below,

public Student(){
super();
System.out.println("Inside Student:Constructor");
}

This code calls the default constructor of its immediate


superclass (which is Person) and
executes it.
Encapsulation and Inheritance
Inheritance
There are a few things to remember when using the
super constructor call:
1. The super() call MUST OCCUR THE FIRST STATEMENT
IN A CONSTRUCTOR.
2. The super() call can only be used in a constructor
definition.
3. This implies that the this() construct and the super()
calls CANNOT BOTH OCCUR IN
THE SAME CONSTRUCTOR.

Encapsulation and Inheritance


Inheritance
Another use of super is to refer to members of the
superclass (just like the this
reference ).

For example,

public Student()
{
super.name = “somename”;
super.address = “some address”;
}

Encapsulation and Inheritance


Overriding Methods
A subclass can override a method defined in its superclass by providing a
new implementation for that method.

Suppose we have the following implementation for the getName method in


the Person superclass,
public class Person
{
:
:
public String getName(){
System.out.println("Parent: getName");
return name;
}
:
}
Encapsulation and Inheritance
Overriding Methods
To override, the getName method in the subclass Student, we write,

public class Student extends Person


{
:
:
public String getName(){
System.out.println("Student: getName");
return name;
}
:
}

Encapsulation and Inheritance


Overriding Methods
So, when we invoke the getName method of an object of class Student, the
overridden method would be called, and the output would be,

Student: getName

Encapsulation and Inheritance


Final Methods and Final Classes
In Java, it is also possible to declare classes that can no longer be
subclassed. These classes are called final classes.

To declare a class to be final, we just add the final


keyword in the class declaration. For example, if we want the class Person
to be declared
final, we write,
public final class Person
{
//some code here
}

Encapsulation and Inheritance


Final Methods and Final Classes
It is also possible in Java to create methods that cannot be overridden.
These methods are what we call final methods. To declare a method to
be final, we just add the final keyword in the method declaration.

For example, if we want the getName method in


class Person to be declared final, we write,

public final String getName(){


return name;
}

Static methods are automatically final. This means that you cannot
override them.

Encapsulation and Inheritance


Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress

You might also like