• 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

Dynamic

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Pls let me know the o/p of following program, but pls be sure to explain the things given out.
Thanks for u r attention.
Nilesh.
class superclass{
protected String String;
superclass(){
nameString();
}
public void nameString(){
String = "Aishwarya";
System.out.println("String: "+String);
}
}
class Q29 extends superclass{
private String String = "Priyanka";
Q29(){
nameString();
}
public void nameString(){
System.out.println("String: "+String);
}
public static void main(String[] args){
new Q29();
}
}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nilesh,
The output will be "String: null" and "String: Priyanka".
When the subclass is instantiated, the default constructor of the superclass will be called. In this case the constructor of superclass will call the method nameString(). Since the this method is overridden in the subclass, the nameString() method of the subclass will be called. At this stage String will be null, since nothing has been assigned to this variable. Note that instance variables will be initialised with their default values when they are not initialised by the programmer. Hence, the above output.
My email id is pillaiyar72@hotmail.com
R.Balasubramanian

Originally posted by Nilesh Kadu:
Hi,
Pls let me know the o/p of following program, but pls be sure to explain the things given out.
Thanks for u r attention.
Nilesh.
class superclass{
protected String String;
superclass(){
nameString();
}
public void nameString(){
String = "Aishwarya";
System.out.println("String: "+String);
}
}
class Q29 extends superclass{
private String String = "Priyanka";
Q29(){
nameString();
}
public void nameString(){
System.out.println("String: "+String);
}
public static void main(String[] args){
new Q29();
}
}


reply
    Bookmark Topic Watch Topic
  • New Topic