You are on page 1of 15

OOP

By Ms Mirza
HumanBeing1.java

KEY TERMINOLOGY

Label the following:

● Class name & type


● Create an instance of the class
● Keyword that creates a new
instance of the class
Main.java
● Assign attributes to fields of the
instance.
● Method
● State
● Behaviour
Constructors

A special method that is used to


initialise an object of a class.

Called when an instance of the class


is created.

Its purpose is to set the initial values


of the object’s data members or
perform any other required
initialization tasks.
Constructors - 2

A class may have more than one


constructor

Each one of these represent a


different way that the same object
can be created.

Also called method overloading.


Having methods with the same name
and with different parameters.
‘This’ keyword

Refers to the current object instance


of a class.

Used to differentiate between


instance variables and parameters or
local variables that have the same
name.

Local variables
Encapsulation

1. Grouping everything in a class.


One class contains the methods
and the attributes. Our Human
class contains states and
behaviours, so it is
HumanBeing.java
encapsulated.
2. Data Hiding -The variables are
private. They can only be
accessed through getters and
setters.
Why hide data?
Imagine two scenarios for a bank.

1. Customers just walk into the safe and either leave or take money. No record is
kept. We rely on a)people's honesty and that b)they never make a mistake. We
can also never check who has taken what or when. That is equivalent to
allowing public access to a variable. E.g. bankBalance.
2. If a customer wants to withdraw or access money, they go to one of two
different bank tellers and fill out a form. The bank worker will stop them taking
out more than they have, and will definitely record the transaction. The bank
teller will check for mistakes in everything. That is equivalent to having a
mutator and accessor(getter and setter) and a private bankBalnce.
Why hide data?
Task - Create an employee class
Accessors and Mutators

Accessors is a type of method that


returns the value of a private
instance (class) variable. They are
widely known as getter methods

Mutator is a type of method used to


control changes to a encapsulated
instance (class) variable/state. They
are widely known as setter methods.
Scenario: Inheritance
1. The Shell Corporation is a large organisation and there are many types of
employees.
2. The CEO wants to model this aspect and has therefore asked his team of
programmers to model this aspect.
3. Therefore, they are creating classes to represent different types of employees in
the organisation.
4. These new types of objects will contain all the attributes(states) and behaviour
of Employee objects, in addition to some additional, customised states and
behaviours.
Task - Create an employee class
Inheritance

Allows new classes to be based on


existing classes, thus inheriting their
properties(states) and behaviour.

Existing class is called the


superclass, base class or parent
class, and the new class is the
subclass or child class.

extends keyword is used to create a


subclass of a superclass.
Inheritance: Pros & Cons
Pros Cons
Code Reusability: allows reuse of code Tight Coupling: base class and derived
from existing classes, which reduces class are tightly coupled; making it difficult
development time. to modify base class without affecting the
derived class.

Code maintenance: changes to base class Fragile Base Class: modifications to base
are reflected in all derived classes, making class and potentially break functionality in
code easier to modify and maintain derived class

Modularity: promoted the breaking down of


programs into smaller , specialised
components.
Scenario: Aggregation
1. The Shell Corporation wants to keep track of all sales that each salesman has
made.
2. Each sale should have certain information attached to it.
3. Each salesman will have a maximum of 100 sales (none survive beyond that
many sales.

You might also like