SlideShare a Scribd company logo
2
Most read
Inheritance

Types  of inheritance
Implementing inheritance
Interface
TYPES OF INHERITANCE
   Implementation inheritance means that a
    type derives from a base type, taking all the
    base type’s member fields and functions.

   Interface inheritance means that a type
    inherits only the signatures of the functions
    and does not inherit any implementations.
IMPLEMENTATION
INHERITANCE
  A class derive from another class
 syntax:
Class MyDerivedClass: MyBaseClass
{ //function and data memebers here}

  A class derive from interface,the list of base class
   and interfaces is separated by commas
syntax:
Public class MyDerivedClass: MyBaseClass,
   Interface1,Interface2
{//function and data memebers here}
VIRTUAL METHODS
 By declaring a base class function as virtual, you allow the function to
  be overridden in any derived classes.
  Class MyBaseClass
   {
    public virtual string VirtualMethod( )
         { return “this method is virtural and defined in MyBaseClass” ;}
   }
 It is also permitted to declare a property as virtual.
  Public virtual string ForeName
  {
     get { return foreName;}
     set { foreName=value;}
  }
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 hide the base class
    version.

 Abstract          classes and Functions:
   An abstract class cannot be instantiated,were as an abstract function does
    not have an implementation,and must be overriden in any non-abstract
    derived class
   Abstract function is automatically virtual.
SEALED CLASSES AND
METHODS
   C# allows classes and methods to be declared as sealed.
 In case of class this means that you can’t inherit from that class
  syntax:
          sealed class Class_Name
          { // etc }
 In case of method
 syntax:
 class MyClass: MyclassBase
    {
    public sealed override void FinalMethod()
      { // etc }
    }
 class DerivedClass: MyClass
 {
 public override void FinalMethod() // wrong..will give compilation error
 { // etc } }
Constructors of Derived Classes

   Adding a constructor in a Hierarchy

   Adding a constructor with parameters to a
    Hierarchy
MODIFIERS
                  Modifiers can indicate the visibility of a methods.
MODIFIER           APPLIES TO                  DESCRIPTION

public             Any type or members         The item is visible to any
                                               other code.
protected          Any member of a type, also The item is visible only to
                   any nested type            any derived type.
internal           Any member of a type, also The item is visible only with
                   any nested type            in its containing assembley.
private            Any types or memebers       The item is visible only
                                               inside the type to which it
                                               belongs
protected internal Any member of a type, also The item is visible to any
                   any nested type            code within its containing
                                              assembly and also to any
                                              code inside a derived type
MODIFIER APPLIES TO                DESCRIPTION
new        Function members        The member hides an inherited
                                   member with the same signature
static     All members             The member does not operate on a
                                   specific instance of the class
virtual    Function members only The member can be overridden by a
                                 derived class
abstract   Function members only A virtual member that defines the
                                 signature of the member, but doesn’t
                                 provide an implementation.
override   Function members only The member overrides an inherited
                                 virtual or abstract member
sealed     Classes, methods, and   class cannot be inherited, member
           properties              overrides an inherited virtual member,
                                   but cannot be overridden by any
                                   members in any derived classes
extern     static [DllImport]      The member is implemented
           methods only            externally, in a different language.
INTERFACES
syntax:

    public interface Interface_Name
    {
      void method();
    }

   You can never instantiate an interface, it contains only the
    signatures of its members.

   An interface has neither constructors nor fields and also not
    allowed to contain operator overloads.

   An interface members are always implicitly public, and cannot
    be declared as virtual or static.
Inheritance C#

More Related Content

PPTX
class and objects
PPT
Java interfaces & abstract classes
PPTX
Association agggregation and composition
PPTX
C# Inheritance
PPTX
Java abstract class & abstract methods
PPTX
Abstract class in c++
PPTX
Inheritance in c++
PPTX
Inheritance in OOPS
class and objects
Java interfaces & abstract classes
Association agggregation and composition
C# Inheritance
Java abstract class & abstract methods
Abstract class in c++
Inheritance in c++
Inheritance in OOPS

What's hot (20)

PPTX
C# classes objects
PPTX
Object Oriented Programming Using C++
PPT
Method overriding
PPTX
Virtual base class
PPTX
Multiple inheritance possible in Java
PPTX
Abstract class and Interface
PDF
Object-oriented Programming-with C#
PPTX
Friend function & friend class
PPT
Inheritance in java
PPTX
Member Function in C++
PPTX
Abstract Class Presentation
PPT
friend function(c++)
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
PPT
Core java concepts
PPTX
Packages in java
PPTX
Classes objects in java
PPTX
interface in c#
PPT
Abstract class
PDF
Polymorphism In Java
PPTX
Java exception handling
C# classes objects
Object Oriented Programming Using C++
Method overriding
Virtual base class
Multiple inheritance possible in Java
Abstract class and Interface
Object-oriented Programming-with C#
Friend function & friend class
Inheritance in java
Member Function in C++
Abstract Class Presentation
friend function(c++)
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Core java concepts
Packages in java
Classes objects in java
interface in c#
Abstract class
Polymorphism In Java
Java exception handling
Ad

Similar to Inheritance C# (20)

PDF
OOPs difference faqs-3
PPTX
C# interview
PPTX
OOPS – General Understanding in .NET
PPTX
Object Oriented Programming C#
PPTX
Inheritance.pptx
PPTX
Oop concepts
PPT
Object Oriented Programming In .Net
PPT
PDF
OOPs reasoning faqs- 1
PPT
Inheritance
PPTX
Better Understanding OOP using C#
PPSX
PDF
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
PDF
Intake 38 4
DOCX
Ganesh groups
PPTX
Object Oriented Programming with C#
PPT
Inheritance
OOPs difference faqs-3
C# interview
OOPS – General Understanding in .NET
Object Oriented Programming C#
Inheritance.pptx
Oop concepts
Object Oriented Programming In .Net
OOPs reasoning faqs- 1
Inheritance
Better Understanding OOP using C#
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
Intake 38 4
Ganesh groups
Object Oriented Programming with C#
Inheritance
Ad

More from Raghuveer Guthikonda (9)

PPTX
PPTX
C# Delegates
PPTX
Operators & Casts
PPTX
PPTX
PPTX
Objects and Types C#
PPTX
Introduction to C#
PPT
Introduction to .NET Framework
PPTX
C# Delegates
Operators & Casts
Objects and Types C#
Introduction to C#
Introduction to .NET Framework

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
1. Introduction to Computer Programming.pptx
Encapsulation theory and applications.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Encapsulation_ Review paper, used for researhc scholars
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Tartificialntelligence_presentation.pptx
A comparative study of natural language inference in Swahili using monolingua...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Mushroom cultivation and it's methods.pdf
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
Accuracy of neural networks in brain wave diagnosis of schizophrenia
cloud_computing_Infrastucture_as_cloud_p
Assigned Numbers - 2025 - Bluetooth® Document
1. Introduction to Computer Programming.pptx

Inheritance C#

  • 1. Inheritance Types of inheritance Implementing inheritance Interface
  • 2. TYPES OF INHERITANCE  Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions.  Interface inheritance means that a type inherits only the signatures of the functions and does not inherit any implementations.
  • 3. IMPLEMENTATION INHERITANCE  A class derive from another class syntax: Class MyDerivedClass: MyBaseClass { //function and data memebers here}  A class derive from interface,the list of base class and interfaces is separated by commas syntax: Public class MyDerivedClass: MyBaseClass, Interface1,Interface2 {//function and data memebers here}
  • 4. VIRTUAL METHODS  By declaring a base class function as virtual, you allow the function to be overridden in any derived classes. Class MyBaseClass { public virtual string VirtualMethod( ) { return “this method is virtural and defined in MyBaseClass” ;} }  It is also permitted to declare a property as virtual. Public virtual string ForeName { get { return foreName;} set { foreName=value;} }
  • 5. 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 hide the base class version.  Abstract classes and Functions:  An abstract class cannot be instantiated,were as an abstract function does not have an implementation,and must be overriden in any non-abstract derived class  Abstract function is automatically virtual.
  • 6. SEALED CLASSES AND METHODS  C# allows classes and methods to be declared as sealed.  In case of class this means that you can’t inherit from that class syntax: sealed class Class_Name { // etc }  In case of method syntax: class MyClass: MyclassBase { public sealed override void FinalMethod() { // etc } } class DerivedClass: MyClass { public override void FinalMethod() // wrong..will give compilation error { // etc } }
  • 7. Constructors of Derived Classes  Adding a constructor in a Hierarchy  Adding a constructor with parameters to a Hierarchy
  • 8. MODIFIERS  Modifiers can indicate the visibility of a methods. MODIFIER APPLIES TO DESCRIPTION public Any type or members The item is visible to any other code. protected Any member of a type, also The item is visible only to any nested type any derived type. internal Any member of a type, also The item is visible only with any nested type in its containing assembley. private Any types or memebers The item is visible only inside the type to which it belongs protected internal Any member of a type, also The item is visible to any any nested type code within its containing assembly and also to any code inside a derived type
  • 9. MODIFIER APPLIES TO DESCRIPTION new Function members The member hides an inherited member with the same signature static All members The member does not operate on a specific instance of the class virtual Function members only The member can be overridden by a derived class abstract Function members only A virtual member that defines the signature of the member, but doesn’t provide an implementation. override Function members only The member overrides an inherited virtual or abstract member sealed Classes, methods, and class cannot be inherited, member properties overrides an inherited virtual member, but cannot be overridden by any members in any derived classes extern static [DllImport] The member is implemented methods only externally, in a different language.
  • 10. INTERFACES syntax: public interface Interface_Name { void method(); }  You can never instantiate an interface, it contains only the signatures of its members.  An interface has neither constructors nor fields and also not allowed to contain operator overloads.  An interface members are always implicitly public, and cannot be declared as virtual or static.