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: -
/**
-
* Prints an Artwork piece to the console
-
*/
-
public void print(){
-
System.out.println(stockID + "," + title + "," +
-
artist + "," + price +
-
"," + height + "," + width + "," + sold + ",");
-
}
-
-
/**
-
* Writes the Artwork object to a file [man].
-
* <br> Object attributes are de-limited with the <b>#</b>
-
* character and output on a single line.
-
* @param pw The PrintWriter attached to the file.
-
*/
-
public void outputToWriter(PrintWriter pw){
-
pw.print(stockID + "#");
-
pw.print(title + "#");
-
pw.print(artist + "#");
-
pw.print(price + "#");
-
pw.print(height + "#");
-
pw.print(width + "#");
-
pw.println(sold + "#");
-
}
-
/**
-
* Creates a new Artwork object by reading the attributes from a file [man].
-
* @param br The BufferedReader attached to the file.
-
*/
-
-
public static Artwork constructFromReader(BufferedReader br){
-
String line;
-
try{
-
line = br.readLine();
-
String [] attribs = line.split("#");
-
int sid = Integer.parseInt(attribs[0]);
-
String t = attribs[1];
-
String a = attribs[2];
-
double p = Double.parseDouble(attribs[3]);
-
double h = Double.parseDouble(attribs[4]);
-
double w = Double.parseDouble(attribs[5]);
-
boolean sld = attribs[6].equalsIgnoreCase("true");
-
return new Artwork(sid,t,a,p,h,w,sld);
-
}
-
catch (Exception e){
-
return null;
-
}
-
}
-
So how do I inherit these methods and how do I add an 8th parameter to the inherited methods within the Print subclass?
1 1941
The keyword is super.
Here is an example - public class Inherit {
-
public static void main(String[] args) {
-
new Person().printMe("Human from Person");
-
new Person1().printMe("Human from Person1", 2);
-
}
-
}
-
class Person {
-
public void printMe (String name) {
-
System.out.println(name);
-
}
-
}
-
class Person1 extends Person {
-
public void printMe(String name, int age) {
-
super.printMe(name);
-
System.out.println(age);
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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,
|
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 (...
|
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
|
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):
| |
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...
|
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:
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |