CPRG215 - Module 3 - Topic 4_v3_YD

Download Report

Transcript CPRG215 - Module 3 - Topic 4_v3_YD

CPRG 215
Introduction to
Object-Oriented Programming with Java
Module 3- Introduction to Object Oriented
Programming concepts
Topic 3.4
Constructors, Overloading, Over-riding, this and super
Produced by Harvey Peters, 2008
Copyright SAIT
Please review the following sections
in your textbook
Core Java, Volume I–Fundamentals, Eighth Edition
By Cay S. Horstmann & Gary Cornell
•Chapter 4 - Objects and Classes
•Object Construction
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Constructors
• Constructing and initializing objects
– Constructors are special methods that run when we
instantiate an object
– Usually used for passing initial values to the object
and saving them
– Class can have many constructors, each with a
different set of arguments
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.1
Constructors
• Constructing and initializing objects
– Java determines constructor to run based on values
passed in the parentheses
House myHouse = new House(“Bungalow”, 4);
Constructor Arguments
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.1
Constructors
• Rules:
– Constructor name must match classname
– must not be a return type declared
• return type causes the constructor to appear as a
method that never gets called
• Default Constructor
– is in every class
– enables you to create object instances with “new
Xxx()”
– will be invalid if you add a new constructor
declaration with arguments
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.1
Constructors
public class House {
private String houseType;
private int numBedrooms;
public House() {
//no argument constructor
houseType = “bi-level”;
numBedrooms = 2;
}
public House(String ht, int nb) {
houseType = ht;
numBedrooms = nb;
}
}
Topic 3.4.1
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Constructors
• When you instantiate an object, Java creates an
object of each of the parent classes to the top of
the class hierarchy
• As each parent object is created its constructor
runs
– see ConstructorExample.java
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.1
Over-riding methods
• When an object is instantiated it inherits all
methods from higher levels
• Sometimes we want to replace the inherited
method with a different one
• The replacing method “over-rides” the inherited
method
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.2
Over-riding methods
• Over-riding method must have the same
“signature” as the inherited method:
–
–
–
–
Name
Return Type
Argument List
Other rules
• cannot be less accessible than the method it overrides
• must throw exceptions that are the same type as the
method being overridden
• Over-riding supports polymorphism because object
behaves differently if called through a reference
variable of a higher level type
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.2
Overloading Methods
• method can have numerous versions within a class
• When calling an overloaded method, Java
determines which version to use by matching the
argument structure
• overloaded methods will have different functionality
and argument “signature” but have the same name
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.3
this & super
• this
– Refers to the current object that the code is running inside
public class House
{
private String houseType;
private int numBedrooms;
•Refers to variable inside the object
•Refers to local variable defined in
the constructor argument
public House(String houseType, int
numBedrooms)
{
this.houseType = houseType;
this.numBedrooms = numBedrooms;
}
}
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.4
super
• The “super” keyword
– super is used in a class to refer to the parent class or
member variables in the parent class
– superclass behaviour is invoked as if the object is
part of the superclass
– the behaviour can be inherited from a definition
further up the hierarchy from the superclass
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.5
this()
public class Employee {
public String name;
public int salary;
• Invoking Overloaded
Constructors
– use “this” as a method call to
pass values and run other
constructors in the same class
public Employee(String
n, int s) {
name = n;
salary = s;
}
public Employee() {
this(“unknown”,
10000);
}
}
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.6
super()
public class Employee {
String name;
public Employee(String n) {
name = n;
}
}
public class Manager extends
Employee {
String department;
public Manager(String s, String
d) {
super(s);
department = d;
}
}
• Invoking parent class
constructors
– Often, a parent-level
constructor must be
called to initialize the
parent object
If using “super”or “this”,
they must be in the first
line of the constructor
CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super
Copyright SAIT
Topic 3.4.7