Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Java - using super to call a method in the parent class
If you're writing a Java class in which you override a method from the base class, can you still make use of that base class method in your new code? Yes, you can. You want the super call.

Here's an example - I had a base class called Shape which overrides the toString method. And I was then writing a Rectangle class in which I wanted a further enhanced toString method. So here it is - firstly running toString from Shape and then adding some extra text.

public String toString() {
String firstbit = super.toString();
String result = firstbit + " Rectangle " + width + " by " + height;
return result;
}


The toString method in Java is defined for all Objects as the method which converts the object into a printable string - so that if you ask for an object to be printed this is how it will be formatted. It's a method that all classes that you write will inherit - either from Object or from other classes if you define inheritance yourself, and it is very often overridden at many levels.
(written 2008-01-10 14:23:22)

 
Associated topics are indexed under
J710 - Java - Extending Classes and More

Back to
First Class Java. First step and moving forward.
Previous and next
or
Horse's mouth home
Forward to
Java, sorting, ArrayList example, generics

Some other Articles
Script to present commonly used images - PHP
Flooding by Asda-s proposed new supermarket
Web page (http) error status 405
Java, sorting, ArrayList example, generics
Java - using super to call a method in the parent class
First Class Java. First step and moving forward.
Climate change, renewable resources and paper v plastic
Java is a dynamic language .... (and comparison)
Training Season Starts again!
PHP / Web 2 logging
1693 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho