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
Abhilash J A
530
2.4k
598.3k
When to use Inheritance in C#?
Sep 12 2016 2:13 AM
Hello everyone,
Kindly explain "
What is the use of Inheritance? and When to use?
" with respect two below programs.
Why I cannot get output from 1st program after using class initialization?
Program 1:
class
Program
{
class
Shape
{
public
void
setWidth(
int
w)
{
width = w;
}
public
void
setHeight(
int
h)
{
height = h;
}
public
int
width;
public
int
height;
}
// Derived class
class
Rectangle
{
Shape objshape =
new
Shape();
public
int
getArea()
{
return
(objshape.width * objshape.height);
}
}
static
void
Main(
string
[] args)
{
Shape Rect =
new
Shape();
Rectangle objRectangle =
new
Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine(
"Total area: {0}"
, objRectangle.getArea());
Console.ReadKey();
}
}
Program 2:
class
Program
{
class
Shape
{
public
void
setWidth(
int
w)
{
width = w;
}
public
void
setHeight(
int
h)
{
height = h;
}
public
int
width;
public
int
height;
}
// Derived class
class
Rectangle : Shape
{
public
int
getArea()
{
return
(width * height);
}
}
static
void
Main(
string
[] args)
{
Rectangle Rect =
new
Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
Console.WriteLine(
"Total area: {0}"
, Rect.getArea());
Console.ReadKey();
}
}
Reply
Answers (
1
)
dynamically create Microsoft SQL Server Compact database
how to call dynamic Web Service call using Windows Service