• 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().this()!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi folks,
is the following way of using this() is legal? why or why not?


i know we can call constructor in A like this super("test"); but in class B, i have used this() with super() to call a constructor in A. tell me why it's invalid?
thanks.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple things:
1). Constructors do not have return values; therefore, you cannot chain them together.
2). Secondly, since A has a defined constructor, a default, no-argument constructor will not be provided; you will get a "cannot resolve symbol" error when you call super()
3). You do not have a constructor that takes a String as a parameter in your B class. The call to this(String) will not resolve ("cannot resolve symbol" error message.
Remember, Constructors are not Methods; they do not return anything, they are not inherited, and they do not participate in polymorphism.
I'm not sure what result you were trying to acheive with that syntax, but to get the result I think you want, try:

[ July 15, 2003: Message edited by: Joel McNary ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
Couple things:
I'm not sure what result you were trying to acheive with that syntax
[ July 15, 2003: Message edited by: Joel McNary ]



class B extends A{
B(){
super().this("test");
}
}


what i am trying to do here is calling a constructor in superclass A. my question is can i use this()with super() the way i am using?
thanks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you just trying to say

That's the right syntax to pass constructor arguments to a parent class.
[ July 15, 2003: Message edited by: Ernest Friedman-Hill ]
[ July 15, 2003: Message edited by: Ernest Friedman-Hill ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No that's not what i am trying to say. what i am asking is that is super().this("test"); valid?

][/qb]<hr></blockquote>
[ July 15, 2003: Message edited by: Namaste Sathi ]
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is no, that syntax is not valid. However, you can call the constructor in class A with the syntax provided by myself and Ernest.
[ July 15, 2003: Message edited by: Joel McNary ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not even close. What's the point anyway? If you're trying to call a constructor in your superclass just use super("astring") or this.super("astring").
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys for your help. i appreciate your help Joel and Ken with super().this("somevalue");
thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic