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

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

What's hot (20)

C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Java packages
Java packagesJava packages
Java packages
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

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

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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.