Live Webinar: Prompt Engineering: Skill Everyone Must Learn Today
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Simple Program of Inheritance in C#
WhatsApp
Favas CM
Jul 03
2015
1.4
k
0
0
Use Inheritance Create 3 classes, first Parrots, second Kiwis and third Mousebird. After then create a function to print details about birdsdetails1. colour2. weight3. length4. height create objects of the three classes and save it in a array then retrieve the object and print details.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
UseInheritanceParrotKiwisMousebird {
public
class
parrots {
int
weight, length, height;
string
colour;
public
void
details(
string
colour,
int
weight,
int
length,
int
height) {
this
.colour = colour;
this
.weight = weight;
this
.length = length;
this
.height = height;
Console.WriteLine(
"\ncolour : "
+ colour);
Console.WriteLine(
"weight : "
+ weight +
" gm"
);
Console.WriteLine(
"length : "
+ length +
" cm"
);
Console.WriteLine(
"height : "
+ height +
" cm"
);
}
}
public
class
kiwi: parrots {}
public
class
mousebird: kiwi {}
class
Program {
static
void
Main(
string
[] args) {
Console.WriteLine(
"PARROTS"
);
parrots pr =
new
parrots();
pr.details(
"red"
, 200, 8, 4);
Console.WriteLine(
"\n\nKIWI"
);
kiwi kw =
new
kiwi();
kw.details(
"yellow"
, 230, 10, 8);
Console.WriteLine(
"\n\nMOUSEBIRD"
);
mousebird mb =
new
mousebird();
mb.details(
"blue"
, 250, 14, 8);
Console.ReadLine();
}
}
}
Output
PARROTS
colour :red
weight :200 gm
length :8 cm
height :4 cm
KIWI
colour : yellow
weight : 230 gm
length : 10 cm
height : 8 cm
MOUSEBIRD
colour : blue
weight : 250 gm
length : 14 cm
height : 8cm
C#
Program of Inheritance
Up Next
Simple Program of Inheritance in C#