User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2007
Posts: 184
Reputation: ceyesuma is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Junior Poster

Super!

  #1  
Sep 26th, 2007
Can someone show me a simple program that will display how to extend and use super?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Adelaide, South Australia
Posts: 428
Reputation: darkagn will become famous soon enough darkagn will become famous soon enough 
Rep Power: 3
Solved Threads: 53
darkagn's Avatar
darkagn darkagn is offline Offline
Posting Pro in Training

Re: Super!

  #2  
Sep 27th, 2007
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...
  1. public class Shape {
  2. int height;
  3. int width;
  4. public Shape() {
  5. height = 1;
  6. width = 2;
  7. }
  8. }
  9.  
  10. public class Triangle extends Shape {
  11. double area;
  12. public Triangle() {
  13. super(); // this means height = 1 and width = 2
  14. area = height * width / 2.0;
  15. }
  16. }

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.
Reply With Quote  
Join Date: Aug 2007
Posts: 184
Reputation: ceyesuma is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Junior Poster

Re: Super!

  #3  
Sep 27th, 2007
Originally Posted by darkagn View Post
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...
  1. public class Shape {
  2. int height;
  3. int width;
  4. public Shape() {
  5. height = 1;
  6. width = 2;
  7. }
  8. }
  9.  
  10. public class Triangle extends Shape {
  11. double area;
  12. public Triangle() {
  13. super(); // this means height = 1 and width = 2
  14. area = height * width / 2.0;
  15. }
  16. }

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.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Super!

  #4  
Sep 27th, 2007
Super can also be used to call a parent class method that you overriding to extend the original functionality. i.e.
  1. public void someMethod(){
  2. // go ahead and do what parent class has defined
  3. super.someMethod();
  4.  
  5. // now do a few other things that this subclass needs...
  6. }
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Super!

  #5  
Sep 27th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Super!

  #6  
Sep 27th, 2007
Originally Posted by no1zson View Post
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.
Reply With Quote  
Join Date: Aug 2007
Posts: 184
Reputation: ceyesuma is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Junior Poster

Re: Super!

  #7  
Sep 27th, 2007
great! thanks. I will try to use this and see if I undestand.
Reply With Quote  
Join Date: Jul 2007
Posts: 186
Reputation: no1zson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Junior Poster

Re: Super!

  #8  
Sep 27th, 2007
Thanks Ezzaral, that is exactly what I needed to know.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC