SlideShare a Scribd company logo
1 of 31
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
{
...
}
CommentToken
« concrete »
Token
« concrete »Derived class Base class
Colon
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; }
} }
}


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) { }
...
}
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( )
{ ...
}
}
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( ) { ... }
}
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( ) { ... }
}


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( ) { ... }
}
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( ) { ... }
}
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();
}
}
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( ) { ... }
}
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 { ... }
}

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( );
}
IToken
« interface »
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
{ ...
}
IToken
« interface »
IVisitable
« interface »
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)
{ ...
}
}
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)
{ ...
}
}
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( );
}
}
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( );
}
}
Token
{ abstract }
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
{ ...
}
Token
{ abstract }
IToken
« interface »
Comment
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
{ ...
}
Token
{ abstract }
IToken
« interface »
Comment
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( ) { ... }
}
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( );
}
abstract class Second
{
public abstract void Method( ) { }
}
interface IThird
{
void Method( );
}
abstract class Third: IThird
{
}
2
3
1
Review
 Deriving Classes
 Implementing Methods
 Using Sealed Classes
 Using Interfaces
 Using Abstract Classes

More Related Content

What's hot

20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner ClassesSoba Arjun
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 

What's hot (20)

20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Friend functions
Friend functions Friend functions
Friend functions
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Similar to Module 11 : Inheritance

Similar to Module 11 : Inheritance (20)

Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Inheritance
InheritanceInheritance
Inheritance
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
DotNet Conference: code smells
DotNet Conference: code smellsDotNet Conference: code smells
DotNet Conference: code smells
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Abstract class
Abstract classAbstract class
Abstract class
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 

More from Prem Kumar Badri

Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexersPrem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopePrem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and eventsPrem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variablesPrem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsPrem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingPrem Kumar Badri
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsPrem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variablesPrem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformPrem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collectionPrem Kumar Badri
 
C# Filtering in generic collections
C# Filtering in generic collectionsC# Filtering in generic collections
C# Filtering in generic collectionsPrem Kumar Badri
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
 
C# Global Assembly Cache
C# Global Assembly CacheC# Global Assembly Cache
C# Global Assembly Cache
 
C# Filtering in generic collections
C# Filtering in generic collectionsC# Filtering in generic collections
C# Filtering in generic collections
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Module 11 : Inheritance

  • 2. Overview  Deriving Classes  Implementing Methods  Using Sealed Classes  Using Interfaces  Using Abstract Classes
  • 3. Deriving Classes  Extending Base Classes  Accessing Base Class Members  Calling Base Class Constructors
  • 4. 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 { ... } CommentToken « concrete » Token « concrete »Derived class Base class Colon
  • 5. 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; } } } }  
  • 6. 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) { } ... }
  • 7. 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
  • 8. Defining Virtual Methods  Syntax: Declare as virtual  Virtual methods are polymorphic class Token { ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }
  • 9. Working with Virtual Methods  To use virtual methods:  You cannot declare virtual methods as static  You cannot declare virtual methods as private
  • 10. Overriding Methods  Syntax: Use the override keyword class Token { ... public virtual string Name( ) { ... } } class CommentToken: Token { ... public override string Name( ) { ... } }
  • 11. 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( ) { ... } }  
  • 12. 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( ) { ... } }
  • 13. 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( ) { ... } }
  • 14. 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(); } }
  • 15. 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( ) { ... } }
  • 16. 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 { ... } } 
  • 17. Using Interfaces  Declaring Interfaces  Implementing Multiple Interfaces  Implementing Interface Methods  Implementing Interface Methods Explicitly  Quiz: Spot the Bugs
  • 18. Declaring Interfaces  Syntax: Use the interface keyword to declare methods interface IToken { int LineNumber( ); string Name( ); } IToken « interface » LineNumber( ) Name( ) No method bodies Interface names should begin with a capital “I” No access specifiers
  • 19. 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 { ... } IToken « interface » IVisitable « interface » Token « concrete »
  • 20. 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) { ... } } Same access Same return type Same name Same parameters
  • 21. 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) { ... } }
  • 22. 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( ); } }
  • 23. 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
  • 24. Declaring Abstract Classes  Use the abstract keyword abstract class Token { ... } class Test { static void Main( ) { new Token( ); } } Token { abstract } An abstract class cannot be instantiated
  • 25. Using Abstract Classes in a Class Hierarchy interface IToken { string Name( ); } abstract class Token: IToken { string IToken.Name( ) { ... } ... } class CommentToken: Token { ... } class KeywordToken: Token { ... } Token { abstract } IToken « interface » Comment Token « concrete » Keyword Token « concrete »
  • 26. 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 { ... } Token { abstract } IToken « interface » Comment Token « concrete » Keyword Token « concrete »
  • 27. 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
  • 28. 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( ) { ... } }
  • 29. 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
  • 30. Quiz: Spot the Bugs class First { public abstract void Method( ); } abstract class Second { public abstract void Method( ) { } } interface IThird { void Method( ); } abstract class Third: IThird { } 2 3 1
  • 31. Review  Deriving Classes  Implementing Methods  Using Sealed Classes  Using Interfaces  Using Abstract Classes