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

What's hot

Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 

What's hot (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Inheritance
InheritanceInheritance
Inheritance
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
OOP java
OOP javaOOP java
OOP java
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 

Similar to Inheritance C#

Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
Shiva Cse
 

Similar to Inheritance C# (20)

Lecture 10
Lecture 10Lecture 10
Lecture 10
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Java Core
Java CoreJava Core
Java Core
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 
Interface
InterfaceInterface
Interface
 
C# interview
C# interviewC# interview
C# interview
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Class properties
Class propertiesClass properties
Class properties
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
1
11
1
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Java
JavaJava
Java
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 

More from Raghuveer Guthikonda (9)

C# String
C# StringC# String
C# String
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Generics C#
Generics C#Generics C#
Generics C#
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Regex in C#
Regex in C#Regex in C#
Regex in C#
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

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.