473,785 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting methods and from a super class

1 New Member
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 1941
r035198x
13,262 MVP
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
1787
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 implementation of super. For example, 'len(super(Y, y)' will always result in 'len() of unsized object'. As far as I understand, this is because builtins don't use a dynamic lookup chain, but go directly to the slots for the builtins. However, super...
0
1850
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 particular, should I be concerned that in the example below, f is different from g and h when applying f to instances of test.Sub? For my simple example things appear reasonable, but ... Thanks,
99
5924
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
8
1549
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 implementing only 2 methods, __init__ and setConfig. ------------------------------------------------------- #!/usr/bin/python class A (object): def __init__(self): super(A, self).__init__() self.x = 0
16
2088
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
2168
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 methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
1
336
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 down and look at it, I'm wondering why it behaves the way it does, and also how to improve its functionality. First, understand the basic format of a type-safe collection:
3
1527
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 several MustOverride subs in there, and also a public property. I'm going to inherit a bunch of usercontrols from this one superclass (terminology correct?) and then they all have to make sure they can respond to that set of functions that I've...
20
1735
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 learning JavaScript and then BAM, I understood them. Just a little while ago, I had a fear of decorators because I really couldn't find a definitive source to learn them (how to with with @). How important are they? They must be important...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9954
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.