Results 1 to 6 of 6
Thread: Sub class and super class
- 09-28-2011, 03:21 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Sub class and super class
I am trying to learn more about sub classes and super classes. I understand the sub class extends the superclass and is thus derived from the super class.
So is it true to say that a sub class is then a subset of a super class?
I also understand that if a subclass does not define a constructor it invokes the constructor of the super class but is that always the case? Or is that only when there is not a sub class constructor defined?
-
Re: Sub class and super class
No. A sub class does not have the super class's private fields and methods, but it has all the public and protected fields and methods and often a few of its own fields and methods.
A sub class always invokes the super class constructor, always, either explicitly or implicitly.I also understand that if a subclass does not define a constructor it invokes the constructor of the super class
- 09-28-2011, 03:38 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Re: Sub class and super class
Thank you. Just to make sure I am understanding correct. To invoke the constructor explicitly would be to call super() and to invoke it implicitly would be simply to just do nothing?
-
Re: Sub class and super class
Correct. When you call it explicitly, you can choose which constructor to call, the default one with no parameters or one with one or many parameters. If you do nothing, the default constructor is always called. This can cause a compiler error if no default constructor is available in the super class.
- 09-28-2011, 03:50 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Re: Sub class and super class
Ok that makes sense. What are the requirements for defining a constructor within the subclass? Playing around with it in the IDE I can only get classes to compile if either there is no constructor defined in the subclass or if both the superclass constructor and subclass constructors have no arguments.
- 09-28-2011, 09:50 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Sub class and super class
Java Code:public class MyParent { public MyParent(int someParameter) { } } .... public class MySub extends MyParent { public MySub(int someParameter) { super(someParameter); // Works as there is a constructor that takes an int argument in the parent. } public MySub() { super(1); // Works as there is a constructor that takes an int argument in the parent. } public MySub() { super(); // Doesn't work as there is no constructor that takes no parameters. } public MySub() { // Doesn't work for the same reason. } }
Similar Threads
-
Gathering Data from a super class from a sub class
By kammce in forum New To JavaReplies: 2Last Post: 08-17-2011, 08:09 AM -
Referring to a super class
By blug in forum New To JavaReplies: 7Last Post: 03-20-2011, 05:06 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
super instanceof Class?
By mikeiz404 in forum New To JavaReplies: 11Last Post: 01-23-2009, 07:23 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks