“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Class Hierarchy (Inheritance)
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
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.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism Lecture 3. C# Classes  Classes are used to accomplish:  Modularity: Scope for methods  Blueprints for generating objects.
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, 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
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Extending Classes.
Overriding Methods & Class Hierarchies
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Inheritance and Polymorphism
Presentation transcript:

“is a”

 Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class contents }

 A child class inherits the methods and data defined for the parent class; however, whether a data or method member of a parent class is accessible in the child class depends on the visibility modifier of a member.  Variables and methods declared with private visibility are not accessible in the child class  However, a private data member defined in the parent class is still part of the state of a derived class.  Variables and methods declared with public visibility are accessible; but public variables violate our goal of encapsulation  There is a third visibility modifier that helps in inheritance situations: protected.

Class publicA protected B internal C privateD ASSEMBLY SubClass (outside package) publicA protectedB internal C privateD Class (outside package) publicA protectedB internal C privateD

 Some languages, e.g., C++, allow Multiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents.  C# and Java support single inheritance, meaning that a derived class can have only one parent class.

A child can redefine a method that it inherits from its parent The new method must have the same signature as the parent's method, but can have a different implementation. The type of the object executing the method determines which version of the method is invoked.

change augmented Keep it

 virtual – tells the compiler that this method CAN be overridden by derived classes public virtual int myMethod() {…}  override – in the subclass, tells the compiler that this method is OVERRIDING the same method in the base class public override int myMethod() {…}  base – in the subclass, calls the base class’s method base.myMethod();

 An inherited member is continually passed down the line  Inheritance is transitive.  Good class design puts all common features as high in the hierarchy as is reasonable. Avoids redundant code.

 A polymorphic reference is one which can refer to different types of objects at different times. It morphs!  The type of the actual instance, not the declared type, determines which method is invoked.  Polymorphic references are therefore resolved at run-time, not during compilation.  This is called dynamic binding.

 Overloading deals with multiple methods in the same class with the same name but different signatures  Overloading lets you define a similar operation in different ways for different data int foo(string[] bar); int foo(int bar1, float a);  Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature  Overriding lets you define a similar operation in different ways for different object types class Base { public virtual int foo() {} } class Derived { public override int foo() {}}

 All classes in C# are derived from the Object class  if a class is not explicitly defined to be the child of an existing class, it is a direct descendant of the Object class  The Object class is therefore the ultimate root of all class hierarchies.  The Object class defines methods that will be shared by all objects in C#, e.g.,  ToString: converts an object to a string representation  Equals: checks if two objects are the same  GetType: returns the type of a type of object  A class can override a method defined in Object to have a different behavior, e.g.,  String class overrides the Equals method to compare the content of two strings

class Program { static void Main(string[] args) { Honda honda = new Honda(); Car car = honda; Honda honda2 = car; Honda honda3 = (Honda)car; } class Car { } class Honda : Car { }

 Assigning an object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment Car car = honda;  Assigning an ancestor object to a reference can also be done, but it is considered to be a narrowing conversion and must be done with a cast: Honda honda = new Honda(); Car car = honda; Honda honda3 = (Honda)car;

 Widening conversions are most common.  Used in polymorphism.  Note: Do not be confused with the term widening or narrowing and memory.

 Everything in C# inherits from object  Similar to Java except includes value types.  Value types are still light-weight and handled specially by the CLI/CLR.  This provides a single base type for all instances of all types.  Called Type Unification

abstract class

Not required, just to demo

new

protected members public virtual decimal Earnings()

OK, because they are protected

 An abstract function has to be overridden while a virtual function may be overridden.  An abstract function can have no functionality. You're basically saying, any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class.  A virtual function, is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.