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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.1 Basics of Java Produced by Harvey Peters, 2008 Copyright.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
BY:- TOPS Technologies
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.1 Fundamental Concepts.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
8/30/2018 CPRG 215 Introduction to Object-Oriented Programming with Java Module 5- The java.io Package Topic 5.1 Input and Output Streams, Readers.
An Introduction to Inheritance
Understanding Inheritance
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Overloading and Overriding
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Java – Inheritance.
Chapter 11 Inheritance and Polymorphism
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Inheritance.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

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 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 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 Constructor Arguments CPRG 215 Module Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic House myHouse = new House(“Bungalow”, 4);

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

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 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 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 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 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; 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 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 Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 3.4.5

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

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 Constructors, Overloading, Over-riding, this and super Copyright SAIT Topic 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; }