• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Method question !!

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello my question.
considering the code :

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);
}}
prints A!
c1 is the reference to the class C also if the type of c1 is A;
so i know that the invoked method is m1 of class C and the result i believed was C instead of A.
Thanks in advance.

Sorry for my languages
Gabriele
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ur object "c1" is of type A, so it has access to all methods in class A... Is that not clear? If so, just tell us ur doubt on it... We will solve it as much as we can....
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello!
what i understand is that ... ur doing overloading here not overriding ... so your methods will not show polymorphism
when u call
c1.m1(c2)
here c1 is of type A and the passing argument is of type C
and because in m1(A a) of class A, an instance of C can be passed in m1(A a) as C is a subclass of A
so it prints A
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gabriele,
Welcome to JavaRanch.

Sorry for my languages.


I really appreciated your previous post where you were looking for italian guys. JR is a place where you'll find people coming from all parts of the world (Italy included ) who share a passion : Java.
The common language we use to understand each other here is English. And yours is very understandable.
BTW, far better than your code snippet !
JavaRanch uses UBB codes to help us in formatting our messages. The tag used for code formatting is "CODE".
For your convenience, here is the list of UBB codes.
Best,
Phil.
[ December 17, 2003: Message edited by: Philippe Maquet ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gabriele,
I was confused about the same question. Marlene Miller explained it pretty clearly so I'll refer you to her response to my question.
The link is here
 
gabriele rigamonti
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now is Clear!!
Thanks!!
Gabriele
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic