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
325.1k
INHERITANCE
Jan 30 2012 10:48 AM
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
*/
Reply
Answers (
5
)
translate a string in c#
C# class example