SlideShare a Scribd company logo
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

PPTX
C# Inheritance
PPT
Inheritance C#
PPTX
Java- Nested Classes
PDF
Java Inner Classes
PPTX
Object oriented programming Fundamental Concepts
PPTX
EASY TO LEARN INHERITANCE IN C++
PDF
Implementation of oop concept in c++
PPTX
Inner classes in java
C# Inheritance
Inheritance C#
Java- Nested Classes
Java Inner Classes
Object oriented programming Fundamental Concepts
EASY TO LEARN INHERITANCE IN C++
Implementation of oop concept in c++
Inner classes in java

What's hot (20)

PPT
20. Object-Oriented Programming Fundamental Principles
PPTX
Inheritance in c++
PPTX
Multiple inheritance possible in Java
PPTX
Inheritance in oops
PPTX
C# classes objects
PDF
Java Inner Classes
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PDF
OOP Inheritance
PPTX
Inheritance
PPTX
Access controlaspecifier and visibilty modes
PPTX
Inheritance in c++
PPT
Inheritance
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
Inheritance
PPTX
Inheritance in c++
PPTX
Friend functions
PPTX
Inheritance in c++theory
PPT
friend function(c++)
PDF
Object oriented programming With C#
PPTX
Inheritance in java
20. Object-Oriented Programming Fundamental Principles
Inheritance in c++
Multiple inheritance possible in Java
Inheritance in oops
C# classes objects
Java Inner Classes
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
OOP Inheritance
Inheritance
Access controlaspecifier and visibilty modes
Inheritance in c++
Inheritance
[OOP - Lec 07] Access Specifiers
Inheritance
Inheritance in c++
Friend functions
Inheritance in c++theory
friend function(c++)
Object oriented programming With C#
Inheritance in java
Ad

Similar to Module 11 : Inheritance (20)

PPT
Visula C# Programming Lecture 8
PPT
Advanced c#
PPTX
Object Oriented Programming with Object Orinted Concepts
PPT
Inheritance
PPT
java tutorial 3
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPSX
DotNet Conference: code smells
PPT
java tutorial 4
PPT
Java oops PPT
PPT
Lecture4
PPT
Lecture4
PPTX
Interfaces c#
PPT
025466482929 -OOP with Java Development Kit.ppt
PPT
02-OOP with Java.ppt
PPTX
Lecture 9a Interface.pptxdytfhyggtdfggfghff
PPTX
Java notes of Chapter 3 presentation slides
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
PPT
Abstract class
Visula C# Programming Lecture 8
Advanced c#
Object Oriented Programming with Object Orinted Concepts
Inheritance
java tutorial 3
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
DotNet Conference: code smells
java tutorial 4
Java oops PPT
Lecture4
Lecture4
Interfaces c#
025466482929 -OOP with Java Development Kit.ppt
02-OOP with Java.ppt
Lecture 9a Interface.pptxdytfhyggtdfggfghff
Java notes of Chapter 3 presentation slides
Abstract class
Abstract class
Abstract class
Abstract class
Ad

More from Prem Kumar Badri (20)

PPTX
Module 15 attributes
PPTX
Module 14 properties and indexers
PPTX
Module 12 aggregation, namespaces, and advanced scope
PPTX
Module 13 operators, delegates, and events
PPTX
Module 10 : creating and destroying objects
PPTX
Module 9 : using reference type variables
PPTX
Module 8 : Implementing collections and generics
PPTX
Module 7 : Arrays
PPTX
Module 6 : Essentials of Object Oriented Programming
PPTX
Module 5 : Statements & Exceptions
PPTX
Module 4 : methods & parameters
PPTX
Module 3 : using value type variables
PPTX
Module 2: Overview of c#
PPTX
Module 1 : Overview of the Microsoft .NET Platform
PPTX
C# Non generics collection
PPTX
C# Multi threading
PPT
C# Method overloading
PPTX
C# Generic collections
PPTX
C# Global Assembly Cache
PPTX
C# Filtering in generic collections
Module 15 attributes
Module 14 properties and indexers
Module 12 aggregation, namespaces, and advanced scope
Module 13 operators, delegates, and events
Module 10 : creating and destroying objects
Module 9 : using reference type variables
Module 8 : Implementing collections and generics
Module 7 : Arrays
Module 6 : Essentials of Object Oriented Programming
Module 5 : Statements & Exceptions
Module 4 : methods & parameters
Module 3 : using value type variables
Module 2: Overview of c#
Module 1 : Overview of the Microsoft .NET Platform
C# Non generics collection
C# Multi threading
C# Method overloading
C# Generic collections
C# Global Assembly Cache
C# Filtering in generic collections

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
Cell Structure & Organelles in detailed.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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