Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.

Similar presentations


Presentation on theme: "Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and."— Presentation transcript:

1 Inheritance

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. Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. With implementation inheritance, a derived type adopts the base type’s 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. 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. 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. 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. 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. 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. C# does not support multiple implementation inheritance. On the other hand, it does allow types to derive from multiple interfaces. 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. This means that a C# class can derive from one other class, and any number of interfaces.

5 Structs and Classes structs don’t really support implementation inheritance, but they do support interface inheritance. structs don’t really support implementation inheritance, but they do support interface inheritance. Structs are always derived from System.ValueType. Structs are always derived from System.ValueType. They can also derive from any number of interfaces. They can also derive from any number of interfaces. Classes are always derived from one other class of your choosing. Classes are always derived from one other class of your choosing. They can also derive from any number of interfaces. 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, C# does not support private inheritance, hence the absence of a public or private qualifier on the base class name 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: 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. // etc.} For a struct, the syntax is as follows: 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. 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 Is same as in class MyClass // derives from System.Object { // etc. } For the sake of simplicity, the second form is more common. 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: 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() public virtual string VirtualMethod() { return “This method defined in MyBaseClass”; return “This method defined in MyBaseClass”; }}

10 It is also permitted to declare a property as virtual. 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. 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: 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. In Java, by contrast, all functions are virtual. it requires you to declare when a derived class’s function overrides another function, using the override keyword: it requires you to declare when a derived class’s 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. Neither member fields nor static functions can be declared as virtual. Example: virtual1.cs Example: virtual1.csvirtual1.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. 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 Example: virtual2.csvirtual2.cs

14 Calling Base Versions of Functions Example: virtual3.cs Example: virtual3.csvirtual3.cs

15 Abstract Classes and Functions An abstract class cannot be instantiated, while an abstract function does not have an implementation, and 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. Must be overridden in any non-abstract derived class. Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error). Obviously, an abstract function is automatically virtual (although you don’t 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. 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. C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method. In the case of a method, this means that you can’t 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. 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 isn’t by itself sufficient to initialize the class—the constructors of the base classes must also be called. The constructor of the class you instantiate isn’t by itself sufficient to initialize the class—the constructors of the base classes must also be called.

20 public class B { private String name; private String name;} public class D : B { double x; double x;} public class MainClass { static void Main() static void Main() { B b = new D(); 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. 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. Notice also that in this process, each constructor handles initialization of the fields in its own class. That’s how it should normally work, and when you start adding your own constructors you should try to stick to that principle. That’s 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 = “ ”; }OR public GenericCustomer() { name = “ ”; }

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

24 What’s 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. What’s 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, we’ve made it inaccessible to the derived class. By declaring that constructor as private, we’ve 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. 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, it’ll again find that it can’t because a no parameter base class constructor is not available. 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, it’ll again find that it can’t because a no parameter base class constructor is not available.

25 Modifiers Visibility 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. 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. 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. 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 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. 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. 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. 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 Example: interface.csinterface.cs

31 Interface references can in all respects be treated like class references—but the power of an interface reference is that it can refer to any class that implements that interface. Interface references can in all respects be treated like class references—but 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: For example, this allows us to form arrays of interfaces, where each element of the array is a different class: IBankAccount[] accounts = new IBankAccount[2]; accounts[0] = new SaverAccount(); accounts[1] = new GoldAccount(); // If implemented IBankAccount Note, however, that we’d get a compiler error if we tried something like this Note, however, that we’d get a compiler error if we tried something like this accounts[1] = 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. 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. 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. Failure to implement all of these methods will result in a compilation error.

34 Example: derInterface.cs Example: derInterface.cs derInterface.cs derInterface.cs


Download ppt "Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and."

Similar presentations


Ads by Google