Following example is provided to demonstrate Accessing Superclass Methods from a Subclass. Before new key word is use to hide the subclass method then base keyword is used to access the superclass method.
There are two questions:
-I wish to know whether it is necessary to hide superclass method every time when accessing superclass methods from a subclass.
-It seems paradox because hiding and accessing happening at the same time. If you hide how can access it.
using System;
public class BankLoan //Note: BankLoan class is created but object is not instantiated
{
private int loanNumber;
private string lastName;
protected double loanAmount;
public int GetLoanNum()
{
return loanNumber;
}
public void SetLoanNum(int num)
{
loanNumber = num;
}
public string GetName()
{
return lastName;
}
public void SetName(string name)
{
lastName = name;
}
public double GetLoanAmount()
{
return loanAmount;
}
public void SetLoanAmount(double amt)
{
loanAmount = amt;
}
}
public class DemoCarLoan2
{
public static void Main()
{
CarLoan aCarLoan = new CarLoan();
CarLoan anotherCarLoan = new CarLoan();
aCarLoan.SetLoanNum(111);
aCarLoan.SetName("Morris");
aCarLoan.SetLoanAmount(12000.00);
aCarLoan.SetYear(1992);
aCarLoan.SetMake("Toyota");
anotherCarLoan.SetLoanNum(88888);
anotherCarLoan.SetName("Jefferson");
anotherCarLoan.SetLoanAmount(18000.00);
anotherCarLoan.SetYear(2002);
anotherCarLoan.SetMake("Chevrolet");
Console.WriteLine("Loan #{0} for {1} is for {2}",
aCarLoan.GetLoanNum(), aCarLoan.GetName(), aCarLoan.GetLoanAmount().ToString("C2"));
Console.WriteLine("Loan #{0} is for a {1} {2}",
aCarLoan.GetLoanNum(), aCarLoan.GetYear(), aCarLoan.GetMake());
Console.WriteLine("Loan #{0} is for a {1} {2}",
anotherCarLoan.GetLoanNum(), anotherCarLoan.GetName(), anotherCarLoan.GetLoanAmount().ToString("C2"));
Console.WriteLine("Loan #{0} is for a {1} {2}",
anotherCarLoan.GetLoanNum(), anotherCarLoan.GetYear(), anotherCarLoan.GetMake());
}
}
class CarLoan : BankLoan
{
private int carYear;
private string carMake;
public int GetYear()
{
return carYear;
}
public void SetYear(int year)
{
carYear = year;
if (carYear < 1997)
loanAmount = 0;
}
public string GetMake()
{
return carMake;
}
public void SetMake(string make)
{
carMake = make;
}
new public void SetLoanNum(int num)
{
if (num <= 999)
base.SetLoanNum(num); // Accessing Superclass Methods from a Subclass
else
{
num = num % 1000;
base.SetLoanNum(num); // Accessing Superclass Methods from a Subclass
}
}
}
/*
Loan #111 for Morris is for $0.00
Loan #111 is for a 1992 Toyota
Loan #888 is for a Jefferson $18,000.00
Loan #888 is for a 2002 Chevrolet
*/
Loading
VulpesPosted Jan 15, 2012, 6:36 AM
You don't have to use a feature every time just because the language allows you to do that. In fact, in my experience, hiding base class members is not done very often. You may do it by accident, because you've forgotten that the base class already has a member with the name you want to use in the derived class but, if you do that, the compiler will warn you and more often than not, you'll then choose a different name.
Hiding a base class member deliberately is seldom necessary.
With regard to the second question, if you do need to hide a base class member in the class's public interface, then you might still want to access it internally, The 'base' keyword exists for this purpose, amongst others.
Posted Jan 15, 2012, 7:50 AM
Prabhu RajaPosted Jan 15, 2012, 6:27 AM
Actually in this case, you are not hiding SuperClass Method SetLoanNum(int num) from Sub class. I think, you should change your base class method definition as,
And Sub Class Method Definition to ,