Inheritance - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Inheritance

Description:

Inheritance Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type s member fields and functions. – PowerPoint PPT presentation

Number of Views:175
Avg rating:3.0/5.0
Slides: 35
Provided by: Ajay155
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
2
Types of Inheritance
  • Implementation inheritance means that a type
    derives from a base type, taking all the base
    types member fields and functions.
  • With implementation inheritance, a derived type
    adopts the base types implementation of each
    function, unless it is indicated in the
    definition of the derived type that a function
    implementation is to be overridden.
  • This type of inheritance is most useful when you
    need to add functionality to an existing type, or
    where a number of related types share a
    significant amount of common functionality.

3
  • Interface inheritance means that a type inherits
    only the signatures of the functions, but does
    not inherit any implementations.
  • This type of inheritance is most useful when you
    want to specify that a type makes certain
    features available.
  • Interface inheritance is often regarded as
    providing a contract By deriving from an
    interface, a type is guaranteed to provide
    certain functionality to clients.

4
Multiple Inheritance
  • Some languages such as C support what is known
    as multiple inheritance, which a class derives
    from more than one other class.
  • C does not support multiple implementation
    inheritance.
  • On the other hand, it does allow types to derive
    from multiple interfaces.
  • This means that a C class can derive from one
    other class, and any number of interfaces.

5
Structs and Classes
  • structs dont really support implementation
    inheritance, but they do support interface
    inheritance.
  • Structs are always derived from System.ValueType.
  • They can also derive from any number of
    interfaces.
  • Classes are always derived from one other class
    of your choosing.
  • They can also derive from any number of
    interfaces.

6
Implementation Inheritance
  • class MyDerivedClass MyBaseClass
  • // functions and data members here
  • C does not support private inheritance,
  • hence the absence of a public or private
    qualifier on the base class name

7
  • If a class (or a struct) also derives from
    interfaces, then the list of base class and
    interfaces is separated by commas
  • public class DerivedClass BaseClass, Iface1,
    Iface2
  • // etc.
  • For a struct, the syntax is as follows
  • public struct DerivedStruct Iface1, Iface2
  • // etc.

8
  • If you do not specify a base class in a class
    definition, the C compiler will assume that
    System.Object is the base class.
  • class MyClass Object // derives from
    System.Object
  • // etc.
  • Is same as in
  • class MyClass // derives from System.Object
  • // etc.
  • For the sake of simplicity, the second form is
    more common.

9
Virtual Methods
  • By declaring a base class function as virtual, we
    allow the function to be overridden in any
    derived classes
  • class MyBaseClass
  • public virtual string VirtualMethod()
  • return This method defined in MyBaseClass

10
  • It is also permitted to declare a property as
    virtual.
  • For a virtual or overridden property, the syntax
    is the same as for a non-virtual property with
    the exception of the keyword virtual, which is
    added to the definition.
  • The syntax looks like this
  • public virtual string ForeName
  • get return foreName
  • set foreName value
  • private string foreName

11
  • In Java, by contrast, all functions are virtual.
  • it requires you to declare when a derived classs
    function overrides another function, using the
    override keyword
  • class MyDerivedClass MyBaseClass
  • public override string VirtualMethod()
  • return This is an override defined in
    MyDerivedClass

12
  • Neither member fields nor static functions can be
    declared as virtual.
  • Example virtual1.cs

13
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 hidden by the base class version.
  • Example virtual2.cs

14
Calling Base Versions of Functions
  • Example virtual3.cs

15
Abstract Classes and Functions
  • An abstract class cannot be instantiated, while
    an abstract function does not have an
    implementation, and
  • Must be overridden in any non-abstract derived
    class.
  • Obviously, an abstract function is automatically
    virtual (although you dont need to supply the
    virtual keyword doing so results in a syntax
    error).
  • If any class contains any abstract functions,
    then that class is also abstract and must be
    declared as such.
  • abstract class Building
  • public abstract decimal CalculateHeatingCost()
    // abstract method

16
  • abstract class Building
  • private bool damaged false // field
  • public abstract decimal CalculateHeatingCost()
    // abstract method

17
Sealed Classes and Methods
  • C allows classes and methods to be declared as
    sealed.
  • In the case of a class, this means that you cant
    inherit from that class.
  • In the case of a method, this means that you
    cant override that method.
  • sealed class FinalClass
  • ..
  • class DerivedClass FinalClass // wrong. Will
    give compilation error
  • .

