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

CSEN401 - Lecture 2

The document discusses object-oriented programming concepts of inheritance and encapsulation. It defines inheritance as a way for a subclass to inherit properties from a superclass, and encapsulation as hiding implementation details within an object through private variables and public access methods. The document provides examples of implementing inheritance and encapsulation in Java code.

Uploaded by

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

CSEN401 - Lecture 2

The document discusses object-oriented programming concepts of inheritance and encapsulation. It defines inheritance as a way for a subclass to inherit properties from a superclass, and encapsulation as hiding implementation details within an object through private variables and public access methods. The document provides examples of implementing inheritance and encapsulation in Java code.

Uploaded by

Ahmed Hussein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Inheritance Encapsulation Recap

CSEN 401 - Computer Programming Lab

Lecture 2 - Inheritance+Encapsulation

Prof. Dr. Slim Abdennadher


Dr. Nourhan Ehab
Dr. Ahmed Abdelfattah

[email protected]
[email protected]
[email protected]

Faculty of Media Engineering and Technology

18.2.2024 − 22.2.2024
© Slim Abdennadher L2- Inheritance+Encapsulation 1/23
Inheritance Encapsulation Recap

Recall: OOP Features


A language is object-oriented if it has four features.

Easily remembered as A PIE.


• Abstraction.
• Polymorphism.
• Inheritance.
• Encapsulation.
© Slim Abdennadher L2- Inheritance+Encapsulation 2/23
Inheritance Encapsulation Recap

Outline

1 Inheritance

2 Encapsulation

3 Recap

© Slim Abdennadher L2- Inheritance+Encapsulation 3/23


Inheritance Encapsulation Recap

Inheritance: Intuition

© Slim Abdennadher L2- Inheritance+Encapsulation 4/23


Inheritance Encapsulation Recap

Inheritance
A way to describe family of types when one subclass (child)
“inherits” instance variables and instance methods from a
superclass (parent).

• Superclass specifies similarities. Subclass specifies differences.


• Advantage: It enables the programmer to change the behavior
of a class and add functionality without rewriting the entire
class from scratch.
• In Java, a class can only inherit from one superclass:Single
Inheritance.
© Slim Abdennadher L2- Inheritance+Encapsulation 5/23
Inheritance Encapsulation Recap

Inheritance in Java
• To define a class B to be a subclass of a class A:
class B extends A {
.... }
• Several classes can be declared as subclasses of the same
superclass.
• Inheritance can also extend over several generations of classes.

© Slim Abdennadher L2- Inheritance+Encapsulation 6/23


Inheritance Encapsulation Recap

