Reputation: 1151
I need help figuring this out, what am I doing wrong my book doesn't give a clear explanation on how to deal with this and neither does the online help in VS2008.
Can you show me how to do this correctly?
Another error I am getting:
Cannot Create an instance of the abstract class or interface "Person.Person"
Member Person.Person.rnd' cannot be accessed with an instance reference; qualify it with a type name instead
Person.cs
public abstract class Person
{
private string title;
private string firstName;
private string lastName;
private string address;
private string gender;
private string dateOfBirth;
private string userID;
static Random rnd = new Random();
// constructors
public Person()
{
}//end default constructor
public Person(string aTitle, string aFirstName, string aLastName,
string aAddress, string aGender, string aDateOfBirth)
{
title = aTitle;
firstName = aFirstName;
lastName = aLastName;
address = aAddress;
gender = aGender;
dateOfBirth = aDateOfBirth;
}
}
Student.cs
public class Student: Person
{
public override string GenerateUserID()
{
this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5);
//ERROR HAPPENS HERE
this.userID = this.userID + this.rnd.Next(1000, 9999);
}//end method Generate UserID
}
PersonTest.cs
static void Main(string[] args)
{
//ERROR HAPPENS HERE TOO Cannot Create an instance of the abstract class or interface "Person.Person"
Person testPerson = new Person("Mr.", "Merry ", "Lanes",
" 493 Bluebane RD", "Male", " 8-06-1953 ");
}
Upvotes: 1
Views: 2158
Reputation: 19201
You can't create a Person object since it's abstract.
If you are not a native english speaker or if you just don't know what abstract means, grab a dictionary and see what this word means.
In the OOP world it means that you can only instancate classes that derive that class.
For further reference google polymorphism.
This means that your code should be:
static void Main(string[] args)
{
Person testPerson = new Student("Mr.", "Merry ", "Lanes", " 493 Bluebane RD", "Male", " 8-06-1953 ");
}
You should also change your Student constructor to accept those parameters and move them up to the base class like this:
public Student(string aTitle, string aFirstName, string aLastName, string aAddress,
string aGender, string aDateOfBirth)
: base(aTitle, aFirstName, aLastName, aAddress,
aGender, aDateOfBirth)
{
}
And as others have mentioned, if you want to access a member variable of the base class from a derived class use protected.
protected
means this is public for my derived classes, private for anyone else.
Also, your rnd variable is static and private.
This means you can't access it from this
because it belongs to all Person objects and not to specific instances. You can remove this or if you think you need to, remove the static. Make it protected as well.
Good luck.
Upvotes: 3
Reputation: 3983
In Student.cs, you're using userID
and other members that are private
in the base class, make it protected
and it'll be running. More information, Access Modifiers (C# Programming Guide).
The next exception is thrown because you're instantiating an object of Person
which is an abstract class. Abstract classes are just meant to be inherited, therefore if you wish to create an object of that class, remove the abstract
modifier.
Upvotes: 1
Reputation: 26227
private
means that only the class itself can access it, i.e. no sub-classes. Therefore your Student
class can't access userID
.
Upvotes: 0
Reputation: 1706
In your Student class, you're trying to access userID, which is a private member of the Person class. If you want to be able to access base class members from a derived class, you need to mark them protected.
Upvotes: 0
Reputation: 62057
firstName
and userID
are private in the base class, so the subclass cannot access them. Change them to protected
.
Upvotes: 0
Reputation: 6440
private
fields and properties cannot be accessed by subclasses. If you use protected
, you should be fine.
Upvotes: 4