CSC/ECE 517 Fall 2011/ch1 1i lj

From Expertiza_Wiki
Revision as of 00:09, 9 September 2011 by Jmiller5 (talk | contribs)
Jump to navigation Jump to search

Reimplementation of Methods in Descendant Classes


Introduction

In object-oriented programming, a modified version of one class can be derived from an existing class and it will inherit all of the fields and methods from the existing class. This concept is known as subclassing or implementation inheritance. The derived class (also called extended, descendant, child, or subclasses) will automatically inherit all of the fields and methods from the superclass (also called ancenstor, parent, or base classes). Once a subclass is created, the methods and fields of the superclass can be used or replaced by methods and fields in the subclass if desired. The various object-oriented programming languages have different capabilities when it comes to inheritance and method reimplementation. This wiki article intends to explain the method implementation capabilities of Java, Ruby, and C++.

Overriding Methods

Method overriding[1], in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The reimplementation of the method in the subclass overrides (replaces) the implementation in the superclass. It is generally still possible to refer to the superclass version of the method - even within the subclass version of the method - if needed.

Java

Inheritance in Java

In Java, each class can only inherit directly from a single superclass, which is referred to as single inheritance. Additional subclasses can be created from existing subclasses, however. A descendant class is created using the extends keyword.

Method Overriding and Super

One typical use for implementation inheritance is to extend or modify the capabilities an existing class so as to add or slightly change its functionality, which is known as “specialization.” This can allow a subclass to use most of the features of a superclass, but modify the behavior somewhat to get a desired output without the need to duplicate coding. In Java, a method in the subclass is said to “override” a superclass method when the subclass version has the same signature (name, parameters, and return type) as the superclass version. The subclass version of the overridden method (called an “instance method”) will only be executed when an object of the subclass type calls that method. Any other calls to the method from objects of the superclass type will use the superclass version of the method. The example below shows the distinction between an overridden version of a method in a subclass versus the original method in the superclass. The superclass is Sport and has the class method, and the Subclass is Basketball, which has the instance method. In this example, Basketball extends Subclass.

The super keyword can be used in an instance of the subclass to call back to the the overridden version of the method in the superclass, instead of using the overriding instance method in the subclass. Furthermore, super must be used in the subclass if it is using a parameterized method from the superclass.

public class Sport {
    public static void exampleClassMethod() {
        System.out.println("Class method in Sport superclass.");
    }
    public void testInstanceMethod() {
        System.out.println("Instance method in Sport.");
    }
}

public class Basketball extends Sport {
    public static void exampleClassMethod() {
        System.out.println("The class method in Basketball.");
    }
    public void testInstanceMethod() {
	super.testInstanceMethod();   //call to the superclass method
        System.out.println("The instance method in Basketball.");
    }

    public static void main(String[] args) {
        Basketball myBasketball = new Basketball();
        Sport mySport = myBasketball;
        Sport.exampleClassMethod();
        mySport.testInstanceMethod();
	}
}

The output of this code is:
Class method in Sport superclass.
Instance method in Sport.
Instance method in Basketball.


Overloading Methods

While not explicitly related to the distinction between methods in a subclass and a superclass, method overloading is another use of polymorphism in Java. The language is able to distinguish between methods in a class that have the same name but are implemented with different signatures inside of the same class. The actual method that is called will depend on which parameters are sent. The methods cannot have the same number and type of parameters, even if they have a different return type. One such overloaded method is the print() method in the java.io.PrintStream class[2]:

public void print(Boolean B)
public void print(int i)
public void print(String S)
etc…

Ruby

Inheritance in Ruby

In Ruby, a class can only have one direct superclass, so it is considered a single inheritance language. However, Ruby's classes can access the functionality of mixins, as a sort of simulated multiple inheritance. Mixins allow a class to implement methods from different modules, if the module has defined instance methods.

C++

In native C++, a derived class function having the same name and parameters as a base class virtual function will *always* override it. In C++/CLI you have the option of using the new contextual keyword to specify whether you want to override a base class function or hide it. Example:

ref class Base
{
public:
    virtual void Goo()
    {
        Show("Base::Goo");
    }
    
    virtual void Boo()
    {
        Show("Base::Boo");
    }
    
    virtual void Doo()
    {
        Show("Base::Doo");
    }
};

ref class Derived : Base
{
public:
    //Overrides Base::Goo

    virtual void Goo()
    {
        Show("Derived::Goo");
    }
    
    //Overrides Base::Boo as above

    virtual void Boo() = Base::Boo
    {
        Show("Derived::Boo");
    }
    
    //Hides Base::Doo

    virtual void Doo() new
    {
        Show("Derived::Doo");
    }
};

Here's some sample code that invokes the above methods on a Base handle referencing a Derived object.

void _tmain()
{
    Base^ r = gcnew Derived();
    r->Goo();
    r->Boo();
    r->Doo();
}


You'll get the following output :

Derived::Goo
Derived::Boo
Base::Doo

C++/CLI

Introduction When Microsoft brought out the Managed Extensions to C++ with VS.NET 7, C++ programmers accepted it with mixed reactions. While most people were happy that they could continue using C++, nearly everyone was unhappy with the ugly and twisted syntax offered by Managed C++. Microsoft obviously took the feedback it got very seriously and they decided that the MC++ syntax wasn't going to be much of a success. On October 6th 2003, the ECMA announced the creation of a new task group to oversee development of a standard set of language extensions to create a binding between the ISO standard C++ programming language and Common Language Infrastructure (CLI). It was also made known that this new set of language extensions will be known as the C++/CLI standard, which will be supported by the VC++ compiler starting with the Whidbey release (VS.NET 2005). Renamed Overriding: Native C++ insisted that the derived class method name must match the name of the base class virtual method that it is overriding. C++/CLI allows us to have a derived class method override a base class virtual method even if the derived class method name does not match the base class method name. Of course the method signatures must be equivalent. The following code snippet should make things clear.


C#