0% found this document useful (0 votes)
23 views56 pages

Week3 Recap OOP

Uploaded by

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

Week3 Recap OOP

Uploaded by

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

Software Engineering

Week 3
Learning Outcomes

Understand the OOP principles.

Demonstrate an understanding of and apply current


theories, models, and techniques that provide a basis for
problem identification and analysis, software design, implementation,
verification, and documentation.

Use a software development environment supporting the


development of computer programs to develop a
software application.
OOP principles

• Classes and Objects

• Data Hiding

• Inheritance

• Polymorphism
OOP principles - recap

 Classes and Objects

• Data Hiding

• Inheritance

• Polymorphism
Classes and Objects

Vehicles
Classes and Objects

A Vehicle

(hard to draw a vehicle)


Classes and Objects

A Vehicle has
manufacturer
number of seats

number of wheels
Classes and Objects

A Vehicle does

• accelerate
• stop
• brake
Classes and Objects

what a vehicle has


Vehicle
or
manufacturer
noOfSeats the vehicle’s state
noOfWheels
accelerate() what a vehicle does
stop() or
brake()
the vehicle’s behaviour
Classes and Objects

Ferrari f430

has
•manufacturer Ferrari
•2 seats
•4 wheels
Classes and Objects

Science Support Vehicle

has
•manufacturer Ford
•8 seats
•6 wheels
Classes and Objects

What is the difference between


a Vehicle and Ferrari?

We see a Ferrari on the roads. We can say something


about its manufacturer, number of seats and wheels.
Classes and Objects

What is the difference between


a Vehicle and Ferrari?

Ferrari f430 is concrete. A Vehicle is abstract.


Classes and Objects

What is the difference between


a Vehicle and Ferrari?

Ferrari f430 is an object. A Vehicle is a class of objects.


What is an Object?

• The software objects are similar to real-world objects:


they too consist of state and related behaviour.

• An object stores its state in fields (attributes) and


exposes its behaviour through methods.
What is an Object?

• Methods operate on an object's internal state and serve


as the primary mechanism for object-to-object
communication.

• Hiding internal state and requiring all interaction to be


performed through an object's methods is known as
data encapsulation a fundamental principle of object-
oriented programming.
Why Objects? - Benefits

1. Modularity: The source code for an object can be


written and maintained independently of the source code
for other objects. Once created, an object can be easily
passed around inside the system.
2. Information-hiding: By interacting only with an
object's methods, the details of its internal
implementation remain hidden from the outside world.
Why Objects? - Benefits

3. Code re-use: If an object already exists you can use


that object in your program. This allows specialists to
implement/test/debug complex, task-specific objects,
which you can then trust to run in your own code.
4. Pluggability and debugging ease: If a particular object
turns out to be problematic, you can simply remove it from
your application and plug in a different object as its
replacement. This is analogous to fixing mechanical
problems in the real world. If a bolt breaks, you replace it,
not the entire machine.
What is a Class?

The objects are instances of a class.

In object-oriented terms, we say that your car is an instance


of the class of objects known as cars.

A class is the blueprint from which individual objects are


created.

A class is a structure that defines the state (attributes) and


the behaviour (methods).
Classes and Objects
DO NOT confuse the CLASS with the OBJECT !!!

The class is the abstract concept for an object that is


created at design-time.

The objects, instances of classes, are concrete instances of


the class that occur at run-time.

Remember!!!

CLASS OBJECT
Creating a C# class

Create a class called class_name.

class class_name
{
// field, constructor, and
// method declarations
}

The class body is the code between the brackets.


Instantiating a C# class

Create an object object_name as an instance of the class


class_name.

class_name object_name = new class_name(parameters);


Activity 1 – Spot the errors!
public class Vehicle{
private string manufacturer;
private int noOfWheels;

public void Accelerate(){


Console.WriteLine("Vehicle accelerates ...");
}
public void Stop() {
Console.WriteLine("Vehicle stops ...");
}
}

