Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
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.
ITF11006.NET Inheritance. Classes and Inheritance Constructors and Inheritance Modifiers Interfaces Operators.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Inheritance Inheritance Reserved word protected Reserved word super
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Module 7: Essentials of Object-Oriented Programming.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C# G 1 CSC 298 Object Oriented Programming Part 2.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
1 Advanced Programming Inheritance (2). 2 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing.
© 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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Overview of C++ Polymorphism
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Abstract classes and interfaces
Web Design & Development Lecture 9
Classes and Inheritance
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Inheritance AKEEL AHMED.
Polymorphism Lec
Object Oriented Analysis and Design
Module 4: Implementing Object-Oriented Programming Techniques in C#
OOP’S Concepts in C#.Net
Class Inheritance (Cont.)
Abstract classes and interfaces
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Conditional Statements
Abstract Classes AKEEL AHMED.
Classes & Objects: Examples
Interfaces.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Abstract classes and interfaces
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Module 10: Inheritance in C#

Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes

 Deriving Classes Extending Base Classes Accessing Base Class Members Calling Base Class Constructors

Extending Base Classes Syntax for deriving a class from a base class A derived class inherits most elements of its base class A derived class cannot be more accessible than its base class class Token {... } class CommentToken: Token {... } class Token {... } class CommentToken: Token {... } CommentToken « concrete » CommentToken « concrete » Token « concrete » Token « concrete » Derived class Base class ColonColon

Accessing Base Class Members Inherited protected members are implicitly protected in the derived class Methods of a derived class can access only their inherited protected members Protected access modifiers cannot be used in a struct class Token {... class Outside protected string name; { } void Fails(Token t) class CommentToken: Token { { public string Name( ) t.name {... return name; } } } } class Token {... class Outside protected string name; { } void Fails(Token t) class CommentToken: Token { { public string Name( ) t.name {... return name; } } } } 

Calling Base Class Constructors Constructor declarations must use the base keyword A private base class constructor cannot be accessed by a derived class Use the base keyword to qualify identifier scope class Token { protected Token(string name) {... }... } class CommentToken: Token { public CommentToken(string name) : base(name) { }... } class Token { protected Token(string name) {... }... } class CommentToken: Token { public CommentToken(string name) : base(name) { }... }

 Implementing Methods Defining Virtual Methods Working with Virtual Methods Overriding Methods Working with Override Methods Using new to Hide Methods Working with the new Keyword Practice: Implementing Methods Quiz: Spot the Bugs

Defining Virtual Methods Syntax: Declare as virtual Virtual methods are polymorphic class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... } class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... }

Working with Virtual Methods To use virtual methods: You cannot declare virtual methods as static You cannot declare virtual methods as private

Overriding Methods Syntax: Use the override keyword class Token {... public virtual string Name( ) {... } } class CommentToken: Token {... public override string Name( ) {... } } class Token {... public virtual string Name( ) {... } } class CommentToken: Token {... public override string Name( ) {... } }

Working with Override Methods You can only override identical inherited virtual methods You must match an override method with its associated virtual method You can override an override method You cannot explicitly declare an override method as virtual You cannot declare an override method as static or private class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... } } class CommentToken: Token {... public override int LineNumber( ) {... } public override string Name( ) {... } } class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... } } class CommentToken: Token {... public override int LineNumber( ) {... } public override string Name( ) {... } } 

Using new to Hide Methods Syntax: Use the new keyword to hide a method class Token {... public int LineNumber( ) {... } } class CommentToken: Token {... new public int LineNumber( ) {... } } class Token {... public int LineNumber( ) {... } } class CommentToken: Token {... new public int LineNumber( ) {... } }

Working with the new Keyword Hide both virtual and non-virtual methods Resolve name clashes in code Hide methods that have identical signatures class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... } } class CommentToken: Token {... new public int LineNumber( ) {... } public override string Name( ) {... } } class Token {... public int LineNumber( ) {... } public virtual string Name( ) {... } } class CommentToken: Token {... new public int LineNumber( ) {... } public override string Name( ) {... } }

