Explain what is an interface and what is an abstract class? Please, expand by examples of using both. Explain why?



Explain what is an interface and what is an abstract class? Please, expand by examples of using both..

Answer / Rohit Sachan

An interface defines a contract that a class must implement to inherit its members. It doesn't have any implementation itself and is used for polymorphism.nnExample: An IShape interface might define methods like Area() and Perimeter(), but doesn't provide an implementation:ntpublic interface IShape{nttdouble Area();nttdouble Perimeter();nt}nnA class implementing the IShape interface must provide its own implementation for each method defined in the interface.nnAn abstract class is a class that cannot be instantiated and serves as a base class for other classes. It may have some implemented methods, but it's usually used to define common properties or behavior that can be shared among multiple derived classes.nnExample: An abstract Shape class might define default implementations for Area() and Perimeter() methods and provide a concrete implementation for simple shapes like Circle and Rectangle:ntpublic abstract class Shape {nttprotected double _width; nttprotected double _height;nttpublic double Area() {ntttreturn _width * _height;ntt}nttpublic double Perimeter() {ntttthrow new NotImplementedException();ntt}nt}ntpublic class Circle : Shape {nttprotected double _radius;nttpublic override double Area() {ntttreturn Math.PI * (_radius * _radius);ntt}nttpublic override double Perimeter() {ntttreturn 2 * Math.PI * _radius;ntt}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Dot Net General Interview Questions

What is .NET Framework?

2 Answers  


What is UDDI and how to register the web service in it?

1 Answers   TCS,


Do you know the difference between the stack and the heap?

1 Answers  


What is the difference between a namespace and assembly name in .net?

1 Answers  


What is garbage collection in .net? Explain the garbage collection process?

1 Answers  


Explain the difference between public and static modifiers?

1 Answers  


How does linq work?

1 Answers  


What is common language specification (cls)?

1 Answers  


asp mean

3 Answers   CTS, TCS,


How do you generate a strong name?

1 Answers  


What is managed code in .NET?

1 Answers  


Can you write a class without specifying namespace?

1 Answers  


Categories