Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

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.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 5 What is object-oriented programming OOP techniques How Windows Forms applications rely on OOP.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Programming in Java CSCI-2220 Object Oriented Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Operators and Cast. Operators Operator Shortcuts.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Static data members Constructors and Destructors
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Inheritance AKEEL AHMED.
Lecture 23 Polymorphism Richard Gesick.
OOP’S Concepts in C#.Net
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Abstract Classes AKEEL AHMED.
Java Inheritance.
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

Inheritance

Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. This type of inheritance is most useful when you need to add functionality to an existing type, or where a number of related types share a significant amount of common functionality. This type of inheritance is most useful when you need to add functionality to an existing type, or where a number of related types share a significant amount of common functionality.

Interface inheritance means that a type inherits only the signatures of the functions, but does not inherit any implementations. Interface inheritance means that a type inherits only the signatures of the functions, but does not inherit any implementations. This type of inheritance is most useful when you want to specify that a type makes certain features available. This type of inheritance is most useful when you want to specify that a type makes certain features available. Interface inheritance is often regarded as providing a contract: By deriving from an interface, a type is guaranteed to provide certain functionality to clients. Interface inheritance is often regarded as providing a contract: By deriving from an interface, a type is guaranteed to provide certain functionality to clients.

Multiple Inheritance Some languages such as C++ support what is known as multiple inheritance, which a class derives from more than one other class. Some languages such as C++ support what is known as multiple inheritance, which a class derives from more than one other class. C# does not support multiple implementation inheritance. C# does not support multiple implementation inheritance. On the other hand, it does allow types to derive from multiple interfaces. On the other hand, it does allow types to derive from multiple interfaces. This means that a C# class can derive from one other class, and any number of interfaces. This means that a C# class can derive from one other class, and any number of interfaces.

Structs and Classes structs don’t really support implementation inheritance, but they do support interface inheritance. structs don’t really support implementation inheritance, but they do support interface inheritance. Structs are always derived from System.ValueType. Structs are always derived from System.ValueType. They can also derive from any number of interfaces. They can also derive from any number of interfaces. Classes are always derived from one other class of your choosing. Classes are always derived from one other class of your choosing. They can also derive from any number of interfaces. They can also derive from any number of interfaces.

