adspace


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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Write the .net syntax for 'while loop'?

1143