• 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

Constructors

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did line6 "Star" printed out? I understand output1--5 and7.
D.
1.Star
2.Star Wars1
3.Mercury is a Star.
4.Venus and Saturn are also Stars.
5.Star Wars2
6.Star
7.Earth
public class Star {
Star() {
System.out.println("Star");
}
Star(String s1) {
super();
System.out.println(s1 + " is a Star");
}
Star(String s2, String s3) {
this("Mercury");
System.out.println( s2 + " and " + s3 + " are also Stars");
}
public static void main(String [] args) {
Sun sun = new Sun();
Sun sun1 = new Sun("Venus");
Sun sun2 = new Sun("Mars", "Earth");
}
}
class Sun extends Star {
public Sun() {
System.out.println("Star Wars1");
}
public Sun(String v1) {
super(v1, "Saturn");
System.out.println("Star Wars2");
}
public Sun(String v2, String v3) {
if ( v2.substring(0, v2.length()).length() > v3.length())
System.out.println("Mission to Mars");
else
System.out.println("Earth");
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you created sun1 you made an explicit call to a particular constructor of the super class, so that was used. When you created sun and sun2 you did not call any of the constructors of the superclass, so the no-argument constructor was called for you. Some constructor of every superclass MUST be called before the constructor of the sub-class. Both line 1 and line 6 are caused by that.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you are creating object of Sun class by :
Sun sun2 = new Sun("Mars", "Earth");
In the constructor given in Sun class there is no explicit call for any constructor hence compiler calls default constructor of the super class .
public Sun(String v2, String v3) {
// No super() or this()
if ( v2.substring(0, v2.length()).length() > v3.length())
System.out.println("Mission to Mars");
else
System.out.println("Earth");
}
 
jing zhao
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha Ha. I really understand it this time.
Thanks Cindy.

Originally posted by Cindy Glass:
When you created sun1 you made an explicit call to a particular constructor of the super class, so that was used. When you created sun and sun2 you did not call any of the constructors of the superclass, so the no-argument constructor was called for you. Some constructor of every superclass MUST be called before the constructor of the sub-class. Both line 1 and line 6 are caused by that.


 
reply
    Bookmark Topic Watch Topic
  • New Topic