CSC/ECE 517 Fall 2011/ch1 1i zf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 273: Line 273:
}
}
</pre>
</pre>
== References ==
*[1]: [http://en.wikipedia.org/wiki/Method_(computer_programming)]
*[2]: [http://www.wisegeek.com/what-is-an-abstract-method.htm]

Revision as of 02:53, 7 September 2011

CSC/ECE 517 Fall 2010/ch1 1i zf


Introduction

In OO languages, method (http://en.wikipedia.org/wiki/Method_(computer_programming)) is a subroutine which associates with the instances of the class to define the specific behavior of this class. In the inheritance of these languages, methods reimplementation is essential because descendant class inherited from the ancestor class in which properties and method have been defined to some extent. With the method reimplementation, coding can be more efficient and simple without rewriting the same code again and agian. However, in different OO languages, the ways of acquiring or allowing method reimplementation are significantly different. In this article, we are going to introduce these differences and provide code for better understanding.

Principle of method reimplementation in descendant class

Method reimplementation is various in different OO languages according to their own programming rules and complier environments. And it also associated with inheritance between ancestor class and descendant class. Several terms of method are listed below.

Abstract method

Abstract method is a method only has name but little or no implementation in the ancestor class, which must be also an abstract class. In most of the case, codes in abstract method are considered dummy but it does not mean useless. In fact, it provides a place for descendant class which inherited from the ancestor class to define specific behaviors and actions.

For example, a toy company develops an ancestor class called “animal” which contains “dog”, “bird” and “fish” as its descendant classes. These subclasses have same characteristic such as color, size, price and behavior like move, speak. Here we use the abstract methods for move and speak so that the subclasses can define their own behaviors which act differently.

Virtual method

Virtual method, or virtual function, is the method that first declared or defined in the ancestor class and then completed defined in the descendant class. Based on the functionality in the subclasses, virtual method can be simply defined or overridden with details. In a virtual method in which only has the name but no implementation, we call this as pure virtual method, as known as abstract method introduced above.

Overridden method

Overridden method, as mentioned in virtual method, is used in OO language involving inheritance hierarchy. It allows descendant classes to redefine the method in their ancestor class with more details and particular purposes. “Override” means the implementation in subclasses rewrites or redefines the original implementation of the method in their super class without changing the name, parameter and return type. To better demonstrate how overridden method work, we take the toy company example again. In the behavior aspect of “animal”, we have the method call “move”. In this method, we know it define the same of movement for all the animals. However, the way of movement among “dog”, “bird” and “fish” are different. Thus, in “dog” class, we may redefine the detail in move method as “I use leg”; in “bird” class, the detail of move method is “I use wings”; and in “fish” class, the detail of move method is “I use fin”.

Overloaded method

Overloaded method means that method share the same name with other method but may be different in other aspects such as parameter, number of parameter and data type. And the correct method will be executed automatically during the runtime depends on how it is called.

Method reimplementation in different languages

Smalltalk

In Smalltalk language, we can use the inheritance to reimplementation the methods. Inheritance is very useful in Smalltalk which makes the Smalltalk language reusability and extensibility. There are two ways to modify the behavior of the methods in Smalltalk by inheritance. One is adding new methods and the other is overriding the inherited method.

Adding Methods

Adding new method is very simple; we can add either instance or class methods, to the class definition. The following diagrams show how the adding method works:

ClassRoom’s Methods
Room instance methods
Room_Number
Room_Size
Open_Window
Open_Door
ClassRoom instance methods
Turn_On_Computer
Clean_Blackboard

The ClassRoom class is specific types of Room. The ClassRoom class is inheriting from Room Class, so the new methods Turn_On_Computer and Clean_Blackboard will add to the ClassRoom class. Now ClassRoom class definition can now support all of the messages or methods in Room and the two new messages.

Overridden method

Overriding an inherited method is very important way to provide unique behaviors to a class. If an object receives a message which we have a method in the class definition for that massage, the object will work its way up the hierarchy until it finds a method with that name. We cannot delete the inherited methods, if we do not need the method in the superclass, we can provide a method by the same name with the superclass in the subclass with no code in the method. In this way, we can simply replace the behavior of the superclass behavior by no behavior.

Java

We can use the inheritance to make the method reimplementation in Java. They are similar with the ways we do in Smalltalk, like the overriding and overloading. But in Java, we can define abstract method and virtual method in Java, and it provides a @override tag that prevents the programmer from thinking another method is being overridden when really it is not.

Overridden method

If we want to override a method in subclass that inherited from the superclass, we should invoke the superclass method by using the keyword super. Below is the example:

public class Say {
	public void express() {
		System.out.println(“Hello wolrd!”);
}
}

public class Shout extends Say {
	@Override // @Override annotation in Java 5 is optional but helpful.
	public void express() {
		System.out.println(“Hello wolrd, loudly! ”);
}
}

Class Say represents the superclass and implements a method call express (). The subclass called Shout will inherit all the methods which in the Say class. But the class Shout will override the method express in Shout class, and replace it with new action.

Say hello_say = new Say();
hello_say.express();     // Prints “Hello wolrd!”

Say hello_shout = new Shout();   //Polymorphism
hello_shout.express();   //Prints “Hello wolrd, loudly!”

The super can be used to call the superclass's method; we can modify the class in class Shout to make the method express call another express method which in superclass Say.

public class Shout extends Say {
	@Override // @Override annotation in Java 5 is optional but helpful.
	public void express() {
		System.out.println(“Hello wolrd, loudly! ”);
		Super.express();  //will call the express method in class Say
}
}

But some of the methods cannot be overridden. For example, in Java, a method that is declared final in the super class cannot be overridden, and the method declared private or static cannot be overridden too. We cannot make a class that that is declared final to become a super class too.

Overloaded method

Java supports method overloading which allows the creation of several methods with the same method name, but different in their types of input and output of the function. We can define two methods with the same name in Java:

int Add(int a, int b) {	
	return a + b;}

float Add(float a, float b) {
	return a + b;}

The two methods have the same method name in define. To call the methods, we should pass the parameters to the methods above. When we call Add(1,1) it will call the first method Add(int a, int b), because we pass two int types of the parameters to the method Add. And if we call Add(1.0,1.0) it will call the second method Add(float a, float b) because we pass the float types of the parameters to the method Add. But if we give these two methods parameters a, b with default value, it will cause an error, which would result in an ambiguous call error, as the compiler wouldn’t know which of the two methods to use.

Abstract Method

Abstract method: When a class contains an abstract method, that class must be declared as abstract. The abstract method has no implementation and thus, classes that derive from that abstract class, must provide an implementation for this abstract method.

Abstract method are the method that with no body specification. Subclasses must provide the method statements to fulfill the abstract method. Especially, if the method was one provided by the superclass, we should override them in each subclass. If we forgot to override that will cause some problem like that the applied method statement may be inappropriate. Below is the abstract method example, we need to define the abstract method in subclass to reimplementation the method.

public abstract class Vehicle  // class is abstract
{
  private String name;
  public Vehicle (String nm)      // constructor method
  { name=nm; }
  public String getName()       // regular method
  { return (name); }
  public abstract void run(); // abstract method - note no {}
}

Virtual Method

Virtual method: A class can have a virtual method. The virtual method has an implementation. When you inherit from a class that has a virtual method, you can override the virtual method and provide additional logic, or replace the logic with your own implementation. A virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. In java, all the non-static methods with the default type “virtual function”. But if the methods marked with the keyword final, which cannot be overridden. The private methods are non-virtual which cannot be inherited. Below is the example for Virtual Method:

public class toy
{
   public void say()
   {
     System.out.println(“I am toy.”);
   }

   public static void main(String[] args)
   {
     List<toy> toys = new LinkedList<toy>();
	
     toys.add(new toy());
     toys.add(new toy_man());
     toys.add(new toy_car());

     for(toy current_toy : toys)
     {
	current_toy.say();
     }
   }
}

public class toy_man extends toy
{
   @Override
   public void say()
   {
      System.out.println(“I am toy_man.”);
   }
}

public class toy_car extends toy
{
   @Override
   public void say()
   {
      System.out.println(“I am toy_car.”);
   }
}

The output will be: I am toy. I am toy_man. I am toy_car.

Interfaces

In java interfaces are very similar with the abstract classes but all the methods in interfaces are abstract and all properties are static final. As the abstract classes, interfaces also can be inherited. And we also use the extends keywords too. Different from C++, in Java we cannot use the multiple inheritances for class, which means that a subclass extends from more than one superclass. An interface can tie elements of several classes together, and it also can separate design from coding as class method headers are specified but not their body. For example we build a running interface for the subclasses of Vehicle. Because there is a method called stop(), we should define the method in any class which using the running interface.

public interface running
{
  public void stop();
} 

When we create a class that will use the interface, we should use the keywords implements which will follow one or more interface list.

public class Vehicle_Run extends Vehicle implements running
{
    public Vehicle_Run (String nm)
    {
       super(nm);    // builds ala parent
    }
    public void run ()
    {
       System.out.println("I am running.");
    }
    public void stop ()  // this method specific to Vehicle_Run
   {
       System.out.println("I stop.");
   }
}

C++

C++ is very similar with Java in method overriding and method overloading. The method overloading of C++ we can refer the method overloading in Java. But they have difference in method overriding between C++ and Java.

Overridden method

C++ does not provide the keyword super that a subclass can use in Java to invoke a superclass version of a method that it wants to override. But in C++ we can use the base class follow with the scope resolution operator to override the superclass method. Below is the example:

class Toy {
public:
	Toy(char* nm, int s):name(name),size(s){}
	virtual void print() const;
private:
	char* name;
	int size;
};

void Toy::print() const{  // print() method of base class
	std::cout<<”Name = ” << this->name << “; Size = ” << this->size;
}

class Toy_car: public Toy {
public:
	Toy_car(char * nm, int s, int t) : Toy(nm,s), type(t) {}
	virtual void print() const;

private:
	int type;
};

void Toy_car::print() const{
	Toy::print();
	Std::cout<<”; Type = ” << this-> type;
}

The main method will call the print method in class Toy and class Toy_car separately.

Int main(int argc, char** argv) {
	Toy toy_test(“Jack”, 9);
        Toy_test.print();
        //outputs:
        //Name = Jack; Size = 9

        Toy_car toy_car_test(“Ann”,”7”,”1”);
        //the pointer to the most overridden method in the vtable in on Toy_car::print
        toy_car_test.print();// but this call does not illustrate overriding
        static_cast<Toy&>(toy_car_test).print(); // this one does
        //output
        // Name = Jack; Size = 9;Type = 1
}



References