LECTURE 07 Programming using C# Inheritance

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Polymorphism Lecture - 9.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
One class is an extension of another.
One class is an extension of another.
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Lecture 10 Concepts of Programming Languages
Programming in C# CHAPTER 5 & 6
Presentation transcript:

LECTURE 07 Programming using C# Inheritance 4/20/2017 Programming using C# LECTURE 07 Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism Lecture05

What’s to come today? Inheritance IS-A and HAS-A Relationships Overloading Overriding Polymorphism

What is Inheritance? A relationship between a more general class (called the superclass or base class) and a more specialised class(called the subclass or derived class). For example, a Cat is a specific type of Animal. Therefore the Animal class could be the superclass/base class, and the Cat class could be a subclass of Animal.

Why is Inheritance important? Inheritance is a fundamental concept in Object-Oriented programming. Many objects exist that share a lot of commonality between them. By using inheritance, subclasses “inherit” the public variables and methods of the superclass, in addition to their own variables and methods. Any private method or variables from the superclass will not be inherited. Inheritance helps to improve code re-use and minimise duplicate code among related classes.

Inheritance Hierarchies We can represent inheritance among objects via an inheritance hierarchy. Vehicle Motorbike Car Saloon MPV Truck

IS-A Relationship Use the IS-A test to verify that your inheritance hierarchy is valid. A Dog IS-A Animal – makes sense, therefore the Dog class can inherit from an Animal class. A Door IS-A Car– doesn’t make sense, so the Door class shouldn’t inherit from the Car class. An Animal IS-A Dog – doesn’t make sense. A Dog IS-A Animal, but an Animal is not a Dog. The IS-A test only works in one direction i.e. a Cat IS-A Animal, but an Animal is not a Cat.

Programming using C# 4/20/2017 HAS-A Relationships Use the HAS-A test to verify whether an object should contain another object as an instance variable. This is called object composition. A Car IS-A Engine – doesn’t make sense. A Car HAS-A Engine – This makes sense. So therefore our Car class would have an instance variable of type Engine. public class Car{ private Engine carEngine; public Car(){ carEngine = new Engine(); } Lecture05

public class Subclass : BaseClass Programming using C# 4/20/2017 Inheritance in C# public class Cat : Animal { public Cat() : base() Console.WriteLine("Cat Constructor"); } public void runUpATree() Console.WriteLine("I am sleeping"); To declare a subclass in C#, we use the following notation public class Subclass : BaseClass We give the subclass a name followed by a colon (:) and the name of the base class we want to inherit from. Lecture05

Programming using C# 4/20/2017 C# Inheritance Ex. 1 public class Animal { public Animal() Console.WriteLine("Animal Constructor"); } public void move() Console.WriteLine("I am moving."); public class Cat : Animal public Cat() : base() Console.WriteLine("Cat Constructor"); public void climb() Console.WriteLine(”Cat climbing."); Here the Cat class is inheriting all the public methods of the Animal class as well as it’s own methods. Now the following methods can be called on a Cat. Cat cat = new Cat(); cat.move(); cat.climb(); Lecture05

C# Inheritance Ex. 2 Superclass Subclass public class Bird{ protected bool canFly; protected void setCanFly(bool cFly) { canFly = cFly; } public void message(){ if (this.canFly){ Console.WriteLine("This bird can fly"); else{ Console.WriteLine("This bird cannot fly"); } } Subclass public class Robin: Bird { public Robin(){ setCanFly(true); } } class birdTest{ static void Main(string[] args) { Robin robin1 = new Robin(); robin1.message(); Console.Read(); }

Calling Superclass Constructors Programming using C# 4/20/2017 Calling Superclass Constructors When creating subclass objects, it is important that the superclass constructor of that object is called in the subclass constructor. The reasoning behind this is in order to use the subclass, we want to ensure that the superclass and any instance variables or set up logic that it may have is fully initialised before continuing with the more specific subclass. For example, when a Cat object is being constructed we want to call the constructor of the superclass Animal first, before setting up our Cat. Lecture05

Constructing Cat subclass Ex. 1 Programming using C# 4/20/2017 Constructing Cat subclass Ex. 1 public class Animal { public Animal() Console.WriteLine("Animal Constructor"); } public void move() Console.WriteLine("I am moving."); public class Cat : Animal public Cat() : base() Console.WriteLine("Cat Constructor"); public void runUpATree() Console.WriteLine("I am sleeping"); The keyword base is used for calling the superclass constructor. Therefore whenever we construct a Cat, the Animal constructor is called first, then the Cat constructor. Lecture05

Constructing Subclass Ex. 2 Programming using C# 4/20/2017 Constructing Subclass Ex. 2 If our Animal constructor requires a name parameter, then whenever we construct a Cat object, we want our animal’s name to be initialised. public class Animal { private String Name; public Animal(String name) Name = name; } public void move() Console.WriteLine("I am moving."); public class Cat : Animal public Cat(String catName) : base(catName) Console.WriteLine("Cat Constructor"); We can achieve this by adding a parameter to our Cat constructor called catName, and then pass this to the superclass constructor using base(catName). Lecture05

Programming using C# 4/20/2017 Overriding When we create a subclass, we may want to extend the functionality of the inherited superclass methods. In order to override an inherited method, we must declare the same method signature in the subclass. Then inside the body of the method, we can provide our subclass specific functionality. Lecture05

Overriding Example in C# Programming using C# 4/20/2017 Overriding Example in C# If we declare a superclass method with the virtual keyword, we are saying that any subclasses can override this method. We then declare the method we are overriding in the subclass with the override keyword. class Animal { public virtual void Move() Console.WriteLine(”Moving."); } class Rhino: Animal { public override void Move() Console.WriteLine(”Charge!!!"); } Rhino myRhino = new Rhino(); myRhino.Move(); C# will always calls the most specific method, i.e. the method that is lowest in the inheritance hierarchy. In this case the overridden Move() method will be called in the Rhino class and not the Animal class. Lecture05

Programming using C# 4/20/2017 Overloading Overloading allows you to create multiple methods with the same name in the same scope however they differ in their method signatures. public void Add(int number1, int number2) public void Add(int number1, int number2, int number3) public void Add(int number1, int number2, int number3, int number4) Lecture05

Programming using C# 4/20/2017 Polymorphism Polymorphism originates from the greek word “polys” which means many, and “morphe” which means forms, or shapes. Polymorphism is possible when classes share a common interface. For example, a Cat class and a Dog class may share a common interface through inheriting the Animal class. To understand Polymorphism, we must take a look at how we instantiate objects. Cat myCat = new Cat(); Here we declare the reference type Cat, the variable name myCat and instantiate a new Cat object by calling new Cat(); Lecture05

Polymorphism (continued) Programming using C# 4/20/2017 Polymorphism (continued) When classes share a common interface, we can redefine the reference type of the objects we are creating. So our myCat definition can be changed to: Animal myCat = new Cat(); For example, we can now create an Array of Animals that can contains Dog objects, Cat objects and any other subclass of Animal. Animal[] animals = new Animal[2]; animals[0] = new Cat(); animals[1] = new Dog(); Polymorphism is a very powerful concept that improves code extensibility and modularity. Lecture05

Reading Head First C# Inheritance: pages 237 - 257 Overriding: pages 260 – 271 Overloading: pages 381 - 387 Calling Superclass: pages 272 - 278 Polymorphism: pages 330 - 348

What’s to come next time Using Graphical User Interfaces Input via buttons Input via the keyboard Input via the mouse Use of GUI objects