• 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

Doubt in overloading & overriding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have seen the following code snippet in a site

Code:
class Parent {
String message = "parent";
}

class Child extends Parent {
String message = "child";
}

public class Test {
public static void main(String[] args) {
Parent yo = new Child();
System.out.println(yo.message);
}
}

ans:Parent


I expected that it would print as "Child" because i thought that the object "yo" is the reference of Child(bacause of new Child ();)
Can anyone explain me
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jesintha jegan wrote:Hi

I have seen the following code snippet in a site

Code:
class Parent {
String message = "parent";
}

class Child extends Parent {
String message = "child";
}

public class Test {
public static void main(String[] args) {
Parent yo = new Child();
System.out.println(yo.message);
}
}

ans:Parent


I expected that it would print as "Child" because i thought that the object "yo" is the reference of Child(bacause of new Child ();)
Can anyone explain me



In this case the yo object will reflect "Parent" because the instance variables are not overriden as they belong to class hence their value is determined on the basis of class instantiated at the compile time.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jesintha jegan wrote:I have seen the following code snippet in a site



Can you please tell us which site it is. This is a rule here at javaranch that whenever you post a question from a site, you have to QuoteYourSources...
 
jesintha jegan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah sure.

Source : http://nikojava.wordpress.com/2008/10/02/scjp-mock-exam-for-overriding-overloading/
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:
class Parent {
String message = "parent";
}

class Child extends Parent {
String message = "child";
}

public class Test {
public static void main(String[] args) {
Parent yo = new Child();
System.out.println(yo.message);
}
}
---------------------------

You are accesing the instance variable (String object) not a overridden method. You will get result as child when you access a overridden method (Dynamic method despatch)

In this case to get result as "child"

write like this

Parent yo =new Child();
System.out.println(((Child)yo).message);
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do correct me If I am wrong..

Overriding is a concept which occurs at runtime basically so methods will be overridden at runtime but variables has been assigned during compile time. Thats why you will get parent in your case but if you do the same thing with a method, you will get Child's method..

Hope I am clear.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pankaj Upadhyay wrote:Overriding is a concept which occurs at runtime basically so methods will be overridden at runtime but variables has been assigned during compile time. Thats why you will get parent in your case but if you do the same thing with a method, you will get Child's method..



This concept is called polymorphism . Overriding is a way of implementing polymorphism...
 
Pankaj Upadhyay
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the correction. Basically I am not good in forming sentences thats why I always love to ask that my answer was clear or not.

you are absolutely right. This is polymorphism. Basically I was trying to help with the correct answer.

Hope I did that :P
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pankaj Upadhyay wrote:Basically I am not good in forming sentences thats why I always love to ask that my answer was clear or not.



I didn't find any problem with your sentences. Just be confident buddy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic