Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 8 More Object Concepts
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
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.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Classes and Objects
Objects as a programming concept
Interface, Subclass, and Abstract Class Review
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Virtual Functions
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Classes and Objects
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
One class is an extension of another.
Understanding Inheritance
Object-Oriented Programming: Inheritance
Introduction to Classes
Inheritance Basics Programming with Inheritance
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
Computer Programming with JAVA
Java – Inheritance.
Object-Oriented Programming
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Inheritance and Polymorphism Copyright © 2012 Pearson Education, Inc. Chapter 10 Inheritance and Polymorphism

Copyright © 2012 Pearson Education, Inc. Topics 10.1 Inheritance 10.2 Polymorphism 10.3 Abstract Classes Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. 10.1 Inheritance Inheritance allows a new class to extend an existing class The new class inherits the members of the class it extends It helps to specialize a class from an existing class bumblebees class and grasshoppers class are two specialized classes of inserts class It also helps to generalize common members of many classes into a new class Sedan, pickup, and SUV have common characteristics of a car Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. “Is a” Relationship When one object is a specialized version of another object, there is an “is a” relationship between them A grasshopper is an insect A bumblebee is an insect The logic is: All insects have certain common characteristics and can be described by an Insect class The grasshopper has its own unique characteristics to be described by a Grasshopper class The bumblebee has its own unique characteristics to be described by a Bumblebee class Insect Copyright © 2012 Pearson Education, Inc.

“Is a” Relationship (Cont’d) An “is a” relationship implies that the specialized object has all the characteristics of the generalized object The specialized object has additional characteristics that make it special, which the generalized object does not have In object-oriented programming, inheritance creates “is a” relationship among classes when you declare a class to be a specialized class of another specialized “is a” generalized This allows you to extend the capabilities of a class by creating another class that is a specialized version of it Copyright © 2012 Pearson Education, Inc.

Base and Derived Classes Inheritance involves base and derived classes The base class is the generalized class and is sometimes called superclass The derived class is the specialized class and is sometimes called subclass You can think of the derived class as an extended version of the base class The derived class inherits fields, properties, and methods from the base class without any of them having to be rewritten New fields, properties, and methods may be added to the derived class to make it special Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Inheritance Notation Assuming there exists an Automobile class: class Automobile { Members…. } In C# the generic format to declare inheritance in the class header class Car : Automobile { } where Car is the derived class and Automobile is the base class the colon (:) indicates that this class is derived from another class Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Examples A dealership’s inventory include three types of automobiles: cars, pickup trucks, and SUVs The dealership keeps the following data: Make Year model Mileage Price Each type of vehicle that is kept in inventory has the above general data Each type of vehicle also has its own specialized data as shown below: Automobile Cars Pickup SUV Items Cars Pickups SUVs Specialized data Number of doors Drive type Passenger capacity Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Sample Code // base class Automobile { // fields private string _make; private string _model; private int _mileage; private decimal _price; // parameterless constructor public Automobile() { .… } // properties public string Make { .... } public string Model { …. } public int Mileage { …. } public decimal Price { …. } } // derived class Car : Automobile { // field private int _doors; // parameterless constructor public Car() _door = 0; } public int Doors get { return _doors; } set { _doors = value; } // instantiation Car myCar = new Car(); myCar.Make = “Ford”; myCar.Model = “Echo”; myCar.Mileage = 56781; myCar.Price = 7010m; Copyright © 2012 Pearson Education, Inc.

Base Class and Derived Class Constructors When you create an instance of a derived class, the base class constructor is executed first the derived class constructor next by default the base class’ parameterless constructor is automatically executed // base class Rectangle { …. // parameterless constructor public Rectangle() { …. } // parameterized constructor public Rectangle(int length, int width) { …. } ….. } // derived class Box : Rectangle { private int _height; // parameter constructor public Box() { …. } // parameterized constructor public Box(int length, int width, in height) : base(length, width) { …. } …. } inheritance Copyright © 2012 Pearson Education, Inc.

Constructor Issues in Inheritance If you want a parameterized constructor in the base class to execute, or if the base class does not have a parameterless constructor, you must explicitly call the base class’ parameterized constructor using the base keyword public Box(int length, int width, in height) : base(length, width) { …. } The above example calls the base class’ parameterized constructor, passing length and width as arguments Copyright © 2012 Pearson Education, Inc.

Constructor Issues in Inheritance (Cont’d) Given the following statement, Box myBox = new Box(100, 200, 300); 100, 200, 300 // base class Rectangle { …. // parameterless constructor public Rectangle() { …. } // parameterized constructor public Rectangle(int length, int width) { …. } ….. } // derived class Box : Rectangle { private int _height; // parameter constructor public Box() { …. } // parameterized constructor public Box(int length, int width, in height) : base(length, width) { …. } …. } 100, 200, 300 100, 200 Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. 10.2 Polymorphism The term polymorphism refers to an object’s ability to take different forms It allows derived classes to have methods with the same names as methods in their base classes It gives the ability for a program to call the correct method, depending on the type of object that is used to call it When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class To change the data and behavior of a base class, you have an option to override a virtual base member Copyright © 2012 Pearson Education, Inc.