Practice: Implementing Methods class A { public virtual void M() { Console.Write("A"); } } class B: A { public override void M() { Console.Write("B"); } } class C: B { new public virtual void M() { Console.Write("C"); } } class D: C { public override void M() { Console.Write("D"); } static void Main() { D d = new D(); C c = d; B b = c; A a = b; d.M(); c.M(); b.M(); a.M(); } class A { public virtual void M() { Console.Write("A"); } } class B: A { public override void M() { Console.Write("B"); } } class C: B { new public virtual void M() { Console.Write("C"); } } class D: C { public override void M() { Console.Write("D"); } static void Main() { D d = new D(); C c = d; B b = c; A a = b; d.M(); c.M(); b.M(); a.M(); }

Quiz: Spot the Bugs class Base { public void Alpha( ) {... } public virtual void Beta( ) {... } public virtual void Gamma(int i) {... } public virtual void Delta( ) {... } private virtual void Epsilon( ) {... } } class Derived: Base { public override void Alpha( ) {... } protected override void Beta( ) {... } public override void Gamma(double d) {... } public override int Delta( ) {... } } class Base { public void Alpha( ) {... } public virtual void Beta( ) {... } public virtual void Gamma(int i) {... } public virtual void Delta( ) {... } private virtual void Epsilon( ) {... } } class Derived: Base { public override void Alpha( ) {... } protected override void Beta( ) {... } public override void Gamma(double d) {... } public override int Delta( ) {... } }

Using Sealed Classes You cannot derive from a sealed class You can use sealed classes for optimizing operations at run time Many.NET Framework classes are sealed: String, StringBuilder, and so on Syntax: Use the sealed keyword namespace System { public sealed class String {... } namespace Mine { class FancyString: String {... } } namespace System { public sealed class String {... } namespace Mine { class FancyString: String {... } } 

 Using Interfaces Declaring Interfaces Implementing Multiple Interfaces Implementing Interface Methods Implementing Interface Methods Explicitly Quiz: Spot the Bugs

Declaring Interfaces Syntax: Use the interface keyword to declare methods interface IToken { int LineNumber( ); string Name( ); } interface IToken { int LineNumber( ); string Name( ); } IToken « interface » IToken « interface » LineNumber( ) Name( ) LineNumber( ) Name( ) No method bodies Interface names should begin with a capital “I” No access specifiers

Implementing Multiple Interfaces A class can implement zero or more interfaces An interface can extend zero or more interfaces A class can be more accessible than its base interfaces An interface cannot be more accessible than its base interfaces A class must implement all inherited interface methods interface IToken { string Name( ); } interface IVisitable { void Accept(IVisitor v); } class Token: IToken, IVisitable {... } interface IToken { string Name( ); } interface IVisitable { void Accept(IVisitor v); } class Token: IToken, IVisitable {... } IToken « interface » IToken « interface » IVisitable « interface » IVisitable « interface » Token « concrete » Token « concrete »

Implementing Interface Methods The implementing method must be the same as the interface method The implementing method can be virtual or non-virtual class Token: IToken, IVisitable { public virtual string Name( ) {... } public void Accept(IVisitor v) {... } class Token: IToken, IVisitable { public virtual string Name( ) {... } public void Accept(IVisitor v) {... } Same access Same return type Same name Same parameters Same access Same return type Same name Same parameters

Implementing Interface Methods Explicitly Use the fully qualified interface method name Restrictions of explicit interface method implementation You can only access methods through the interface You cannot declare methods as virtual You cannot specify an access modifier class Token: IToken, IVisitable { string IToken.Name( ) {... } void IVisitable.Accept(IVisitor v) {... } class Token: IToken, IVisitable { string IToken.Name( ) {... } void IVisitable.Accept(IVisitor v) {... }

Quiz: Spot the Bugs interface IToken { string Name( ); int LineNumber( ) { return 42; } string name; } class Token { string IToken.Name( ) {... } static void Main( ) { IToken t = new IToken( ); } interface IToken { string Name( ); int LineNumber( ) { return 42; } string name; } class Token { string IToken.Name( ) {... } static void Main( ) { IToken t = new IToken( ); }

 Using Abstract Classes Declaring Abstract Classes Using Abstract Classes in a Class Hierarchy Comparing Abstract Classes to Interfaces Implementing Abstract Methods Working with Abstract Methods Quiz: Spot the Bugs

Declaring Abstract Classes Use the abstract keyword abstract class Token {... } class Test { static void Main( ) { new Token( ); } abstract class Token {... } class Test { static void Main( ) { new Token( ); } Token { abstract } Token { abstract } An abstract class cannot be instantiated An abstract class cannot be instantiated 

Using Abstract Classes in a Class Hierarchy interface IToken { string Name( ); } abstract class Token: IToken { string IToken.Name( ) {... }... } class CommentToken: Token {... } class KeywordToken: Token {... } interface IToken { string Name( ); } abstract class Token: IToken { string IToken.Name( ) {... }... } class CommentToken: Token {... } class KeywordToken: Token {... } Example 1 Token { abstract } Token { abstract } IToken « interface » IToken « interface » Comment Token « concrete » Comment Token « concrete » Keyword Token « concrete » Keyword Token « concrete »

Using Abstract Classes in a Class Hierarchy (continued) interface IToken { string Name( ); } abstract class Token { public virtual string Name( ) {... }... } class CommentToken: Token, IToken {... } class KeywordToken: Token, IToken {... } interface IToken { string Name( ); } abstract class Token { public virtual string Name( ) {... }... } class CommentToken: Token, IToken {... } class KeywordToken: Token, IToken {... } Example 2 Token { abstract } Token { abstract } IToken « interface » IToken « interface » Comment Token « concrete » Comment Token « concrete » Keyword Token « concrete » Keyword Token « concrete »

Comparing Abstract Classes to Interfaces Similarities Neither can be instantiated Neither can be sealed Differences Interfaces cannot contain any implementation Interfaces cannot declare non-public members Interfaces cannot extend non-interfaces

Implementing Abstract Methods Syntax: Use the abstract keyword Only abstract classes can declare abstract methods Abstract methods cannot contain a method body abstract class Token { public virtual string Name( ) {... } public abstract int Length( ); } class CommentToken: Token { public override string Name( ) {... } public override int Length( ) {... } } abstract class Token { public virtual string Name( ) {... } public abstract int Length( ); } class CommentToken: Token { public override string Name( ) {... } public override int Length( ) {... } }

Working with Abstract Methods Abstract methods are virtual Override methods can override abstract methods in further derived classes Abstract methods can override base class methods declared as virtual Abstract methods can override base class methods declared as override

Quiz: Spot the Bugs class First { public abstract void Method( ); } class First { public abstract void Method( ); } abstract class Second { public abstract void Method( ) { } } abstract class Second { public abstract void Method( ) { } } interface IThird { void Method( ); } abstract class Third: IThird { } interface IThird { void Method( ); } abstract class Third: IThird { }

Lab 10.1: Using Inheritance to Implement an Interface

Review Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes