TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
329.1k
new and base
Jan 15 2012 5:00 AM
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
*/
Reply
Answers (
3
)
book?!
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. in visual studio 2010