Essential Ingredients of Polymorphism The textbook identifies two essential ingredients of polymorphic behavior: The ability to define a method in a base class and then define a method with the same name in a derived class The derived class overrides the base class method The ability to call the correct version of an overridden method, depending on the type of object it is used to call it If a derived class object is used to call an overridden method, then the derived class’s version is the one that executes If a base class object is used to call an overridden method, then the base class’ version is the one that executes Copyright © 2012 Pearson Education, Inc.

Polymorphism (Cont’d) The keyword virtual is used to declare that a derived class is allowed to override a method of a base class class Animal // base class { private string _species; // field public Animal(string species) { _species = species; } // constructor public string Species { ….. } // property public virtual void MakeSound() { …. } // allow derived class to override } To create an Animal object, your only option is: Animal myAnimal = new myAnimal(“regular animal”); Copyright © 2012 Pearson Education, Inc.

Polymorphism (Cont’d) The keyword override declares that this method overrides a method in the base class class Dog : Animal { private string _name; // field public Dog(string name) : base (“Dog”) { _name = name; } // constructor public string Name { ….. } // property public override void MakeSound() { …. } } To create an Animal object, your options are: Dog myDog = new Dog(“Fido”); Animal myAnimal = new Dog(“Fido”); // an object of Dog is also an Animal object Copyright © 2012 Pearson Education, Inc.

Overriding Properties Properties in a base class can be overridden in the same way that methods can be overridden public virtual double Weight { get { return _weight; } set { _weight = value; } } To override the property in the derived class you use the override keyword public override double Weight get { return _weight * 0.165; } Copyright © 2012 Pearson Education, Inc.

Passing Objects to Base Class Parameters Given the following method of a derived class, private void ShowAnimalInfo(Animal animal) { MessageBox.Show(“Species: “ + animal.Species); animal.MakeSound(); } This method has an Animal variable as its parameter. You can pass an Animal object to the method Animal myAnimal = new Animal(“Regular animal”); ShowAnimalInfo(myAnimal); The method can display the object’s Species property and calls its MakeSound method Due to polymorphism, you can also pass a Dog object as argument to the ShowAnimalInfo method Dog myDog = new Dog(“Fido”); Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Base Class Reference A base class reference variable can reference an object of any class that is derived from the base class A base class reference variable knows only about the members that are declared in the base class class Animal // base class { private string _species; …. public string Species { } } If the derived class introduces additional methods, properties, or fields, a base class reference variable cannot access them Animal myAnimal = new Dog(“Fido”); MessageBox.Show(“The species is “ + myAnimal.Species); MessageBox.Show(“The animal’s name is “ + myAnimal.Name); // ERROR! class Dog : Animal // derived class { private string _name; …. public string Name { } } Copyright © 2012 Pearson Education, Inc.

The “Is a” Relationship Does Not Work in Reverse It is important to understand that the “is a” relationship does not work in reverse A dog is an animal. Yet, “an animal is a dog” is not always true. Dog myDog = new Animal(“Dog”); // will not compile You cannot assign an Animal reference to a Dog variable because Dog is a derived class class Animal // base class { …. } class Dog : Animal // derived class Copyright © 2012 Pearson Education, Inc.

Summary of Polymorphism Issues In order for an instance of a derived class to completely take over a class member from a base class, the base class has to declare that member as virtual Fields cannot be virtual Only methods, properties, events and indexers can be virtual A derived class then has the option of using the override keyword to replace the base class implementation with its own Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. 10.3 Abstract Classes In certain applications, some base classes are not intended to be instantiated to create objects They are designed solely for the purpose of providing an outline for subclasses. For example, a Student class that describes what is common to all students, but does not provide details for students majoring in Computer Science The Student class is intended to be a base class that can be derived by the Computer Science class An abstract class serves as a base class but is not instantiated itself It only provides some class members to its derived classes Copyright © 2012 Pearson Education, Inc.

Abstract Classes (Cont’d) To declare a class as abstract, use the abstract keyword in the class header. For example, abstract class Person { …. } The abstract keyword indicates that a class is intended only to be a base class of other classes The primary differences between an abstract class and a regular class (aka concrete class) is: An abstract class cannot be instantiated A concrete class can be instantiated Copyright © 2012 Pearson Education, Inc.

Members of Abstract Classes An abstract class can have abstract and concrete members. For example, abstract class Student { private string _name; // concrete public Student(string name) { _name = name; } // concrete public string Name { get { return _name; } // concrete public abstract double Required Hours { get; } // abstract } Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Abstract Methods Abstract classes can contain abstract methods An abstract method has only a header and no body It must be overridden in a derived class To create an abstract method, use the abstract keyword before the return type abstract class Person { public abstract void DoSomething(); // no method body } Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc. Abstract Properties Abstract classes can also contain abstract properties An abstract property is a property that appears in a base class Abstract properties are expected to be overridden in a derived class To create an abstract property, use the abstract keyword before the property type abstract class Person { public abstract string JobTitle // abstract property get; // abstract get accessor set; // abstract set accessor } To create an abstract read-only property, leave out the set accessor Copyright © 2012 Pearson Education, Inc.