Hi...
I know abstract class and inheritance but I want to know main difference between the abstract class and inheritance with example ?
Thanks....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Nov 16, 2011, 10:20 AM
Abstract Class:-
1) An abstract method is created by specifying the abstract type modifier.
2) An abstract method contains no body.
3) An abstract method is not implemented by the base class.
4) An abstract method is automatically virtual.
5) A derived class must override it.
6) Abstract class can have modifiers for methods,properties etc.,
7) An abstract class can implement a property.
8) The abstract modifier cannot be applied to static methods.
9) Properties can also be abstract.
10) A class containing abstract methods must be declared as abstract with the abstract specifier.
11) There can be no objects of an abstract class.
12) If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.
13) An abstract class can inherit from a class and one or more interfaces.
14) An abstract class can implement code with non-Abstract methods.
15) Abstract class can have constant and fields.
16) An abstract class can have constructors or destructor's.
17) An abstract class cannot be inherited from by structures.
18) An abstract class cannot support multiple inheritance.
19) If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Example Abstract Class:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractClassDemo2
{
abstract class Employee
{
public Employee(string name, float billingRate)
{
this.name = name;
this.billingRate = billingRate;
}
virtual public float CalculateCharge(float hours)
{
return (hours * billingRate);
}
abstract public string TypeName();
private string name;
protected float billingRate;
}
class Manager : Employee
{
public Manager(string name, float billingRate): base(name, billingRate)
{
}
override public float CalculateCharge(float hours)
{
if (hours < 1.0F)
hours = 1.0F; // minimum charge.
return (hours * billingRate);
}
// This override is required, or an error is generated.
override public string TypeName()
{
return ("Manager");
}
}
class Clerk : Employee
{
public Clerk(string name, float billingRate) :base(name, billingRate)
{
}
override public string TypeName()
{
return ("Clerk");
}
}
class Test
{
public static void Main()
{
Employee[] earray = new Employee[2];
earray[0] = new Manager("A", 40.0F);
earray[1] = new Clerk("C", 45.0F);
Console.WriteLine("{0} charge = {1}",earray[0].TypeName(),earray[0].CalculateCharge(2F));
Console.WriteLine("{0} charge = {1}",earray[1].TypeName(),earray[1].CalculateCharge(0.75F));
}
}
}
Inheritance:-
In the real world there are many objects that can be specialized. In OOP, a parent class can inherit its behavior and state to children classes. This concept was developed to manage generalization and specialization in OOP and is represented by a is-a relationship.
The following OO terms are commonly used names given to parent and child classes in OOP:
- Superclass: Parent class.
- Subclass: Child class.
- Base class: Parent class.
- Derived class: Child class
Example Inheritance:-This example features a simple way to perform inheritance. It creates a class called Circle that contains calculations for a circle based on its radius. It calculates the diameter, the circumference, and the area. Then, a class called Sphere inherits from the Circle class:
Source File: Circle.cs
namespace FlatShapes { class Circle { private double _radius; public double Radius { get { return (_radius < 0) ? 0.00 : _radius; } set{ _radius = value; } } public double Diameter { get{ return Radius * 2; } } public double Circumference { get{ return Diameter * 3.14159; } } public double Area { get{ return Radius * Radius * 3.14159; } } } }Source File: Sphere.cs
namespace Volumes { class Sphere : FlatShapes.Circle { new public double Area { get{ return 4 * Radius * Radius * 3.14159; } } public double Volume { get{ return 4 * 3.14159 * Radius * Radius * Radius / 3; } } } }Source File: Exercise.cs
using System; using Volumes; using FlatShapes; class Exercise { static void Show(Circle round) { Console.WriteLine("Circle Characteristics"); Console.WriteLine("Side: {0}", round.Radius); Console.WriteLine("Diameter: {0}", round.Diameter); Console.WriteLine("Circumference: {0}", round.Circumference); Console.WriteLine("Area: {0}", round.Area); } static void Show(Sphere ball) { Console.WriteLine("\nSphere Characteristics"); Console.WriteLine("Side: {0}", ball.Radius); Console.WriteLine("Diameter: {0}", ball.Diameter); Console.WriteLine("Circumference: {0}", ball.Circumference); Console.WriteLine("Area: {0}", ball.Area); Console.WriteLine("Volume: {0}\n", ball.Volume); } public static int Main() { FlatShapes.Circle c = new FlatShapes.Circle(); Volumes.Sphere s = new Volumes.Sphere(); c.Radius = 20.25; Show(c); s.Radius = 20.25; Show(s); return 0; } }Datta KharadPosted Nov 25, 2011, 7:16 AM
Vineet Kumar SainiPosted Nov 24, 2011, 6:49 PM
AartiPosted Nov 16, 2011, 7:41 AM
dev 0Posted Nov 14, 2011, 11:51 PM
The main diffrence in Abstact and Intrface is-
1.Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don't need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.
2.Multiple inheritance is not possible in case of abstract class /Multiple Inheritance is possible with interface
3.Access Specifiers are been Supported in abstract class /Access Specifiers are not supported in Interface
Hope this will help you.
Thanks
VulpesPosted Nov 14, 2011, 11:46 AM
http://www.c-sharpcorner.com/Forums/Thread/148539/interface-and-abstract-class.aspx