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
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Abstract Class With Abstract Method in Console Programming in C#
Rajan Singh
Sep 17
2015
Code
1.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System.IO;
using
System;
abstract
class
Shape {
public
double
dim1, dim2;
public
Shape(
double
dimension1,
double
dimension2) {
dim1 = dimension1;
dim2 = dimension2;
}
abstract
public
double
area();
}
class
Rectangle: Shape {
public
Rectangle(
double
dimension1,
double
dimension2):
base
(dimension1, dimension2) {}
override
public
double
area() {
Console.WriteLine(
"\nInside Area for Rectangle : "
);
return
dim1 * dim2;
}
}
class
Triangle: Shape {
public
Triangle(
double
dimension1,
double
dimension2):
base
(dimension1, dimension2) {}
override
public
double
area() {
Console.WriteLine(
"\nInside Area for Triangle : "
);
return
dim1 * dim2 / 2;
}
}
class
AbstractClass {
public
static
void
Main(
string
[] args) {
Rectangle newRectangle =
new
Rectangle(6, 7);
Triangle newTriangle =
new
Triangle(12, 23);
Shape referenceShape;
referenceShape = newRectangle;
Console.WriteLine(
"Area is "
+ referenceShape.area());
referenceShape = newTriangle;
Console.WriteLine(
"Area is "
+ referenceShape.area());
}
}
Output:
Inside Area for Rectangle : Area is 42
Inside Area for Triangle : Area is 138
Abstract Class
C#
Abstract Method