adspace


What is the difference between abstract class vs interface? Can give me the real time examples?

Answer Posted / Sachin Bajpai

Abstract classes and interfaces are both used to achieve polymorphism in object-oriented programming, but they have some differences:

1. Method Implementation: Abstract classes can have concrete methods with implementation, whereas interfaces only define method signatures without implementation.
2. Inheritance: A class inherits from one abstract class, but can implement multiple interfaces.
3. Real-time example: Let's consider an example of a shape hierarchy where we have a Shape abstract class and two interfaces IPrintable and IResizable. The Shape abstract class could contain properties like Color and Name, while the IPrintable interface would define a Print() method and IResizable would define Resize() methods.
4. An example implementation might look something like this:

```csharp
public abstract class Shape
{
public string Color { get; set; }
public string Name { get; set; }
}

public interface IPrintable
{
void Print();
}

public interface IResizable
{
void Resize(int newSize);
}

public class Circle : Shape, IPrintable, IResizable
{
public double Radius { get; set; }

public void Print()
{
// Implementation of the print method for the circle shape
}

public void Resize(int newSize)
{
// Implementation of the resize method for the circle shape
}
}
```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by query string?

1013


hi .net gurus. plz if any one has dumps on 70-631 and 70-541 on windows sharepoint services kindly mail me.

1714


a web application needs to be created to accept the product name and quantity of a toy from a customer. After the customer has entered the product name the application needs to display the discounted price of the product to the customer (company is offering 35% discount on all products). The application should allow the customer to select the product name from a list box. and also while i'm data binding to a label with custom data binding with some declarations : "The Discounted Price is "+((System.Convert.todouble(lblprodprice.text)*(system.convert.todouble(txtqty.text)) - ((System.convert.todouble(lblprodprice.text)*(system.convert.todouble(txtqty.text)*0.35)). Where i need to give this declaration in asp.net 2.0.

1890