English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

If the above statement is correct means, Explain with example.

2006-11-02 20:42:44 · 2 answers · asked by Albin Xavier 1 in Computers & Internet Programming & Design

2 answers

here you go:
http://www.phptr.com/articles/article.asp?p=26430&rl=1

///

2006-11-02 20:45:49 · answer #1 · answered by jan 7 · 2 0

When you overload a method, you create a new method (in a subclass) with the same name but with a different set of parameters - so you have two different method signatures - one from the sub class you just defined, and the one from the original class you just extended.

When you override a method, you replace the method from the original class with another in your subclass. That way only the method from the subclass can be called when you have an instance of that subclass. The method from the extended class is not inherited, because it has been overwritten.


class ParentClass {

public void overloadedMethod(String msg) {
System.out.println("ParentClassOverloadedMethod: " + msg);
}

public void overriddenMethod(String msg) {
System.out.println("ParentClassOverridenMethod: " + msg);
}
}

class SubClass extends ParentClass {

public void overloadedMethod(String msg, String level) {
System.out.println("SubClassOverloadedMethod: [" + level + "]" + msg);
}

public void overriddenMethod(String msg) {
System.out.println("SubClassOverridenMethod: " + msg);
}

}

When you have an instance of SubClass:

SubClass myClass = new SubClass();

both overloaded methods are available:

myClass.overloadedMethod("Hello World");
myClass.overloadedMethod("Hello World", "greeting");

but only the SubClass' overridden method is callable:

myClass.overridenMethod("Hello World");

The ParentClass' overriddenMethod() cannot be called using an instance of the subclass, because it has been overridden by a method in the SubClass with the exact same method signature (method name, scope and arguments are the same)

2006-11-03 04:56:58 · answer #2 · answered by Isofarro 3 · 1 0

fedest.com, questions and answers