Implementation Inheritance class MyDerivedClass : MyBaseClass { // functions and data members here } C# does not support private inheritance, C# does not support private inheritance, hence the absence of a public or private qualifier on the base class name hence the absence of a public or private qualifier on the base class name

If a class (or a struct) also derives from interfaces, then the list of base class and interfaces is separated by commas: If a class (or a struct) also derives from interfaces, then the list of base class and interfaces is separated by commas: public class DerivedClass : BaseClass, Iface1, Iface2 { // etc. // etc.} For a struct, the syntax is as follows: For a struct, the syntax is as follows: public struct DerivedStruct : Iface1, Iface2 { // etc. }

If you do not specify a base class in a class definition, the C# compiler will assume that System.Object is the base class. If you do not specify a base class in a class definition, the C# compiler will assume that System.Object is the base class. class MyClass : Object // derives from System.Object { // etc. } Is same as in Is same as in class MyClass // derives from System.Object { // etc. } For the sake of simplicity, the second form is more common. For the sake of simplicity, the second form is more common.

Virtual Methods By declaring a base class function as virtual, we allow the function to be overridden in any derived classes: By declaring a base class function as virtual, we allow the function to be overridden in any derived classes: class MyBaseClass { public virtual string VirtualMethod() public virtual string VirtualMethod() { return “This method defined in MyBaseClass”; return “This method defined in MyBaseClass”; }}

It is also permitted to declare a property as virtual. It is also permitted to declare a property as virtual. For a virtual or overridden property, the syntax is the same as for a non-virtual property with the exception of the keyword virtual, which is added to the definition. For a virtual or overridden property, the syntax is the same as for a non-virtual property with the exception of the keyword virtual, which is added to the definition. The syntax looks like this: The syntax looks like this: public virtual string ForeName { get { return foreName;} set { foreName = value;} } private string foreName;

In Java, by contrast, all functions are virtual. In Java, by contrast, all functions are virtual. it requires you to declare when a derived class’s function overrides another function, using the override keyword: it requires you to declare when a derived class’s function overrides another function, using the override keyword: class MyDerivedClass : MyBaseClass { public override string VirtualMethod() { return “This is an override defined in MyDerivedClass”; }}

Neither member fields nor static functions can be declared as virtual. Neither member fields nor static functions can be declared as virtual. Example: virtual1.cs Example: virtual1.csvirtual1.cs

Hiding Methods If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and override respectively, then the derived class version is said to hidden by the base class version. If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and override respectively, then the derived class version is said to hidden by the base class version. Example: virtual2.cs Example: virtual2.csvirtual2.cs

Calling Base Versions of Functions Example: virtual3.cs Example: virtual3.csvirtual3.cs

Abstract Classes and Functions An abstract class cannot be instantiated, while an abstract function does not have an implementation, and An abstract class cannot be instantiated, while an abstract function does not have an implementation, and Must be overridden in any non-abstract derived class. Must be overridden in any non-abstract derived class. Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error). Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error). If any class contains any abstract functions, then that class is also abstract and must be declared as such. If any class contains any abstract functions, then that class is also abstract and must be declared as such. abstract class Building { public abstract decimal CalculateHeatingCost(); // abstract method }

abstract class Building { private bool damaged = false; // field public abstract decimal CalculateHeatingCost(); // abstract method }

Sealed Classes and Methods C# allows classes and methods to be declared as sealed. C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method. In the case of a method, this means that you can’t override that method. sealed class FinalClass { ….. …..} class DerivedClass : FinalClass // wrong. Will give compilation error { ………. ……….}

class MyClass { public sealed override void FinalMethod() { // etc. }} class DerivedClass : MyClass { public override void FinalMethod() // compilation error {}}

Constructors of Derived Classes When you create an instance of a derived class, there is actually more than one constructor at work. When you create an instance of a derived class, there is actually more than one constructor at work. The constructor of the class you instantiate isn’t by itself sufficient to initialize the class—the constructors of the base classes must also be called. The constructor of the class you instantiate isn’t by itself sufficient to initialize the class—the constructors of the base classes must also be called.

public class B { private String name; private String name;} public class D : B { double x; double x;} public class MainClass { static void Main() static void Main() { B b = new D(); B b = new D(); }}

constructors are called in order of System.Object first, then progressing down the hierarchy until the compiler reaches the class being instantiated. constructors are called in order of System.Object first, then progressing down the hierarchy until the compiler reaches the class being instantiated. Notice also that in this process, each constructor handles initialization of the fields in its own class. Notice also that in this process, each constructor handles initialization of the fields in its own class. That’s how it should normally work, and when you start adding your own constructors you should try to stick to that principle. That’s how it should normally work, and when you start adding your own constructors you should try to stick to that principle.

Adding a No-Parameter Constructor in a Hierarchy public abstract class GCustomer { private string name; public GenericCustomer(): base() { name = “ ”; }OR public GenericCustomer() { name = “ ”; }

The base and this keywords are the only keywords allowed in the line which calls another constructor. The base and this keywords are the only keywords allowed in the line which calls another constructor. Anything else causes a compilation error. Anything else causes a compilation error. If base class no-parameter constructor is private then you’ll get an error in derived class. If base class no-parameter constructor is private then you’ll get an error in derived class.

What’s happened is that the compiler has tried to generate a default constructor for derived class, but not been able to because the default constructor is supposed to invoke the no-parameter base class constructor. What’s happened is that the compiler has tried to generate a default constructor for derived class, but not been able to because the default constructor is supposed to invoke the no-parameter base class constructor. By declaring that constructor as private, we’ve made it inaccessible to the derived class. By declaring that constructor as private, we’ve made it inaccessible to the derived class. A similar error occurs if we supply a constructor to base class, which takes parameters, but at the same time we fail to supply a no- parameter constructor. A similar error occurs if we supply a constructor to base class, which takes parameters, but at the same time we fail to supply a no- parameter constructor. In this case the compiler will not generate a default constructor for base class, so when it tries to generate the default constructors for any derived class, it’ll again find that it can’t because a no parameter base class constructor is not available. In this case the compiler will not generate a default constructor for base class, so when it tries to generate the default constructors for any derived class, it’ll again find that it can’t because a no parameter base class constructor is not available.

Modifiers Visibility Modifiers Visibility Modifiers

Other Modifiers

Interfaces public interface IDisposable { void Dispose(); } it is not permitted to supply implementations of any of the members of an interface. it is not permitted to supply implementations of any of the members of an interface. In general, an interface can only contain declarations of methods, properties, indexers, and events. In general, an interface can only contain declarations of methods, properties, indexers, and events. You can never instantiate an interface; it only contains the signatures of its members. You can never instantiate an interface; it only contains the signatures of its members.

An interface has neither constructors nor fields (because that would imply some internal implementation). An interface has neither constructors nor fields (because that would imply some internal implementation). An interface definition is also not allowed to contain operator overloads, because interfaces are usually intended to be public contracts, and having operator overloads would cause some incompatibility problems with other.NET languages, such as Visual Basic.NET, which do not support operator overloading. An interface definition is also not allowed to contain operator overloads, because interfaces are usually intended to be public contracts, and having operator overloads would cause some incompatibility problems with other.NET languages, such as Visual Basic.NET, which do not support operator overloading. It is also not permitted to declare modifiers on the members in an interface definition. It is also not permitted to declare modifiers on the members in an interface definition. Interface members are always implicitly public, and cannot be declared as virtual or static. Interface members are always implicitly public, and cannot be declared as virtual or static.

class SomeClass : IDisposable { // this class MUST contain an implementation of the // IDisposable.Dispose() method, otherwise // you get a compilation error public void Dispose() { // implementation of Dispose() method } // rest of class }

Defining and Implementing Interfaces Example: interface.cs Example: interface.csinterface.cs

Interface references can in all respects be treated like class references—but the power of an interface reference is that it can refer to any class that implements that interface. Interface references can in all respects be treated like class references—but the power of an interface reference is that it can refer to any class that implements that interface. For example, this allows us to form arrays of interfaces, where each element of the array is a different class: For example, this allows us to form arrays of interfaces, where each element of the array is a different class: IBankAccount[] accounts = new IBankAccount[2]; accounts[0] = new SaverAccount(); accounts[1] = new GoldAccount(); // If implemented IBankAccount Note, however, that we’d get a compiler error if we tried something like this Note, however, that we’d get a compiler error if we tried something like this accounts[1] = new SomeOtherClass(); //Error //As SomeOtherClass does NOT implement IBankAccount

Derived Interfaces public interface ITBAccount : IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); }

Because ITBAccount derives from IBankAccount, it gets all the members of IBankAccount as well as its own. Because ITBAccount derives from IBankAccount, it gets all the members of IBankAccount as well as its own. That means that any class that implements ITBAccount must implement all the methods of IBankAccount, as well as the new TransferTo() method defined in ITBAccount. That means that any class that implements ITBAccount must implement all the methods of IBankAccount, as well as the new TransferTo() method defined in ITBAccount. Failure to implement all of these methods will result in a compilation error. Failure to implement all of these methods will result in a compilation error.

Example: derInterface.cs Example: derInterface.cs derInterface.cs derInterface.cs