• Post Reply Bookmark Topic Watch Topic
  • New Topic

syntax help with inheritance.  RSS feed

 
Oliver Tatlow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I'm new to Java but am already familiar with object oriented programming and am just trying to figure out the syntax in Java.

Have written a class as a test and am getting errors I don't understand.


File 1 - ClassTest.java
File 2 - ParentClass.java
File 3 - DerivedClass.java
In all the examples I found the classes are always written together but after trying that there's error "class DerivedClass is public, should be declared in a file named DerivedClass.java"

after separating them into different files, now get this error and have got no idea why.

constructor ParentClass in class classtest.ParentClass cannot be applied to given types; required: int found :no arguments reason : actual and formal argument lists differ in length

Please can anyone tell me where I've gone wrong?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Oliver,

Welcome at the Ranch.
In order to make your code more readable for other users, you should put it in between the code tags.

However, I think the problem in your code is that you define a custom constructor for the parent class, which means that the default constructor does not exist anymore for that class unless you explicitly define it. And you subclass needs to default constructor of the parent class, as super() is implicitly called if not stated otherwise in the code.

I hope that helps!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver Tatlow wrote:Hi there. I'm new to Java...

Hi Oliver, and welcome to JavaRanch.

First, when you're posting code, please UseCodeTags (←click). I've done it for you this time, and see how much better it is to look at?

Oh, and please read the page carefully; there are line size limits for code that you need to be aware of; otherwise you'll end up with equally unreadable postings.

Thanks.

Winston
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all.

The class goes before the variable name, so.

DerivedClass MyObject; it's the way it works.

Then you have a constructor problem.

Constructors are not inherited.

If you don't specify a constructor, the compiler makes one by itself like this.



In your case it applies to DerivedClass, as you created a constructor on ParentClass.

But here is the problem,

there is no "non-argument" constructor for ParentClass. Because you already put a constructor and the compiler don't insert one by default.
 
Oliver Tatlow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the help.

I didn't know about the super call available from constructors in derived classes. This gets rid of the error and solves everything for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!