• 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

Super Constructor

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
I have a difficulty here in this particular quesiton
class Vehicle {
String str ;
public Vehicle() {
}

public Vehicle ( String s ) {
str = s;
}
}
public class Car extends Vehicle {
public static void main (String args[] ) {
final Vehicle v = new Vehicle ( " Hello" );
v = new Vehicle ( " How are you");
v.str = "How is going";
System.out.println( "Greeting is : " + v.str );
}
}
A) Compiler error while subclassing the Vehicle
B) Compiler error , you cannot assign a value to final variable
B) Prints Hello
C) Prints How is going
This class is executing easily, but i think that the subclass will see for a default constructor, and if it does not find it, it will throw an error.
This particular program is producing error, but b'coz of fina variable not for the reason which i want.
Please help me,
Thanx in advance.
Nisheeth
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case a default constructor for class Car is provided for you automatically, so you will not get a compile time or runtime error! Only if you declared a constructor with an argument would you need to explicitly declared a default constructor (if it's needed).
Study the following code, both class Automobile and class Truck are missing a default constructor, but the code builds and runs fine!

public class Test {
static public void main(String[] arg) {

// Truck s = new Truck(); // this line would not compile!

// This is fine, we have a constructor to build this object!!
Truck t = new Truck("Dodge Ram");
System.out.println(t.getName());
}
}
class Automobile {
String name;
Automobile(String s) {
name = s;
}

String getName() {
return name;
}
}
class Truck extends Automobile {
Truck(String s) {
super(s);
}
}
[ January 22, 2002: Message edited by: Rajinder Yadav ]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Kaushal
sorry.. but I am not getting your prob
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you are right that the statement
final Vehcile v = new Vehcile.......
is not correct. You make a class final where you declare it.
second you asked about the default constructor for Vehcile. well it is there, though it is empty but that is ok.
public Vehcile() {}
but this default constructor is not being invoked. only the constructor which takes string parameters is invoked. (2 times)
i hope now question is clear.

Originally posted by Nisheeth Kaushal:
Hi Friends
I have a difficulty here in this particular quesiton
class Vehicle {
String str ;
public Vehicle() {
}

public Vehicle ( String s ) {
str = s;
}
}
public class Car extends Vehicle {
public static void main (String args[] ) {
final Vehicle v = new Vehicle ( " Hello" );
v = new Vehicle ( " How are you");
v.str = "How is going";
System.out.println( "Greeting is : " + v.str );
}
}
A) Compiler error while subclassing the Vehicle
B) Compiler error , you cannot assign a value to final variable
B) Prints Hello
C) Prints How is going
This class is executing easily, but i think that the subclass will see for a default constructor, and if it does not find it, it will throw an error.
This particular program is producing error, but b'coz of fina variable not for the reason which i want.
Please help me,
Thanx in advance.
Nisheeth

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,

second you asked about the default constructor for Vehcile. well it is there, though it is empty but that is ok.
public Vehcile() {}
but this default constructor is not being invoked. only the constructor which takes string parameters is invoked. (2 times)


Mark I thought that the no args default constructor Vehicle() would only "be there" if there were no other constructor specified. Here there is a constructor for Vehicle which takes a string arg. Also how does this get invoked twice?
TIA
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
yes you are right that the statement
final Vehcile v = new Vehcile.......
is not correct. You make a class final where you declare it.


Actually Mark, this line is fine. You can declare a variable to be final. This allows the variable to be initialized once, then prevented from having its value changed. The error in this code is in the line
v = new Vehicle ( " How are you");
when the final variable v, which has already been initialized, is re-assigned a new value.


second you asked about the default constructor for Vehcile. well it is there, though it is empty but that is ok.
public Vehcile() {}
but this default constructor is not being invoked. only the constructor which takes string parameters is invoked. (2 times)
i hope now question is clear.



In the original example, the Vehicle class has two explicit constructors - one is the no-args constructor, and one is a consructor that takes a String argument. If you comment out the second assignement to v, this program works fine and a Vehicle object is instantiated.
The Car class has no explicit constructors, so the compiler is adding one for you - a no-arg constructor:
Car() { super();}
This means that the only way to instantiate a Car object is with a no-arg constructor. You can write
Car myCar = new Car();
but not
Car myCar = new Car("Toyota");
because the Car class has no constructor that takes a string argument.

Rob
[ January 22, 2002: Message edited by: Rob Ross ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Is it suffice to say that if class Vehicle only had Vehicle(String s) type of constructor, that you would not be able to instantiate the Car class utilizing the default: Car myCar = new Car()
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanx to all,
I wud like to ask one more question to Mark that as he said that Default constructor is not invoked even once.
But if u use something like this.
public Vehicle() {
System.out.println("Default Constructor");
}
This will print the above statement, so i think that the instantiation of default constructor is based on the object that u create in the subclass.
So, as we are not creating any vehicle no arg object, a super will be embed and call the default constructor of the superclass.
Tx
Nisheeth
 
reply
    Bookmark Topic Watch Topic
  • New Topic