• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overriding

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How R u ?
Pls explain me the following concept.
Question 1.
class A3 {
void hello() {
System.out.println("Hello from A3");
}
void hello(int i) {
System.out.println("Hello from A3 " + i);
}
}
class B3 extends A3 {
void hello() {
System.out.println("Hello from B3");
}
}
class C3 extends B3 {
void hello(String s) {
System.out.println("Hello from C3 " + s);
}
}
class D3 extends C3 {
void hello() {
System.out.println("hello from D3 " );
}
}
class MethodOverriding3 {
public static void main(String args[]) {
A3 a3 = new A3();
a3.hello();
A3 b3obj = new B3();
b3obj.hello();
A3 obj = new C3();
obj.hello();
C3 c3 = new C3();
c3.hello();
A3 d3 = new D3();
d3.hello();
}
}
Answer 1:
Hello from A3
Hello from B3
Hello from B3
Hello from B3
hello from D3
Now very similar But with little change in void hello() method of class C3.
Question 2:
class A3 {
void hello() {
System.out.println("Hello from A3");
}
void hello(int i) {
System.out.println("Hello from A3 " + i);
}
}
class B3 extends A3 {
void hello() {
System.out.println("Hello from B3");
}
}
class C3 extends B3 {
void hello() {
System.out.println("Hello from C3 " );
}
}
class D3 extends C3 {
void hello() {
System.out.println("hello from D3 " );
}
}
class MethodOverriding3 {
public static void main(String args[]) {
A3 a3 = new A3();
a3.hello();
A3 b3obj = new B3();
b3obj.hello();
A3 obj = new C3();
obj.hello();
C3 c3 = new C3();
c3.hello();
A3 d3 = new D3();
d3.hello();
}
}
Answer 2:
Hello from A3
Hello from B3
Hello from C3
Hello from C3
hello from D3

Pls Can any one explain me this concept ?
Thankx.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tejas,
This concept is known as "Operator Overloading".
First off, since you're obviously playing with inheritance and variable references, please note that this nothing do with your declarations of variables a3, b3obj, obj, c3 and d3 ; All these variables could be declared to be of type A3 and you'd get the same result.
So, what is operator overloading? Simply put, a method-name by itself does not uniquely identify a function. Instead, a method is identified by both method-name and argument list.
In your example, the methods "hello()" and "hello(String s)" are two TOTALLY unrelated methods.
So, looking at class C3 in your first example, what methods does it have? Answer: "hello()" inherited from B3, and "hello(String s)" newly defined.
So, when you call "hello()" on a C3 object, it is the method inherited from B3 that is invoked - hence "hello from B3" is printed.
In your second example, where you've changed "hello(String s)" to "hello()", this method now OVERRIDES the method inherited from B3. So, now when you call "hello()" on a C3 object, it is now this method that is invoked.
Hope this helps
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify - it's actually method overloading and overriding.
Operator overloading is when you overload one of the operators (+-*% etc) so that it does different things depending on the type of operands it has. Java doesn't let you do this as a programmer although there is some built in operator overloading in Java - particularly, the overloading of '+' to allow concatenation of Strings
eg 1+3 the + adds the numerical values to produce 4.
hel+lo the + concatenates the string to producte hello.
Hope this helps,
Kathy
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy,
you said in your first explanation that C3 has just 2 methods- one that is iinherited from B3 and other one which is newly created.
Now since B3 is inherited from A3, it inherits all of its(A3) methods and these methods are further passed down to C3. Therefore C3 has a total of 3 methods - void hello(), void hello(String s) and void hello(int i).
Is that true?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am agree with veneet Sharma. Yes C3 has three methods void hello(), void hello( String s) and void hello( int i );. Actually this is also related with polymorphism. In the same class, methods are being overloaded. But how you can find your
different method when it has same name. Acutally based on method's arguments. Look,
Class A3 has two method but different arguments:

void hello() - there is no arg.
void hello(int i) - there is an int arg.
So you can find your method based on argument and you can override your method if it has same type arguments. So
Class B3 extends A3 and can override A3 class's method easily
as it has same type arg. method. When C3 extends B3 and if
C3 has same method as B3, automatically it will override B3's
method ie. A3 as same type of method arg.
But when C3 extends B3{}, it has ccome different method, i.e.
void hello( String s) , which couldn't override B3's method as
B3 has different method. So C3 adds extra one method instead of
overriding.
This example is very important to know about polymorphism,
overloading method but different arguments and inhertance
class and also overriding method.
Thanks,
G. Newaz

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this thread to Certification Study forum.
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic