Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

y null????

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can anyone tell me why the following code's output is "null" instead of "Person"?

class Person{
private String sName="Person";
public String getName(){
return sName;
}
public void setName(String sName){
this.sName=sName;
}
}
class User extends Person{
private String sName;
public String getName(){
return sName;
}
public void setName(String sName){
this.sName=sName;
}

}

public class Killer{
private String sName;
public static void main(String argv[]){
new Killer();
}
Killer(){
Person p = new User();
System.out.println(p.getName());

}
}

thanks in advance!!!
regards
pbg!!!
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because p is a User instance, whose name is never set
(both User and Person have private String sName)
 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
Because p is a User instance, whose name is never set
(both User and Person have private String sName)



thanks Satou....how it works even sname is declared as private in all the classes?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pb gaffor:


thanks Satou....how it works even sname is declared as private in all the classes?



p is reference variable of Person type but its object is of User type..

So, at compile it will look for getName in Person class. But at Runtime, the getName() in User Class will get executed and since sName is User class is not initialized the default (null) will get printed
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable p in killer class constructor is a base class reference and it is made to refer derived class (User). when the base class reference refers to a derived class and a method is invoked using the reference then the derived class method is called. Hence the getName() method in User class is called. The method returns SName which is the User class variable. This variable is not yet initialized in the User class. Hence it has null value which is default initiaization for strings. The SName of user is different from the SName for Person. Initializing for Person is not sufficient for User.
 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u
 
reply
    Bookmark Topic Watch Topic
  • New Topic