• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

overriding

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}

Please explain why it will print A???
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anshi,


Please explain why it will print A???



Its not overriding at all. Its just overloading. Overriding means exactly the same signature has to be followed in all the subclasses. But in your case, you have overloaded the method m1() with different Objects.

During runtime, the compiler checks the reference type only and NOT the type of the actual instance which the reference variable is pointing to. So eventhough you assign the instance of C class, the compiler checks the class type of the reference "c1" which is of type class A. So it decides to run the method present in class A.

The argument to the method m1() in class A accepts the Object of type A. What you pass at runtime is the object C, which is anyways allowed as C is a subclass of A. So, the compiler happy decides to run the m1() method in class A.

If you want to have the overridden effect, just have the method m1(C c) with the argument of type C in class A as well. Then it becomes the overridden method in class C. Look at the example below.



The above code will now print "C" which is present in the class C since its overridden.

HtH.
[ July 11, 2007: Message edited by: Raghavan Muthu ]
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!
In the main method see the statement

A c1=new C();

here c1 is the reference of class A but object of class C.
Later in the second statement

C c2=new C();
c2 is an object of class C

while in the third statement
c1.m1(c2);

reference of class A that is c1 will invoke the method in class A.i hope it helps...
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks the overridden example quoted helped in clearing 2 concepts easily.


Please let me know a good mock papers site besides danchilom..
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic