Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
From the author of

Inheritance

Inheritance is the ability of one class to use properties and methods that are defined and implemented in another class. The object from which another “inherits” properties and methods is called the base class. The class that uses these properties and methods in the base class is called the derived class. Consider the object model shown in Figure 1.

Figure 1 A derived class uses the properties and methods of the base class.

The model describes a scenario in which the class, Person is a base class to the derived class, Musician. When a programmer makes an object of type, Musician, that object has Instrument and MasteryLevel properties as well as a Play() method. In addition, the object has FirstName, LastName and DOB properties and the Talk(), Sleep() and Play() methods of the base class, Person. Notice that both the base class and the derived class have a Play() method. In the OOP paradigm, the Play() method of the Musician class overrides the Play() method of the Person class. We will look at overriding methods in a later section. The listing below shows the Java code for the Person class.

import java.util.*;
public class Person{

    private String FirstName = new String();
    private String LastName = new String();

    private Date DOB = new Date();
        public void setFirstName(String firstName){FirstName = firstName;}
    public String getFirstName(){return FirstName;}
    
    public void setLastName(String lastName){LastName = lastName;}
    public String getLastName(){return LastName;}
    
    public void setDOB(Date dob){DOB = dob;}
    public Date getDOB(){return DOB;}
    
    public String talk(){
        String s = new String("I am a Person talking");
        return s;   
    }
    
    public String sleep(){
        String s = new String("I am a Person sleeping");
        return s;
    }
    
    public String play(){
        String s = new String("I am a Person playing");
        return s;
    }
}

When you look at the above code listing, notice that the properties shown in the object model are coded as methods, with each method name prefixed with the characters set or get. The coding convention conforms to the Java Bean specification, which requires that a property name be prefixed with set to accommodate writing data to the property and the get to accommodate reading the property’s value. This is very much a Java thing. VB programmers are accustomed to using Property Get and Property Let methods to accommodate reading from and writing to a property. The following shows the Java code for the Musician class:

public class Musician extends Person
{
	private String Instrument = new String();
	private int MasteryLevel;

	//Simple constructor
	public Musician(){}

	//Constructor that allows you to indicate instrument
	public Musician(String instrument){
	    Instrument = instrument;
	}

     //Constructor that allows you to indicate an instrument
    //and mastery level
	public Musician(String instrument, int masteryLevel){
	    Instrument = instrument;
	    MasteryLevel = masteryLevel; 
	}

	public String getInstrument()
	{
		return Instrument;
	}

	public void setInstrument(String instrument)
	{
	    Instrument = instrument;
	}
    
    public void setMasteryLevel(int masteryLevel){
        MasteryLevel = masteryLevel;    
    }
    
    public int getMasteryLevel(){
        return MasteryLevel;
    }
    
    public String play(){
        String s = new String();
        s = "My mastery level is " + String.valueOf(MasteryLevel) + "\n";
        s = s + "I am playing the " + Instrument;
        return s;
    }
}

The above listing is an example of inheritance. This listing contains many OOP concepts, some of which might be a bit vague to you at this point. We will cover these concepts in the following sections. The important thing to observe at this time is that the class, Musician, inherits all the properties and methods of the base class, Person, by using the Java keyword, extends.

At this time, Visual Basic does not support inheritance. This is unfortunate because inheritance is a very powerful, versatile programming technique. While it is true that VB allows you to sort of do inheritance by doing interface implementation using the Implements keyword, it is not full-fledged inheritance. Interface implementation doesn’t really pick up any behavior or state from the base class. (For more details about using the Implements keyword, you should go to the following URL on the Microsoft MSDN site: http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbenlr98/vastmimplementsx.htm)

The next version of VB promises to support full inheritance. According to prerelease documentation, the next version of VB will introduce the Inherits keyword. This keyword allows a class to inherit all the properties and methods of a base class.

Below is an abridged example of the expected use of the keyword Inherits. Please remember that this listing is based on prerelease information and is subject to change.

Option Explicit

Inherits Person

Private mvarMasteryLevel As Integer
Private mvarInstrument As String
.
.
.
Public Property Let Instrument(ByVal vData As String)
    mvarInstrument = vData
End Property

Public Property Get Instrument() As String
    Instrument = mvarInstrument
End Property

Public Property Let MasteryLevel(ByVal vData As Integer)
    mvarMasteryLevel = vData
End Property


Public Property Get MasteryLevel() As Integer
    MasteryLevel = mvarMasteryLevel
End Property
.
.
.
  • + Share This
  • 🔖 Save To Your Account