public class VehicleTest{


public static void Main (string[] args) {
Vehicle.Accelerate();
Vehicle.Stop();
}
}
Activity 1 – Solution
public class Vehicle{
private string manufacturer;
private int noOfWheels;

public void Accelerate(){


Console.WriteLine("Vehicle accelerates ...");
}
public void Stop() {
Console.WriteLine("Vehicle stops ...");
}
}

public class VehicleTest{


public static void Main (string[] args) {
Vehicle myCar = new Vehicle();
myCar.Accelerate();
myCar.Stop();
}
OOP principles

 Classes and Objects

 Data Hiding

• Inheritance

• Polymorphism
Data Encapsulation or Data Hiding

Encapsulation is the technique of making the fields in a


class private and providing access to them via public
methods.

The main benefit of encapsulation is the ability to modify


the implemented code in a class without breaking the
others’ code who use it.
Data Encapsulation or Data Hiding

Access level modifiers determine whether other classes can


use a particular data or method.

There are two levels of access control:

• at the top level (class) - public, or no modifier;

• at the member level - public, protected, or private.


Data Encapsulation or Data Hiding

At the top level:

• a public class is visible to all classes everywhere

• a class with no modifier is visible to all classes


inside its own namespace.
Data Encapsulation or Data Hiding

At the member level:


Modifier* Class Namespace Subclass World

public Y Y Y Y

protected Y N Y N

private Y N N N

* C# modifiers
Activity 2 – Spot the errors!
public class Vehicle{
private string manufacturer;
private int noOfWheels;
public Vehicle(string manufacturer, int noOfWheels){
this.manufacturer = manufacturer;
this.noOfWheels = noOfWheels;
}

public void Stop() {


Console.WriteLine("Vehicle stops ...");
}
}
public class VehicleTest{
public static void Main (string[] args) {
Vehicle myCar = new Vehicle("Ferrari", 4);
Console.Write("My " + myCar.manufacturer);
myCar.Stop(); }
}
Activity 2 – Solution
public class Vehicle{
private string manufacturer;
private int noOfWheels;
public Vehicle(string manufacturer, int noOfWheels){
this.manufacturer = manufacturer;
this.noOfWheels = noOfWheels;
}
// Properties – needed to access the private attributes
public string Manufacturer
{
get { return manufacturer; }
set { manufacturer = value; }
}
//Methods
public void Stop() {
Console.WriteLine("Vehicle stops ...");
}
}
public class VehicleTest{
public static void Main (string[] args) {
Vehicle myCar = new Vehicle("Ferrari", 4);
Console.Write("My " + myCar.Manufacturer);//use the property
myCar.Stop(); }
}
Activity 2

If an attribute is declared private in a class, how can it be


accessed from outside the class?

By adding a public property for every private attribute.


A C# class
class Vehicle {
// attributes
private string manufacturer;
public int noOfSeats;

// property for manufacturer


public string Manufacturer
{
get {return manufacturer; }
set { manufacturer = value; }
}

// methods
public void Accelerate() {
Console.WriteLine("Vehicle " + manufacturer +
" accelerates ... ");
}
}
OOP principles

 Classes and Objects

 Data Hiding

 Inheritance

• Polymorphism
Inheritance

Inheritance is one of the most important and most powerful


of the object-oriented programming concepts!

Using inheritance, classes become grouped together in a


hierarchical tree structure.
Inheritance

Inheritance defines an IS-A relationship.

If a class B is a subclass of a class A then B inherits its


data and behaviour from its superclass A.

A subclass can add to the inherited data and behaviour.


Inheritance

class A superclass

class B subclass

public class A
{
public A() { }
}

public class B : A
{
public B() { }
}
Inheritance

A class that is derived from another class is called a


subclass or a derived class or a child class.

The class from which the subclass is derived is called a


superclass or a base class or a parent class.
Inheritance in C#

In C#, to create a class named Car as a subclass of a class


named Vehicle, you should write:

subclass superclass

class Car : Vehicle {


/* addition to, and modifications of, stuff inherited
from class Vehicle */
}
Inheritance in C#
public class Vehicle {
protected string manufacturer;
Vehicle
protected int noOfSeats;
protected int noOfWheels;
} Car Motorbike

public class Car : Vehicle {


//Car has 4 attributes: manufacturer, noOfWheels, noOfSeats and
noOfDoors
private int noOfDoors;
}

public class Motorbike : Vehicle {


//Motorbike has 4 attributes: manufacturer, noOfWheels, noOfSeats
and hasSidecar
private boolean hasSidecar;
}
Inheritance

A subclass inherits all the members (fields, properties, and


methods) from its superclass.

Constructors are not inherited by subclasses, but the


constructor of the superclass can be invoked from the
subclass.

C# language uses single inheritance. This means that a


subclass may only inherit functionality from a single base
class.
Inheritance

The constructor of the subclass can invoke the constructor


of the superclass by using the base keyword.

public Car()
: base() /*call the constructor with no parameters of
the superclass Vehicle */
{
noOfDoors = 0;
}
OOP principles

 Classes and Objects

 Data Hiding

 Inheritance

 Polymorphism
Polymorphism

Polymorphism is the ability of an object to take on many


forms.

Through inheritance, a class can be used as more than one


type; it can be used as its own type or as the parent types.

A method is polymorphic if the action performed by the


method depends on the actual type of the object to which
the method is applied.
Activity
public class Vehicle {
public void Accelerate() {
Console.WriteLine(”Vehicle accelerates… ");
}
}
public class Car : Vehicle {
public new void Accelerate() {
Console.WriteLine(”Car accelerates… ");
}
} Vehicle

Accelerate()
Car myCar = new Car();
myCar.Accelerate();
Vehicle myCarIsAVehicle = (Vehicle) myCar;
myCarIsAVehicle.Accelerate();
Car
Vehicle x = new Car()
Accelerate()
Activity

What output generates the code above?

Car accelerates…

Vehicle accelerates…
Polymorphism

When a subclass inherits from a superclass, it gains all the


attributes, properties and methods of the superclass.

To change the data and behaviour of a superclass, you


have two choices:

•replace the member of the superclass with a new derived


member, or

•override a virtual member of the superclass.


Polymorphism

Replacing a member of a superclass with a new derived


member requires the new keyword.

If a superclass defines a property or a method, the new


keyword is used to create a new definition of that property
or method on a subclass.
Activity
public class Vehicle {
public void Accelerate() {
Console.WriteLine(”Vehicle accelerates… ");
}
}
public class Car : Vehicle {
public new void Accelerate() {
Console.WriteLine(”Car accelerates… ");
}
}

Car myCar = new Car();


myCar.Accelerate();
Vehicle myCarIsAVehicle = (Vehicle) myCar;
myCarIsAVehicle.Accelerate();
Activity

What output generates the code above?

Car accelerates…

Vehicle accelerates…
Polymorphism

When the new keyword is used, the new class members


are called instead of the superclass members that have
been replaced.

Those superclass members are called hidden members.

Hidden class members can still be called if an instance of


the subclass is cast to an instance of the superclass.
Polymorphism

An object of a subclass takes over a class member from a


superclass if the superclass declares that member as
virtual. This is accomplished by adding the virtual keyword
before the return type of the member.

A subclass then has the option of using the override


keyword, instead of new, to replace the superclass
implementation with its own.
Activity
public class Vehicle {
public virtual void Accelerate() {
Console.WriteLine(”Vehicle accelerates… ");
}
}
public class Car : Vehicle {
public override void Accelerate() {
Console.WriteLine(”Car accelerates… ");
}
}

Car myCar = new Car();


myCar.Accelerate();
Vehicle myCarIsAVehicle = (Vehicle) myCar;
myCarIsAVehicle.Accelerate();
Activity

What output generates the code above?

Car accelerates…

Car accelerates…
OOP principles

 Classes and Objects

 Data Hiding

 Inheritance

 Polymorphism
Questions

You might also like