Lecture 10 - Inheritance (vb 2008)

  • Uploaded by: curlicue
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 10 - Inheritance (vb 2008) as PDF for free.

More details

  • Words: 817
  • Pages: 12
Lecture 10: Inheritance

Outline 

In the previous lecture, we introduced Objects 

And discussed the basics of Object-Oriented Programming… 





Defining Objects: Classes

We also discussed Console Applications: 

When to abandon a GUI-based Approach. 





Members, Methods, Properties, etc

In favor of a command-line interface.

Our Example: The J_Train Class

We now continue our discussion of O.O. Programming: 

Defining Constructors, which allow:  



Convenient encapsulation of default characteristics. Clients to create new Object instances with specified characteristics.

The concept of Inheritance 

By which we may create ‘derived Classes’... 



Which inherit and expand upon the characteristics of a Base Class

Our Example: The JFreightTrain Class 

Which inherits from JTrain.

Constructors 

Earlier, we created a single JTrain instance (JTrain001)… 

And provided it with natural, default characteristics…  



However, what if we wanted to create another JTrain instance…? 



Based automatically on the initialized Member values (not very elegant). We then set the desired characteristics of JTrain001 manually. But, we want the new instance to start its life with different values…

Object characteristics may be set on instantiation via a Contructor : 

A special-purpose Method defined for Initialization only.  

This Method is always called New(). Once defined, New() is called each time a new instance is created… 



Note: Earlier, New() was called implicitly when making our JTrain001…  

Even though we had not yet defined any Constructor… How so? The System handed us a default (and empty) New() Method… 



Which gave us a JTrain instance with Members set to our default values.

We can define several New() methods…each with a different Parameter set.  



Using the New Keyword.

However, we should always give a zero parameter New() method (for inheritance!). For instance: a 0-parameter New() constructor + a detail-specific constructor.

Now, let’s add two such Constructors to our JTrain Class…

J-Train Ex.: Adding Constructors

J-Train Ex.: Constructor (cont)

Inheritance 

As we noted, a strength of O.O. Programming is Code Reusability 

After defining the JTrain Class, we can instantiate (make) any number. 



For instance, consider ‘derived classes of things’ in the real world…  





Possessed by a more general kind of of thing (e.g., animal). Only possessed by the specific kind of thing (e.g., bear).

The concept of Inheritance neatly captures this relationship: 

A new Class can begin with all the characteristics of a more general class. 





More characteristics are then added, to quickly compose a more specific class.

This is accomplished in VB.NET using the keyword, Inherits. 



A Bear is a kind of of Animal. A ‘Freight Train’ is a more specific kind of ‘Train’

Such ‘derived’ classes broadly have two types of characteristics: 



However, the concept of reusability can be extended…

Such a derived Class is said to Inherit from the original (base) Class.

Again, this is very similar to real-world objects…

To illustrate, let’s make our own JFreightTrain as a type of JTrain… 

With the new capacity to carry Wagons that hold freight.

Inheritance Ex.: JFreightTrain

Inheritance Ex.: JFreightTrain

JFreightTrain (cont.)

Changing Default Instantiation 

A Base Class usually defines a zero-parameter New() Method 

It will be called (implicitly) by the system, for derived classes.  

Providing Default Instatiation details for instances. Ex: Our JFreightTrain somehow was Blue with 3 cars (mysterious…) 



It is often desirable to expand or modify this behavior: 



Since Derived Class instances generally have additional characteristics.

Defining a new Constructor for a derived class is straightforward...  

No special keywords are required (just define it, normally). But (!!)… If the Base Class defines a zero-argument Constructor…  



This Base Class Constructor will be called FIRST, automatically. Combined Effect: BaseClass Constructor Effect + new Constructor Effect!

If we wish to use a different constructor of the Base Class, instead… 

We can do this on the new constructor’s 1st line: MyBase.New( param_list ) 



Why? Because the System called BaseClass.New() automatically!

In this case, the zero-parameter BaseClass Constructor is not called.

To illustrate, let’s add two Constructors to JFreightTrain…

Example: JFreightTrain (cont.)

Conclusion / Forward 

In the last lecture, we continued our discussion of Objects… 

Creating and Using Constructors:  



Used to create object instances with differing characteristics. Example: JTrain (cont.)

Inheritance 

Used to create ‘derived Classes’... 







Which inherit the characteristics of a Base Class

Example: The JFreightTrain Class

We also discussed Overriding Base Class Constructors

Next lecture, we continue our discussion of overriding: 

With Polymorphism, a simple but powerful concept…  



Form 1 : Overriding Base Class Methods and Properties. Form 2 : The Ability of Methods to Accept ‘Related’ Classes.

We also present a generalized Multiple_JTrain Example.

Related Documents


More Documents from "curlicue"