Presentation is loading. Please wait.

Presentation is loading. Please wait.

These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.

Similar presentations


Presentation on theme: "These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would."— Presentation transcript:

1 These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would like to suggest corrections or enhancements please drop me a note at martin@martischray.com Martin Schray http://www.martinschray.com martin@martinschray.com

2 VB.NET - Inheritance Practical Guide to Inheritance Copyright © Martin Schray 1997- 2003

3 Inheritance Defined Inheritance is building on the functionality of another class (by inheriting) Inheriting gives you all the capabilities (all non private instance variables and non private methods) of the class you inherit from (and all classes it inherits from) ALL CLASSES inherit from System.Object

4 More on Inheritance You can stop a class from being inherited by making it NotInheritable You can stop a method from being “inherited” (replaced, extended, disabled) by making it NotOverridable There is no limit on the levels of inheritance you can have (there might be a practical limit)

5 System.Object Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. The ultimate super class object is taken for granted so you don’t have to say extends Object Key methods on Object class are equals, memberWiseclone, toString, and getType

6 System.Object equals on System.Object will compare two objects point to the same area in memory –Typically this is not that useful –Many classes override equals with the own version of the method (I.e., the String class overrides it to compare the contents of the strings) –A person object might define equal to be if both object have the same SSN, and birth date

7 System.Object The toString method returns a string that represents the value of this object Most classes override the default version of toString with their own For most classes all member variables are put in the toString method result

8 How do you do Inheritance? Inheritance is really straight forward in the class declaration use the keyword inherites to indicate you are inheriting from another class

9 Animal base Class public class Animal private Sound as String public sub New(String Sound) ‘ me reference point to this object me.Sound = Sound; End sub public overrideable sub sound() End sub End class

10 Dog subclass public class Dog inherits Animal Private isAttackDog as boolean public sub New(sound as String, isAttackDog as boolean) myBase.new(sound) ‘calls Animals constructor me.isAttackDog =isAttackDog End sub public sub overrides sound() ‘override sound with my ‘own System.Console.Writeline(“bark bark”) End sub End class

11 Key Observations about Dog Class Has it own constructor (new) It’s constructor leverages the parent constructor (by calling myBase) It’s constructor does some additional work It adds it’s own instance variables It replaces the base classes sound method

12 Polymorphic options In a inherited class you can do the following things to a bases class methods –Extend (by calling myBase) and then do more work –Replace (not calling mybase) and do more work –Disable (not calling mybase) and do having no code We will look at examples of each next Method name must be the same and have the same arguments to do polymorphism

13 Extending a method public class Dog inherits Animal Private isAttackDog as boolean public sub New(sound as String, isAttackDog as boolean) mybase.new(sound); ‘ calls Animals constructor me.isAttackDog =isAttackDog End sub Public overrides sub sound() // extend sound with my own mybase.sound(); ‘ call my ancestor object method System.out.println(“bark bark”); ‘add my own code! End sub End class

14 Replacing a method public class Dog inherits Animal Private isAttackDog as boolean public sub New(sound as String, isAttackDog as boolean) mybase.New(sound); ‘ calls Animals constructor me.isAttachDog =isAttackDog End sub Public overrides sub sound() // override sound with my own System.out.println(“bark bark”); End sub End class

15 “Disabling” a method public class Dog inherits Animal Private isAttackDog as boolean public sub New(String sound, boolean isAttackDog) myBase.new(sound); ‘ calls Animals constructor me.isAttackDog =isAttackDog; End sub public overrides sub sound() ‘ override sound with my own and do nothing! End sub End class

16 Abstract classes Represent a very generic base class Often they are so generic that the inherited classes must specialize to be useful Abstract classes are a way of building in a “standard interface” (think API) to all subclasses

17 Abstract base class public MustInherit class Animal private Sound as String public New(Sound as String ) me.Sound = sound; End sub public MustOverride sub sound() End sub End class

18 Concrete subclass public class Dog inherits Animal Private isAttackDog as boolean public New(sound as String, isAttackDog as boolean) mybase.new(sound); ‘ calls Animals constructor me.isAttachDog =isAttackDog; End sub Public overrides void sound() ‘ override sound with my own ‘concrete version of this method System.console.writeline(“bark bark”) End sub End class

19 Rules for abstract classes All subclasses with remain to be abstract until all methods in the base are “implemented” in the subclass When a base class has methods that do actual work it’s better to have only the required methods be abstract. That way you only need implementations for abstract methods

20 Key Terms Abstract class – a class that is only used for design (only inherited from ~ new won’t work on it Concrete class – a class that provides all required methods to “complete” the abstract class


Download ppt "These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would."

Similar presentations


Ads by Google