Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 07 Programming using C# Inheritance

Similar presentations


Presentation on theme: "LECTURE 07 Programming using C# Inheritance"— Presentation transcript:

1 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

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

3 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.

4 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.

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

6 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.

7 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

8 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

9 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

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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 Reading Head First C# Inheritance: pages 237 - 257
Overriding: pages 260 – 271 Overloading: pages Calling Superclass: pages Polymorphism: pages

20 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


Download ppt "LECTURE 07 Programming using C# Inheritance"

Similar presentations


Ads by Google