Results 1 to 16 of 16
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Basic inheritance and access member of superclass in direct and indirect subclasses.

  1. #1
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Post Basic inheritance and access member of superclass in direct and indirect subclasses.

    Compiler shows two errors when I compile the following program. But I think the code is correct and I can't understand the errors. Please help.

    Java Code:
    class myclass
    {
    	int a;
    
    	myclass(int i)
    	{
    		a=i;
    	}
    }
    
    class myclass1 extends myclass
    {
    	myclass1(int j)
    	{
    		a=j;
    	}
    }
    
    class myclass2 extends myclass1
    {
    	myclass2(int k)
    	{
    		a=k;
    	}
    }
    
    class TestStatic1
    {
    	public static void main(String args[])
    	{
    		myclass obj1=new myclass(3);
    		myclass obj2=new myclass(5);
    
    		myclass1 ob1=new myclass1(7);
    		myclass2 ob2=new myclass2(9);
    
    		System.out.println("Value of superclass object1: "+obj1.a);
    		System.out.println("Value of superclass object2: "+obj2.a);
    
    		System.out.println("Value of direct subclass: "+ob1.a);
    		System.out.println("Value of indirect subclass: "+ob2.a);
    	}
    }
    Last edited by Rafiqul; 03-22-2013 at 05:42 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    You didn't declare a any class public. Declare TestStatic1 public and make certain it is a file named TestStatic1.java. Also, you need to either define a default constructor, namely classname() or call the super class constructor that *is* defined from the subclass constructors.

    Also, the convention is that classnames begin with a capital letter. And please put your code between code tags [code][/code].

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Thanks for the reply. Sorry, I didn't know about the code tags, because this was my first post to this forum.

    I did the things as you told. But the same two errors still exist. The error message say something like that, the problem is with the argument types of the constructors. But I don't understand. I also used a member of superclass in the direct and indirect subclasses.

    Can you please compile the program to understand the errors ?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    It would be easier if you just copy and paste the errors in a post.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    I compiled with Textpad 4.73 and I got the following error message:

    Line 14: error: constructor myclass in class myclass can not be applied to given types;
    required: int
    found: no arguments
    reason: actual and formal argument lists differ in length

    Line 22: error: constructor myclass in class myclass can not be applied to given types;
    required: int
    found: no arguments
    reason: actual and formal argument lists differ in length

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    It still sounds like you did not include the empty constructor in your super classes.

    e.g. className() {}

    Regards,
    Jim
    Rafiqul likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Thanks. Now the program works without errors.

    But I know that, there is no need for the programmer to define the default constructor of a class himself, java automatically creates default constructor. This is the first time I have to include the default constructor myself.

    Can you explain, why I need to define the default constructor myself in that program I posted ?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Java only includes the default, zero-argument constructor when you do not supply your own constructor. When you create an instance of your subclass, the constructor first calls the default constructor via an implied super() call. So if one isn't defined, then it can't invoke it. Also, another alternative to what I told you earlier was to simply call a specific super constructor as the first statement in your subclass constructors. Then you don't need the default.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Quote Originally Posted by jim829 View Post
    It still sounds like you did not include the empty constructor in your super classes.

    e.g. className() {}

    Regards,
    Jim
    I would argue that it's not the lack of an empty constructor that's the problem.
    The subclasses should call their parent constructor.
    Last edited by Tolls; 03-25-2013 at 11:18 AM. Reason: missed "parent"
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    You are right from a programming technique perspective. But if you notice in his code, he is trying to set the super class variables from the sub classes. So I assumed that he did not want to invoke the only defined super class constructor., Otherwise, his assignments would be unncecessary. However, I did not really make this clear in my response.

    Regards,
    Jim
    Rafiqul likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Quote Originally Posted by jim829 View Post
    You are right from a programming technique perspective. But if you notice in his code, he is trying to set the super class variables from the sub classes. So I assumed that he did not want to invoke the only defined super class constructor., Otherwise, his assignments would be unncecessary. However, I did not really make this clear in my response.

    Regards,
    Jim
    Except they shouldn't be doing that either as 'a' should be private.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Often, especially in such skeletal code, I just assume they are experimenting to see if it is possible.

    Regards,
    Jim
    Rafiqul likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Sorry, I can't understand your statement, "Except they shouldn't be doing that either as 'a' should be private."

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    The variable 'a' belongs to the myclass class.
    It should be private.
    The only way to set that value should be via myclass, probably in the constructor. You might also supply a setter, though it's not a good idea to automatically add a setter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    I'm sorry to vex you again, but I couldn't understand what do you mean by "setter" ?

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Basic inheritance and access member of superclass in direct and indirect subclass

    Quote Originally Posted by Rafiqul View Post
    I'm sorry to vex you again, but I couldn't understand what do you mean by "setter" ?
    Did you try searching? Google gave me some extremely relevant explanations in the first page of results for setter Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Indirect object access in JAVA
    By cknew1 in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 05:36 AM
  2. Inheritance and Subclasses
    By whateverme in forum New To Java
    Replies: 6
    Last Post: 02-10-2011, 01:47 PM
  3. subclasses and inheritance - how's it all work?
    By 711groove in forum New To Java
    Replies: 14
    Last Post: 12-18-2009, 09:04 AM
  4. Can I use annotations in superclass, and .cfg.xml files in subclasses in EJB3
    By narayanrec in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 01-06-2008, 02:40 AM
  5. Replies: 0
    Last Post: 01-02-2008, 04:24 AM

Tags for this Thread

Posting Permissions

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