• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

inheritance problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I tested the following code, but it did not want to compile when i want to use it with aconstructor.
Can someone please tell me why and explain.
Next question how do you inherit a class of your own?
Thanks for any answers.
class Mother extends Father{
}
public class Father
{int num;
//public Father(int i)//without constructor it compiles
//{ //please explain why
//num = i;
//}

public static void main(String args[])
{
}
}
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a constructor that takes one or more arguments then you also need the default constructor that takes no arguments.
/Rene
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lucious Mucas:
Unless you are creating a nested class then you need to take the class out of your constructor. Your constructor should look something loke this:
//declaring class
public class Example
{
//declare global variables
....
//intialize global variables with constuctor
public Example
{
globalVar = 0;
}//end constructor
}//end class

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cornel,
Rene's right - a no-args constructor in Father would allow your code to compile.
Here's where the problem lies. Every time you write a class, if you don't write a constructor, Java inserts a default constructor (no args). However, if you explicitly write constructors, what you write is what you get.
Also in each constructor, there's a call to the constructor in the class above. Again, either you can write this or Java will automatically insert it for you. If you put the call in, it has to be the first thing in your constructor. If you don't put in this call, Java will insert a call to the no-args constructor of the parent class - if there isn't a no-args constructor, you're in trouble.
So the problem with your code is that Mother extends Father and the default constructor in Mother attempts to call the no args constructor in Father - and there isn't one. So you've got two possible solutions. You can either add a constructor to Mother, which explicitly calls the available constructor in Father:-

Or you can add another, no args, constructor to Father:-

Hope this helps,
Kathy
[ July 18, 2002: Message edited by: Kathy Rogers ]
 
rubbery bacon. crispy tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic