•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,726 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 674 | Replies: 7
![]() |
•
•
Join Date: Aug 2007
Location: Adelaide, South Australia
Posts: 428
Reputation:
Rep Power: 3
Solved Threads: 53
Hi ceyesuma,
The super method is used to call a parent class's constructor method in the child class's constructor method. As an example...
In this example, Circle is the child class of Shape and it's constructor calls super as its first instruction and then extends it by doing something after.
Hope this helps,
darkagn
The super method is used to call a parent class's constructor method in the child class's constructor method. As an example...
java Syntax (Toggle Plain Text)
public class Shape { int height; int width; public Shape() { height = 1; width = 2; } } public class Triangle extends Shape { double area; public Triangle() { super(); // this means height = 1 and width = 2 area = height * width / 2.0; } }
In this example, Circle is the child class of Shape and it's constructor calls super as its first instruction and then extends it by doing something after.
Hope this helps,
darkagn
Last edited by darkagn : Sep 27th, 2007 at 7:34 am.
•
•
Join Date: Aug 2007
Posts: 184
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Hi ceyesuma,
The super method is used to call a parent class's constructor method in the child class's constructor method. As an example...
java Syntax (Toggle Plain Text)
public class Shape { int height; int width; public Shape() { height = 1; width = 2; } } public class Triangle extends Shape { double area; public Triangle() { super(); // this means height = 1 and width = 2 area = height * width / 2.0; } }
In this example, Circle is the child class of Shape and it's constructor calls super as its first instruction and then extends it by doing something after.
Hope this helps,
darkagn
Thanks darkagn It's a start.
Super can also be used to call a parent class method that you overriding to extend the original functionality. i.e.
java Syntax (Toggle Plain Text)
public void someMethod(){ // go ahead and do what parent class has defined super.someMethod(); // now do a few other things that this subclass needs... }
So any child class of Super is going to begin with a height of 1 and width of 2.
You could then add things such as area, volume and other attributes from that base.
Could you also change one of the original attributes? Say if I wanted to call Super, but make the newest object have a height of 2.
You could then add things such as area, volume and other attributes from that base.
Could you also change one of the original attributes? Say if I wanted to call Super, but make the newest object have a height of 2.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
So any child class of Super is going to begin with a height of 1 and width of 2.
You could then add things such as area, volume and other attributes from that base.
Could you also change one of the original attributes? Say if I wanted to call Super, but make the newest object have a height of 2.
Yes, you can change the base class variables as long as their scope allows. With no scope modifier, the variable is package-protected, which means other classes in the same package can access it. Protected access will allow classes in the same package and also any subclasses to access it. Public allows anything to access it. If you declare it private though, not even the subclasses can access the variable.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Computer is super slow (Troubleshooting Dead Machines)
- can't get super web cam started (Windows NT / 2000 / XP / 2003)
- What super power would you most likely want? (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: Want help for Dijkstra algorithm
- Next Thread: Executing a Class using path provided in runtime?



Linear Mode