OOP Concepts 8

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Object-Oriented Programming

(OOP)

OOP Concepts
31st July, 2018

Dr. Ramesh Kumar


Assistant Professor
Electronic Engineering Department, DUET Karachi
Object Oriented (OO) Concepts
• OOP involves programming using objects
• An object represents an entity in the real world
that can be distinctly identified
—For example, a student, a desk, a circle, and a
button
• An object has a unique identity, state, &
behavior
• The state of an object consists of a set of data
fields with their current values.
• The behavior of an object is defined by a set of
methods/functions
2
Objects
• An object has both a state and behavior.
—State defines the object properties
—Behavior defines what the object does

Class Name: Circle A class template

Data Fields:
radius is _______

Methods:
getArea

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125

3
Class
• Every object belongs to (is an instance of) a
class
• A class is like a template/construct that
define objects of the same type
• Additionally, a class provides a special type
of methods, known as constructors
—Invoked to construct objects from the class

4
Class Example

5
Constructors
• Constructors are a special kind of methods
that are invoked to construct objects.

Circle() {
}

Circle(double newRadius) {
radius = newRadius;
}

6
Constructors
• A constructor with no parameters is
referred to as a no-arg constructor
• Constructors must have the same name as
the class itself
• Constructors do not have a return type
• Constructors are invoked using the new
operator when an object is created.
• Constructors play the role of initializing
objects
• A class has no-arg constructor by default
7
Creating Objects Using Constructors

new ClassName();

Example:
new Circle();

new Circle(5.0);

8
Declaring/Creating Objects in a
Single Step

ClassName objectRefVar = new ClassName();


Create an object
Assign object reference
Example:
Circle myCircle = new Circle();

9
Accessing Object’s Members
• Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius
• Invoking the object’s method:
objectRefVar.methodName(arguments)
e.g., myCircle.getArea()
Circle myCircle = new Circle(5.0);

SCircle yourCircle = new Circle();

yourCircle.radius = 100;
Cautions
• Recall that you use
• Math.methodName (arguments)
—e.g., Math.pow(3, 2.5): To invoke a method in
the Math class
• Can you invoke getArea() using
Circle1.getArea()?
• It must be invoked from an object using
• objectRefVar.methodName(arguments)
—e.g., myCircle.getArea().

11
• Primitive vs Object Data Types
Primitive type assignment i = j

Before: After:

i 1 i 2

j 2 j 2

Object type assignment c1 = c2

Before: After:

c1 c1

c2 c2

c1: Circle C2: Circle c1: Circle C2: Circle


radius = 5 radius = 9 radius = 5 radius = 9 12
Encapsulation
• Allows the programmer to group data and the
methods in one place and hide the
irrelevant details from the user
• Information hiding: To hide internal
representation, or state, of an object from the
outside
• E.g. If you have an attribute that is not visible
from the outside of an object, and bundle it
with methods that provide read or write
access to it

13
The this Keyword
• The this keyword is the name of a reference
that refers to an object itself.
• One common use of the this keyword is
reference a class’s hidden data fields.
• Another use of this keyword to enable a
constructor to invoke another constructor of
the same class.

14
Reference to Hidden Data Fields

public class F { Suppose that f1 and f2 are two objects of F.


private int i = 5; F f1 = new F(); F f2 = new F();
private static double k = 0;
Invoking f1.setI(10) is to execute
void setI(int i) { this.i = 10, where this refers f1
this.i = i;
} Invoking f2.setI(45) is to execute
this.i = 45, where this refers f2
static void setK(double k) {
F.k = k;
}
}

15
Calling Overloaded Constructor

public class Circle {


private double radius;

public Circle(double radius) {


this.radius = radius;
} this must be explicitly used to reference the data
field radius of the object being constructed
public Circle() {
this(1.0);
} this is used to invoke another constructor

public double getArea() {


return this.radius * this.radius * Math.PI;
}
} Every instance variable belongs to an instance represented by this,
which is normally omitted 16
Class Abstraction
• It means to separate class implementation from
the use of the class.
• The creator of the class provides a description of
the class and let the user know how the class can
be used.
• The user of the class does not need to know how
the class is implemented.
• The detail of implementation is encapsulated and
hidden from the user.
Class Class Contract
implementation is (Signatures of Clients use the
like a black box public methods and class through the
Class
hidden from the public constants) contract of the
clients
class

17
JavaGUIApp Program

import javax.swing.*;

class JavaGUIApp {
public static void main(String[ ] args) {
JFrame myWindow; Declare
Declareaa
name
name
Create
Createan
an
myWindow = new JFrame( ); object
object

myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);
}
}
Use
Usean
anobject
object
JavaGUIApp Program Diagram

setSize(300, 20
0)

setTitle(“My First Java Program”)

V i sible (true)
set

Class
ClassName
Name Object
ObjectName
Name
This
This classmust
class mustbe
be One
One object isdeclared
object is declared
defined before this
defined before this here.
here.
declaration
declarationcan
canbe
bestated.
stated.

JFrame myWindow;
Circle Class with Test Class

20
Circle Class with Test Class

21
Inheritance
• It is very useful mechanism in OOP to design or
more entities that are different but share the
common features
— e.g. Children possess features of their parents
• Main Class: This class known as “Superclass”
has all common features
• Sub-classes: These class inherit the common
features from the Superclass

22
Inheritance
DUET-Students

Undergraduate Graduate

Electronic Chemical Electronic Chemical

Computer

23
Inheritance
• Very important pillar of OOP
• Inheritance relationship enables a subclass to
inherit features from its superclass with
additional new features
• A subclass is a specialization of its superclass;
every instance of a subclass is also an instance
of its superclass, but not vice versa
— For example, every circle is a geometric object, but
not every geometric object is a circle
• Therefore, you can always pass an instance of
a subclass to a parameter of its superclass type

24
Inheritance Example

25
Inheritance Program (Superclass)

26
Inheritance Program (Superclass)

27
Inheritance Circle (Subclass)

Keyword extends tells the compiler that


the Circle class extends the
GeometricObject class, thus inheriting
the methods getColor, setColor, isFilled,
setFilled, and toString.

28
Inheritance Circle (Subclass)

29
Inheritance Rectangle (Subclass)
Keyword extends tells compiler that
Rectangle class extends the
GeometricObject class, thus inheriting
the methods getColor, setColor, isFilled,
setFilled, and toString.

30
Inheritance Rectangle (Subclass)

31
Test Class (Inheritance)

32
super Keyword Usage
• A subclass inherits accessible data fields and
methods from its superclass
• Unlike properties and methods, the constructors of
a superclass are not inherited by a subclass
• A constructor is used to construct a class instance
• They can only be invoked from the constructors of
the subclasses using the keyword super
• The syntax to call a superclass’s constructor is:
— e.g. super (), super (arguments)

33
super Keyword Usage
• Constructing an instance of a subclass invokes
the constructors of all the superclass
• When constructing an object of a subclass, the
subclass constructor first invokes its
superclass constructor before performing its
own tasks

34
Polymorphism
• Very important pillar of OOP

35

You might also like