Results 1 to 8 of 8
  1. #1
    Ollie999 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default problem with inheritance syntax

    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
    Java Code:
    package classtest;
    
    public class ClassTest 
    {
        public static void main(String[] args) 
        {
            MyObject DerivedClass;
            MyObject = new DerivedClass(3);
        }
    }
    File 2 - ParentClass.java
    Java Code:
    package classtest;
    
    public class ParentClass 
    {
        private int field1;
        
        ParentClass(int nParam)
        {
            field1=nParam;
        }
        
        int GetField()
        {
            return field1;
        }
        
        void SetField(int field)
        {
            field1=field;
        }
    }
    File 3 - DerivedClass.java
    Java Code:
    package classtest;
    
    public class DerivedClass extends ParentClass 
    {
        int field2;
    }
    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.

    problem with inheritance syntax-error.jpg

    Please can anyone tell me where I've gone wrong?
    Last edited by JosAH; 07-18-2012 at 12:03 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: problem with inheritance syntax

    Class DerivedClass extends class ParentClass; you didn't define a constructor for the DerivedClass so the compiler builds on for you (and it doesn't have any arguments). If a constructor doesn't call another constructor in the same class or a constructor in its superclass explicitly, the compiler builds a call to the no-arg superclass constructor; but your super class (ParentClass) doesn't have a no-arg constructor so your complier complains (about its own code building activity, basically).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Ollie999 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: problem with inheritance syntax

    Have tried adding a constructor to the derived class and still get the same error.

    What you're saying seems to suggest that Java forces you to explicitly declare a constructor for all derived classes. Is this right?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: problem with inheritance syntax

    Quote Originally Posted by Ollie999 View Post
    Have tried adding a constructor to the derived class and still get the same error.

    What you're saying seems to suggest that Java forces you to explicitly declare a constructor for all derived classes. Is this right?
    Not really; if you don't define a constructor for a class, the compiler constructs one as follows:

    Java Code:
    public YourClass() { super(); }
    But this constructor assumes that your superclass has a no-arg constructor. If it hasn't one (either constructed by the compiler or by you) the compiler starts complaining.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: problem with inheritance syntax

    Quote Originally Posted by Ollie999 View Post
    Have tried adding a constructor to the derived class and still get the same error.
    Can you give us that code for adding a constructor to the derived class?


    In your class ClassTest.java you have this code:
    Java Code:
            MyObject DerivedClass;
            MyObject = new DerivedClass(3);
    What MyObject represent in your code? Do you have class for it?
    Last edited by cselic; 07-17-2012 at 12:12 PM.

  6. #6
    Ollie999 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: problem with inheritance syntax

    Quote Originally Posted by JosAH View Post
    Not really; if you don't define a constructor for a class, the compiler constructs one as follows:

    Java Code:
    public YourClass() { super(); }
    But this constructor assumes that your superclass has a no-arg constructor. If it hasn't one (either constructed by the compiler or by you) the compiler starts complaining.

    kind regards,

    Jos
    Calling super from the constructor in the derived class - that's what I was looking for. Think I understand how this works now. Thanks for your help.

  7. #7
    Ollie999 is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: problem with inheritance syntax

    Is there a way to mark this thread as solved or shall I just leave it.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default Re: problem with inheritance syntax

    Quote Originally Posted by Ollie999 View Post
    Is there a way to mark this thread as solved or shall I just leave it.
    I just marked the thread 'solved' for you; due to a 'bug' in the forum software you can't do it yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  2. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 03:55 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •