Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,

Similar presentations


Presentation on theme: "CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,"— Presentation transcript:

1 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

2 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

3 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

4 Constructors Constructing and initializing objects –Java determines constructor to run based on values passed in the parentheses Constructor Arguments CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 3.4.1 House myHouse = new House(“Bungalow”, 4);

5 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

6 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; } CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 3.4.1

7 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

8 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

9 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

10 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

11 this & super this –Refers to the current object that the code is running inside public class House { private String houseType; private int numBedrooms; public House(String houseType, int numBedrooms) { this.houseType = houseType; this.numBedrooms = numBedrooms; } Refers to variable inside the object Refers to local variable defined in the constructor argument CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 3.4.4

12 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

13 this() Invoking Overloaded Constructors –use “this” as a method call to pass values and run other constructors in the same class CPRG 215 Module 3.4 - Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 3.4.6 public class Employee { public String name; public int salary; public Employee(String n, int s) { name = n; salary = s; } public Employee() { this(“unknown”, 10000); }

14 super() 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 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; }


Download ppt "CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,"

Similar presentations


Ads by Google