18
  • class MyClass
  • public sealed override void FinalMethod()
  • // etc.
  • class DerivedClass MyClass
  • public override void FinalMethod() //
    compilation error

19
Constructors of Derived Classes
  • When you create an instance of a derived class,
    there is actually more than one constructor at
    work.
  • The constructor of the class you instantiate
    isnt by itself sufficient to initialize the
    classthe constructors of the base classes must
    also be called.

20
  • public class B
  • private String name
  • public class D B
  • double x
  • public class MainClass
  • static void Main()
  • B b new D()

21
  • constructors are called in order of System.Object
    first, then progressing down the hierarchy until
    the compiler reaches the class being
    instantiated.
  • Notice also that in this process, each
    constructor handles initialization of the fields
    in its own class.
  • Thats how it should normally work, and when you
    start adding your own constructors you should try
    to stick to that principle.

22
Adding a No-Parameter Constructor in a Hierarchy
  • public abstract class GCustomer
  • private string name
  • public GenericCustomer() base()
  • name ltno namegt
  • OR
  • public GenericCustomer()
  • name ltno namegt

23
  • The base and this keywords are the only keywords
    allowed in the line which calls another
    constructor.
  • Anything else causes a compilation error.
  • If base class no-parameter constructor is private
    then youll get an error in derived class.

24
  • Whats happened is that the compiler has tried to
    generate a default constructor for derived class,
    but not been able to because the default
    constructor is supposed to invoke the
    no-parameter base class constructor.
  • By declaring that constructor as private, weve
    made it inaccessible to the derived class.
  • A similar error occurs if we supply a constructor
    to base class, which takes parameters, but at the
    same time we fail to supply a no-parameter
    constructor.
  • In this case the compiler will not generate a
    default constructor for base class, so when it
    tries to generate the default constructors for
    any derived class, itll again find that it cant
    because a no parameter base class constructor is
    not available.

25
Modifiers
  • Visibility Modifiers

26
Other Modifiers
27
Interfaces
  • public interface IDisposable
  • void Dispose()
  • it is not permitted to supply implementations of
    any of the members of an interface.
  • In general, an interface can only contain
    declarations of methods, properties, indexers,
    and events.
  • You can never instantiate an interface it only
    contains the signatures of its members.

28
  • An interface has neither constructors nor fields
    (because that would imply some internal
    implementation).
  • An interface definition is also not allowed to
    contain operator overloads, because interfaces
    are usually intended to be public contracts, and
    having operator overloads would cause some
    incompatibility problems with other .NET
    languages, such as Visual Basic.NET, which do not
    support operator overloading.
  • It is also not permitted to declare modifiers on
    the members in an interface definition.
  • Interface members are always implicitly public,
    and cannot be declared as virtual or static.

29
  • class SomeClass IDisposable
  • // this class MUST contain an implementation of
    the
  • // IDisposable.Dispose() method, otherwise
  • // you get a compilation error
  • public void Dispose()
  • // implementation of Dispose() method
  • // rest of class

30
Defining and Implementing Interfaces
  • Example interface.cs

31
  • Interface references can in all respects be
    treated like class referencesbut the power of an
    interface reference is that it can refer to any
    class that implements that interface.
  • For example, this allows us to form arrays of
    interfaces, where each element of the array is a
    different class
  • IBankAccount accounts new IBankAccount2
  • accounts0 new SaverAccount()
  • accounts1 new GoldAccount() // If
    implemented IBankAccount
  • Note, however, that wed get a compiler error if
    we tried something like this
  • accounts1 new SomeOtherClass() //Error
  • //As SomeOtherClass does NOT implement
    IBankAccount

32
Derived Interfaces
  • public interface ITBAccount IBankAccount
  • bool TransferTo(IBankAccount destination,
    decimal amount)

33
  • Because ITBAccount derives from IBankAccount, it
    gets all the members of IBankAccount as well as
    its own.
  • That means that any class that implements
    ITBAccount must implement all the methods of
    IBankAccount, as well as the new TransferTo()
    method defined in ITBAccount.
  • Failure to implement all of these methods will
    result in a compilation error.

34
  • Example derInterface.cs
Write a Comment
User Comments (0)
About PowerShow.com