CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
OOP: Inheritance By: Lamiaa Said.
Class Hierarchy (Inheritance)
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CET203 SOFTWARE DEVELOPMENT Session 1B Modelling and the Theory of Inheritance.
LECTURE 07 Programming using C# Inheritance
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
CET203 SOFTWARE DEVELOPMENT Session 3A Abstract classes and Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
CSC 143 Inheritance.
Inheritance Chapter 5.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Programming in C# CHAPTER 5 & 6
Presentation transcript:

CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading

Objectives Understand how constructors are used within an inheritance hierarchy Use overloaded constructors and methods Know when and how to use overriding Understand the position of the Object class within an inheritance hierarchy and override the ToString method within subclasses

Constructors A constructor for a superclass should deal with general initialization. Each subclass can have its own constructor for specialised initialization. How do we inherit constructor behaviour? In the earlier examples we have seen (probably without even noticing!) that when a subclass object is created both its constructor and that of its superclass are executed.

Exercise 1 ConstructorTestA.sln

Exercise 1 class MyClass { public MyClass() { Console.WriteLine("MyClass constructed"); } class MySubClass : MyClass { public MySubClass() { Console.WriteLine("MySubClass constructed"); } Consider the classes: What will be the result of the following? MySubClass myObj = new MySubClass();

Constructors with parameters If your superclass has a parameterized constructor then you need to ensure your subclasses provide the superclass with what it needs If the superclass constructor requires parameters then you must include a constructor for each derived class The subclass constructor must at least provide values for each parameter in the superclass These parameters are passed “up” to the superclass constructor using the keyword base The subclass may also contain additional parameters for use in its own constructor

Using base class MySubClass : MySuperClass { public MySubClass (parameters) : base (super-parameters) { // local initialization }

Exercise 2 Consider a class Employee with the following constructor: Public Employee (String name, int salary) { this.name = name; this.salary = salary; } PartTimeEmployee is a subclass of Employee which contains an additional integer instance variable numHours What would be a suitable constructor for PartTimeEmployee which would set name, salary and numHours to initial values entered via parameters?

Overloading Constructors We have already seen that we can have multiple constructors in the same class We may have a constructor with no parameters where the values are initialised to some default values Or we may have a constructor which takes parameters and uses their values to perform the initialisation We can have any number of constructors, so long as they each have a different “method signature” i.e a different set of parameters This is called overloading It is not just the case for constructors, any method can be overloaded

Constructor Examples/Rules MySuperClass() MySuperClass MySubClass MySuperClass() MySuperClass(x) MySuperClass MySubClass() can call base constructor MySubClass MySuperClass(x) MySuperClass MySubClass() must call base constructor MySubClass

Demonstration ConstructorTestB.sln

Publication Case Study Book author OrderCopies() Magazine orderQty currIssue AdjustQty() RecvNewIssue() Publication title price copies SellCopy()

Exercise 3 PublicationsA.sln

Exercise 3 Suggest constructors for the Publication and Book classes which set initial values for the instance variables from appropriate parameters.

Overriding methods A subclass inherits the methods of its superclass. A subclass always provides at least that set of methods, and often more. However, the implementation of a method can be changed in a subclass. This is overriding the method. If we are going to allow a method of the superclass to be overridden then we need to mark it as virtual We write a new version of the method in the subclass which replaces the inherited one. We indicate that the method has been overridden using the override keyword Properties can also be overridden if different functionality is required in their get/set methods

DiscMag A special category of magazine which has a disc attached to each copy. These must be checked when a new issue arrives, so we want some additional functionality in the RecvNewIssue() method to remind us to do this. Achieve this by overriding RecvNewIssue() in the DiscMag subclass. If we wish to implement the new functionality in addition to existing functionality then the overridden method must also call the superclass version: base.RecvNewIssue();

New RecvNewIssue() Magazine orderQty currIssue AdjustQty() RecvNewIssue() DiscMag RecvNewIssue() The definition of RecvNewIssue() in DiscMag overrides the inherited one. Magazine is not affected – it retains its original definition of RecvNewIssue() Note that the method appears in both classes on the class diagram

Exercise 4 PublicationsB.sln

Exercise 4 The definition of the Magazine class is shown on the following slide Write the code for the class DiscMag, a subclass of Magazine which overrides the RecvNewIssue method The DiscMag version of RecvNewIssue should output the message "Check discs attached to this magazine“ in addition to the existing functionality Are any changes necessary to the Magazine class?

Magazine class class Magazine : Publication { private int orderQty; // property omitted private String currIssue; // property omitted public Magazine(String title, double price, int copies, int orderQty, String currIssue) : base(title, price, copies) { this.orderQty = orderQty; this.currIssue = currIssue; } public void AdjustQty(int qty) { orderQty = qty; } public void RecvNewIssue(String newIssue) { Copies = orderQty; currIssue = newIssue; }

‘Operations’ Formally, ‘RecvNewIssue()’ is an operation. This (one) operation is implemented by two different methods, one in Magazine and the overriding one in DiscMag. Up to now we have never distinguished between operations and methods because they have effectively been the same thing. Most people don’t worry too much about this distinction except when it becomes significant. It is an important part of ‘polymorphism’ which we will meet in a later session.

The ‘Object’ class In C# all objects are (direct or indirect) subclasses of a class called Object. If a class is not declared to extend another then it implicitly extends Object. Object defines no instance variables but several methods. Generally these methods will be overridden by new classes to make them useful. An example is the ToString() method.

Object superclass Book author OrderCopies() Magazine orderQty currIssue AdjustQty() RecvNewIssue() Publication title price copies SellCopy() Object ToString() Not normally shown on diagrams

The ToString method ToString() has the signature public String ToString() The version of ToString() defined by Object produces output like: "Publication.Book“ To be generally useful we need to override this to give a more meaningful string. When overriding ToString() remember to use the override keyword

Overriding ToString() In Publication public override String ToString() { return title; } In Book public override String ToString() { return base.ToString() + " by " + author; } In Magazine public override String ToString() { return base.ToString() + " (" + currIssue + ")"; }

Demonstration PublicationsC.sln