473,412 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Inheriting methods and from a super class

Hi excuse the mistake in the title.
I'm having trouble on inheriting methods from a superclass in Java. Basically I've forgotten how to.
I want to inherit 3 methods from the Artwork superclass to be used in the Print subclass. The reason being I want to add an extra parameter (printNumber) to these methods in the Print Class.
This section of code from the Artwork class is what I want to be inherited:
Expand|Select|Wrap|Line Numbers
  1.     /**
  2.      * Prints an Artwork piece to the console
  3.      */
  4.     public void print(){
  5.         System.out.println(stockID + "," + title + "," +
  6.                 artist + "," + price +
  7.                 "," + height + "," + width + "," + sold + ",");
  8.     }
  9.  
  10.     /**
  11.      * Writes the Artwork object to a file [man].
  12.      * <br> Object attributes are de-limited with the <b>#</b> 
  13.      * character and output on a single line.
  14.      * @param pw The PrintWriter attached to the file.
  15.      */
  16.     public void outputToWriter(PrintWriter pw){
  17.         pw.print(stockID + "#");
  18.         pw.print(title + "#");
  19.         pw.print(artist + "#");
  20.         pw.print(price + "#");
  21.         pw.print(height + "#");
  22.         pw.print(width + "#");
  23.         pw.println(sold + "#");
  24.     }
  25.     /**
  26.      * Creates a new Artwork object by reading the attributes from a file [man].
  27.      * @param br The BufferedReader attached to the file.
  28.      */
  29.  
  30.     public static Artwork constructFromReader(BufferedReader br){
  31.         String line;
  32.         try{
  33.             line = br.readLine();
  34.             String [] attribs = line.split("#");
  35.             int sid = Integer.parseInt(attribs[0]);
  36.             String t = attribs[1];
  37.             String a = attribs[2];
  38.             double p = Double.parseDouble(attribs[3]);
  39.             double h = Double.parseDouble(attribs[4]);
  40.             double w = Double.parseDouble(attribs[5]);    
  41.             boolean sld = attribs[6].equalsIgnoreCase("true");
  42.             return new Artwork(sid,t,a,p,h,w,sld);
  43.         }
  44.         catch (Exception e){
  45.             return null;
  46.         }
  47.     }
  48.  
So how do I inherit these methods and how do I add an 8th parameter to the inherited methods within the Print subclass?
Nov 2 '06 #1
1 1920
r035198x
13,262 8TB
The keyword is super.
Here is an example
Expand|Select|Wrap|Line Numbers
  1. public class Inherit {
  2.     public static void main(String[] args) {
  3.         new Person().printMe("Human from Person");
  4.         new Person1().printMe("Human from Person1", 2);
  5.     }
  6. }
  7. class Person {
  8.     public void printMe (String name) {
  9.         System.out.println(name);
  10.     }
  11. }
  12. class Person1 extends Person {
  13.     public void printMe(String name, int age) {
  14.         super.printMe(name);
  15.         System.out.println(age);
  16.     }
  17. }
Nov 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Matthias Oberlaender | last post by:
I would like to adopt the cooperation paradigm in conjunction with builtin methods and operators, such as len, iter, +, * etc. But the direct approach does not work with the current...
0
by: daishi | last post by:
Hi, The following code appears to be doing what I'd expect, but I'm wondering if someone could confirm that there aren't any "gotchas" hidden in using methods accessed in this way. In...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
8
by: Dan Perl | last post by:
Here is a problem I am having trouble with and I hope someone in this group will suggest a solution. First, some code that works. 3 classes that are derived from each other (A->B->C), each one...
16
by: Fuzzyman | last post by:
Hello, To create a classic (old style) class, I write : class foo: pass To do the equivalent as a new style class, I write : class foo(object):
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
3
by: YYZ | last post by:
I swear I've done my research, and now I was just hoping someone could explain this to me. I've got a base class (usercontrol) that I am using just as an interface. Meaning, I've defined...
20
by: vbgunz | last post by:
I remember learning closures in Python and thought it was the dumbest idea ever. Why use a closure when Python is fully object oriented? I didn't grasp the power/reason for them until I started...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.