Abstract Class
1) Abstract class defines functionality that is implemented by one or more subclass. 2) Abstract class states “what” to do, rather than “how” to do. 3) Abstract class cannot be instantiated. 4) Abstract method “must” be overridden in derived class. 5) Abstract classes can provide all, some, or none of the actual implementation of a class. It provides a default code or stub for their child class. 6) It is useful while creating Component.
Eg:
abstract class shape { void draw() { } }
class circle : shape { void draw() { Console.WriteLine(“Circle”); } }
class rectangle : shape { void draw() { Console.WriteLine(“rectangle”); } }
class triangle : shape { void draw() { Console.WriteLine(“triangle”); } }
static void main() { shape s; s = new circle(); s.draw(); s = new rectangle(); s.draw(); s = new triangle(); s.draw(); } //Output circle rectangle triangle
Abstract classes may contain constructors. When we can't instantiate the abstract classes, what is the use of these constructors?
A constructor of a class is not only called when its object is instantiated; it also gets called when an object of its subclass is created. So, an abstract class may contain a constructor to be called when it subclass is instantiated.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|