C# overriding a method with different parameters
up vote
1
down vote
favorite
I am working on an assignment that involves inheritance and overriding methods. I am supposed to create a Ship
class and a CruiseShip
subclass. The ship class is supposed to have two member variables, a string for the ship's name and a string for the ships build date. The class also needs a Print
method to display that information.
The CruiseShip
subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers. The subclass is supposed to override the Ship
class's Print
method and display only the ship name and capacity (not the date).
The issue with my code below is that I am not sure how to override the Print
method to take a string and an integer, instead of two strings. How can I have the override method display the appropriate date?
public class Ship
{
public virtual void Print(string name, string date)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date);
}
}
public class CruiseShip : Ship
{
public override void Print(string name, int capacity)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's passanger capacity: {0}", capacity);
}
}
public class SeeShips
{
static void Main(string args)
{
Ship newShip = new Ship();
newShip.Print("Generic Ship","1989");
Console.ReadLine();
}
}
c# class inheritance override subclass
add a comment |
up vote
1
down vote
favorite
I am working on an assignment that involves inheritance and overriding methods. I am supposed to create a Ship
class and a CruiseShip
subclass. The ship class is supposed to have two member variables, a string for the ship's name and a string for the ships build date. The class also needs a Print
method to display that information.
The CruiseShip
subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers. The subclass is supposed to override the Ship
class's Print
method and display only the ship name and capacity (not the date).
The issue with my code below is that I am not sure how to override the Print
method to take a string and an integer, instead of two strings. How can I have the override method display the appropriate date?
public class Ship
{
public virtual void Print(string name, string date)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date);
}
}
public class CruiseShip : Ship
{
public override void Print(string name, int capacity)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's passanger capacity: {0}", capacity);
}
}
public class SeeShips
{
static void Main(string args)
{
Ship newShip = new Ship();
newShip.Print("Generic Ship","1989");
Console.ReadLine();
}
}
c# class inheritance override subclass
1
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove theoverride
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?
– Lasse Vågsæther Karlsen
Nov 10 at 21:27
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have aPrint
method that uses these member variables to display data. I would like to have aShip
class that has a member variable for its name and date and aPrint
method that used the name and date variables. I would then like aCruiseShip
subclass that has a capacity member variable and aPrint
method that overrides the one from the parent class that only prints the ship name and capacity.
– ZL4892
Nov 10 at 21:33
1
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am working on an assignment that involves inheritance and overriding methods. I am supposed to create a Ship
class and a CruiseShip
subclass. The ship class is supposed to have two member variables, a string for the ship's name and a string for the ships build date. The class also needs a Print
method to display that information.
The CruiseShip
subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers. The subclass is supposed to override the Ship
class's Print
method and display only the ship name and capacity (not the date).
The issue with my code below is that I am not sure how to override the Print
method to take a string and an integer, instead of two strings. How can I have the override method display the appropriate date?
public class Ship
{
public virtual void Print(string name, string date)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date);
}
}
public class CruiseShip : Ship
{
public override void Print(string name, int capacity)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's passanger capacity: {0}", capacity);
}
}
public class SeeShips
{
static void Main(string args)
{
Ship newShip = new Ship();
newShip.Print("Generic Ship","1989");
Console.ReadLine();
}
}
c# class inheritance override subclass
I am working on an assignment that involves inheritance and overriding methods. I am supposed to create a Ship
class and a CruiseShip
subclass. The ship class is supposed to have two member variables, a string for the ship's name and a string for the ships build date. The class also needs a Print
method to display that information.
The CruiseShip
subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers. The subclass is supposed to override the Ship
class's Print
method and display only the ship name and capacity (not the date).
The issue with my code below is that I am not sure how to override the Print
method to take a string and an integer, instead of two strings. How can I have the override method display the appropriate date?
public class Ship
{
public virtual void Print(string name, string date)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date);
}
}
public class CruiseShip : Ship
{
public override void Print(string name, int capacity)
{
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's passanger capacity: {0}", capacity);
}
}
public class SeeShips
{
static void Main(string args)
{
Ship newShip = new Ship();
newShip.Print("Generic Ship","1989");
Console.ReadLine();
}
}
c# class inheritance override subclass
c# class inheritance override subclass
asked Nov 10 at 21:14
ZL4892
495
495
1
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove theoverride
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?
– Lasse Vågsæther Karlsen
Nov 10 at 21:27
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have aPrint
method that uses these member variables to display data. I would like to have aShip
class that has a member variable for its name and date and aPrint
method that used the name and date variables. I would then like aCruiseShip
subclass that has a capacity member variable and aPrint
method that overrides the one from the parent class that only prints the ship name and capacity.
– ZL4892
Nov 10 at 21:33
1
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34
add a comment |
1
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove theoverride
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?
– Lasse Vågsæther Karlsen
Nov 10 at 21:27
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have aPrint
method that uses these member variables to display data. I would like to have aShip
class that has a member variable for its name and date and aPrint
method that used the name and date variables. I would then like aCruiseShip
subclass that has a capacity member variable and aPrint
method that overrides the one from the parent class that only prints the ship name and capacity.
– ZL4892
Nov 10 at 21:33
1
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34
1
1
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove the
override
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?– Lasse Vågsæther Karlsen
Nov 10 at 21:27
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove the
override
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?– Lasse Vågsæther Karlsen
Nov 10 at 21:27
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have a
Print
method that uses these member variables to display data. I would like to have a Ship
class that has a member variable for its name and date and a Print
method that used the name and date variables. I would then like a CruiseShip
subclass that has a capacity member variable and a Print
method that overrides the one from the parent class that only prints the ship name and capacity.– ZL4892
Nov 10 at 21:33
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have a
Print
method that uses these member variables to display data. I would like to have a Ship
class that has a member variable for its name and date and a Print
method that used the name and date variables. I would then like a CruiseShip
subclass that has a capacity member variable and a Print
method that overrides the one from the parent class that only prints the ship name and capacity.– ZL4892
Nov 10 at 21:33
1
1
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.
Imagine I create one of your ships:
var myShip = new Ship();
Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:
public class Ship
{
//these should be properties or
//readonly variables but thats for later
public string name;
public string date;
}
Ok, now you can create a Ship
and give it some state:
var myShip = new Ship();
myShip.name = "My Ship";
myShip.date = "Yesterday";
Great! Now we do have an interesting ship in our hands; it has a name and a date.
Furthermore, now the virtual method Print
doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:
public class Ship
{
....
public virtual string Print() {
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date); }
You see how that works?
From here, you should be able to figure out how to subclass CruiseShip
and override Print
and make it do what you want.
Subjects you will definitely want to read about:
- Constructors and initializing state
- Why properties instead of public member fields
- Read only member fields and properties and immutable types
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
add a comment |
up vote
0
down vote
The Print methods shall take their data from member variables, so no parameters required
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.
Imagine I create one of your ships:
var myShip = new Ship();
Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:
public class Ship
{
//these should be properties or
//readonly variables but thats for later
public string name;
public string date;
}
Ok, now you can create a Ship
and give it some state:
var myShip = new Ship();
myShip.name = "My Ship";
myShip.date = "Yesterday";
Great! Now we do have an interesting ship in our hands; it has a name and a date.
Furthermore, now the virtual method Print
doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:
public class Ship
{
....
public virtual string Print() {
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date); }
You see how that works?
From here, you should be able to figure out how to subclass CruiseShip
and override Print
and make it do what you want.
Subjects you will definitely want to read about:
- Constructors and initializing state
- Why properties instead of public member fields
- Read only member fields and properties and immutable types
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
add a comment |
up vote
2
down vote
accepted
The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.
Imagine I create one of your ships:
var myShip = new Ship();
Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:
public class Ship
{
//these should be properties or
//readonly variables but thats for later
public string name;
public string date;
}
Ok, now you can create a Ship
and give it some state:
var myShip = new Ship();
myShip.name = "My Ship";
myShip.date = "Yesterday";
Great! Now we do have an interesting ship in our hands; it has a name and a date.
Furthermore, now the virtual method Print
doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:
public class Ship
{
....
public virtual string Print() {
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date); }
You see how that works?
From here, you should be able to figure out how to subclass CruiseShip
and override Print
and make it do what you want.
Subjects you will definitely want to read about:
- Constructors and initializing state
- Why properties instead of public member fields
- Read only member fields and properties and immutable types
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.
Imagine I create one of your ships:
var myShip = new Ship();
Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:
public class Ship
{
//these should be properties or
//readonly variables but thats for later
public string name;
public string date;
}
Ok, now you can create a Ship
and give it some state:
var myShip = new Ship();
myShip.name = "My Ship";
myShip.date = "Yesterday";
Great! Now we do have an interesting ship in our hands; it has a name and a date.
Furthermore, now the virtual method Print
doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:
public class Ship
{
....
public virtual string Print() {
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date); }
You see how that works?
From here, you should be able to figure out how to subclass CruiseShip
and override Print
and make it do what you want.
Subjects you will definitely want to read about:
- Constructors and initializing state
- Why properties instead of public member fields
- Read only member fields and properties and immutable types
The problem you have is that you are creating stateless classes, which is essentially what you are not supposed to do in this assignment.
Imagine I create one of your ships:
var myShip = new Ship();
Ok great, so... what's special about this ship? I'd say nothing... the ship has no info tied to it, they are all the same. You need to find a mechanism that actually stores information inside your ship, AKA state:
public class Ship
{
//these should be properties or
//readonly variables but thats for later
public string name;
public string date;
}
Ok, now you can create a Ship
and give it some state:
var myShip = new Ship();
myShip.name = "My Ship";
myShip.date = "Yesterday";
Great! Now we do have an interesting ship in our hands; it has a name and a date.
Furthermore, now the virtual method Print
doesn't need any information passed into it (arguments) because it can fetch the state from the ship from which the method is invoked:
public class Ship
{
....
public virtual string Print() {
Console.WriteLine("The ship's name: {0}", name);
Console.WriteLine("The ship's build date: {0}", date); }
You see how that works?
From here, you should be able to figure out how to subclass CruiseShip
and override Print
and make it do what you want.
Subjects you will definitely want to read about:
- Constructors and initializing state
- Why properties instead of public member fields
- Read only member fields and properties and immutable types
edited Nov 10 at 21:36
answered Nov 10 at 21:29
InBetween
24.6k33967
24.6k33967
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
add a comment |
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
Very informative. Thank you!
– ZL4892
Nov 10 at 21:36
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
@ZL4892 You are welcome!
– InBetween
Nov 10 at 21:37
add a comment |
up vote
0
down vote
The Print methods shall take their data from member variables, so no parameters required
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
add a comment |
up vote
0
down vote
The Print methods shall take their data from member variables, so no parameters required
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
add a comment |
up vote
0
down vote
up vote
0
down vote
The Print methods shall take their data from member variables, so no parameters required
The Print methods shall take their data from member variables, so no parameters required
answered Nov 10 at 21:17
Klaus Gütter
1,203612
1,203612
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
add a comment |
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
Perhaps I am not clear on exactly what a member variable is then. Is it defined just like a normal variable? And how is it's value passed to the method?
– ZL4892
Nov 10 at 21:18
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
A class consists of data and behaviour (methods). You declare the data (member variables) within the class as e.g. public string ShipName { get; set; }. Methods of the class have access to these.
– Klaus Gütter
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
@ZL4892 Yeah you definitely don't get what a member variable is. It's a variable thats part of the class body. also called field.
– Adrian
Nov 10 at 21:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243474%2fc-sharp-overriding-a-method-with-different-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You cannot "override" a method and change the parameter types. Period. Since you've opted to call the method accepting two strings, the way to get your code to compile is to remove the
override
keyword. It isn't clear what you expected to happen here, since you seem to want to call the base class method either way, given that that is the only method here that accepts two strings. Can you explain what you wanted to happen beyond "it does not do what I want"?– Lasse Vågsæther Karlsen
Nov 10 at 21:27
"The CruiseShip subclass is similar except that instead of a member variable for build date, it is to have an integer value for its max capacity of passengers." That makes 0 sense. I can only asume you utterly missread the assignment. No other way to interpret this. "Print" that sounds like a "ToString()" method. Normal overloading.
– Christopher
Nov 10 at 21:28
Sure, let me try to elaborate. Based on other comments, I am not sure what a member variable is, or how to use it. I am supposed to have a
Print
method that uses these member variables to display data. I would like to have aShip
class that has a member variable for its name and date and aPrint
method that used the name and date variables. I would then like aCruiseShip
subclass that has a capacity member variable and aPrint
method that overrides the one from the parent class that only prints the ship name and capacity.– ZL4892
Nov 10 at 21:33
1
You can have all that, except that you cannot change the types of the parameters of methods you override. Does the method need parameters? If you want to ask an instance to print itself, shouldn't all the data to print come from the object itself?
– Lasse Vågsæther Karlsen
Nov 10 at 21:34