This example is given to demonstrate "ACCESSING SUPERCLASS METHODS FROM A SUBCLASS by using base reserved Keyword.
Before you access you must use new reserved Keyword to OVERRIDE SUPERCLASS METHODS. I wish to know whether my understanding is correct.
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

Posted May 3, 2014, 7:23 AM
VulpesPosted May 2, 2014, 6:00 PM
Following the change, the setYear method no longer sets the value of this field (carYear is now a method parameter and so doesn't affect the field at all).
Consequently, the field still has its default value of 0 and so this is the year that gets printed out:
Posted May 2, 2014, 3:18 PM
This is the modification:-
public void SetYear(int carYear)
{
carYear = year; //This step is eliminated and year is changed into carYear
if (carYear < 1997)
loanAmount = 0;
}
When checking num variable, name is not changed, straight away it is checked. Why it is not acceptable to above case. Problem is highlighted in blue.
Posted Jan 30, 2012, 11:41 AM
VulpesPosted Jan 30, 2012, 11:09 AM