OOP Concepts 8
OOP Concepts 8
OOP Concepts 8
(OOP)
OOP Concepts
31st July, 2018
Data Fields:
radius is _______
Methods:
getArea
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
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);
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
Before: After:
c1 c1
c2 c2
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
15
Calling Overloaded Constructor
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)
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
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)
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