Superclass: Person
public class Person {
String name;
public Person() {
name = "No name yet";
}
public Person(String initialName) {
name = initialName;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
© Slim Abdennadher L2- Inheritance+Encapsulation 7/23
Inheritance Encapsulation Recap

super keyword in Constructors


• To call the constructor of a superclass in a subclass use the
keyword super.
public class Student extends Person {
int studentNumber;
public Student() {
super();
studentNumber = 0;
}
public Student(String initName, int initNum) {
super(initName);
studentNumber = initNum;
} }

• super must always be the first line in the subclass


constructor.
© Slim Abdennadher L2- Inheritance+Encapsulation 8/23
Inheritance Encapsulation Recap

Inheritance and Overriding Methods

• The definition in a subclass class is said to override the


definition in the superclass if:
• the subclass defines a method with the same name as a
method in the superclass and
• the method has the same number and types of parameters as a
method in the superclass.
• When overriding a method, you can change the body of the
method definition to anything you wish.
• The return type of the overriding method definition must be
the same as the return type for the method in the superclass.

© Slim Abdennadher L2- Inheritance+Encapsulation 9/23


Inheritance Encapsulation Recap

Calling an Overriden Method

• Assume you have the following method in the Person class:


public void writeOutput() {
System.out.println("Name: " + name);
}
• If you would like to write a method writeOutput for the class
Student, you can call the method writeOutput of the class
Person by prefacing the method name with super and a dot:
public void writeOutput() {
super.writeOutput();
System.out.println("Student Number " + studentNumber);
}

© Slim Abdennadher L2- Inheritance+Encapsulation 10/23


Inheritance Encapsulation Recap

Outline

1 Inheritance

2 Encapsulation

3 Recap

© Slim Abdennadher L2- Inheritance+Encapsulation 11/23


Inheritance Encapsulation Recap

Encapsulation

Encapsulation makes an object look like a black box.


• The insides of the box are hidden from view.
• Controls are on the outside of the box.
• The user can change the operation of the box only by using
the controls.

© Slim Abdennadher L2- Inheritance+Encapsulation 12/23


Inheritance Encapsulation Recap

Encapsulation

• Object oriented Programming languages make use of


encapsulation to enforce the integrity of a type (i.e. to make
sure data is used in an appropriate manner) by preventing
programmers from accessing data in a non-intended manner.
• Through encapsulation, only a predetermined group of
functions can access the data.
• The collective term for datatypes and operations (methods)
bundled together with access restrictions is a class.
• In Java, there are 4 possible access modifiers: private,
public, protected, and default.

© Slim Abdennadher L2- Inheritance+Encapsulation 13/23


Inheritance Encapsulation Recap

The private Access Modifier

• When a member of a class is declared private it can be used


only by the methods of that class.
• Access methods are methods which uses the private data of
their object and are visible to other classes.
• When data is private the only changes to it are made
through a small number of access methods.
• This helps keep objects consistent and bug-free. If a bug is
detected, there are only a few places to look for it.

© Slim Abdennadher L2- Inheritance+Encapsulation 14/23


Inheritance Encapsulation Recap

Example

class CheckingAccount {
private String accountNumber;
private String accountHolder;
private int balance;
int currentBalance()
{
return balance ;
}
void processDeposit( int amount )
{
balance = balance + amount ;
}
....
}

© Slim Abdennadher L2- Inheritance+Encapsulation 15/23


Inheritance Encapsulation Recap

Private Metods
• A private method of an object can be used only by the other
methods of the object.
• Parts of a program outside of the object cannot directly use a
private method of the object.
• Example:
class CheckingAccount
{ private int balance;
private int useCount = 0;
private void incrementUse()
{ useCount = useCount + 1; }
void processDeposit( int amount )
{ incrementUse();
balance = balance + amount ;
}
}
© Slim Abdennadher L2- Inheritance+Encapsulation 16/23
Inheritance Encapsulation Recap

The public Access Modifier

• The public access modifier explicitly says that a method or


variable of an object can be accessed by code outside of the
object, i.e. in any other class or subclass.
• The public access modifier is usually used for all access
methods and constructors in a class definition.
• Hint: Most variables are made private.

© Slim Abdennadher L2- Inheritance+Encapsulation 17/23


Inheritance Encapsulation Recap

The protected Access Modifier

• A protected member can be accessed from the same class, a


sub-class, and the same package.
• A package is a group of related classes.
• In the labs, we will discuss how to create a package of classes.
• A protected member cannot be accessed from different
packages.

© Slim Abdennadher L2- Inheritance+Encapsulation 18/23


Inheritance Encapsulation Recap

Thedefault Visibility

• If you do not specify public, private or protected for a variable


or a method, then it will have default visibility.
• Default visibility allows a variable or method to be seen by all
methods of a class or other classes that are part of the same
package.

© Slim Abdennadher L2- Inheritance+Encapsulation 19/23


Inheritance Encapsulation Recap

Why to Use Encapsulation?

• The fields of a class can be made read-only or write-only.


• A class can have total control over what is stored in its fields.
• The users of a class do not know how the class stores its data.
A class can change the data type of a field and users of the
class do not need to change any of their code.

© Slim Abdennadher L2- Inheritance+Encapsulation 20/23


Inheritance Encapsulation Recap

Summary of Access Modifiers


Keyword Effect
private Members are not accessible outside the class.
A private constructor: The class cannot be instan-
tiated. A private method can only be called from
within the class.
public Members are accessible anywhere the class is ac-
cessible. A class can be given either package or
public access.
None (Default) Members are accessible from classes in the same
package only. A class can be given either package
access or public access
protected Members are accessible in the package and in sub-
classes of this class.
Note that protected is less protected than the de-
fault package access.
© Slim Abdennadher L2- Inheritance+Encapsulation 21/23
Inheritance Encapsulation Recap

Outline

1 Inheritance

2 Encapsulation

3 Recap

© Slim Abdennadher L2- Inheritance+Encapsulation 22/23


Inheritance Encapsulation Recap

Points To Take Home

• OOP features.
• Objects and Classes.
• Instance Variables and Constructors.
• Instance and Class Methods.
• Class Variables.
• Uses of the this keyword.

Next Lecture: Inheritance and Encapsulation

© Slim Abdennadher L2- Inheritance+Encapsulation 23/23

You might also like