Hello everyone,
Kindly explain "What is the use of Inheritance? and When to use?" with respect two below programs.
Why I cannot get output from 1st program after using class initialization?
Program 1:
- class Program
- {
- class Shape
- {
- public void setWidth(int w)
- {
- width = w;
- }
- public void setHeight(int h)
- {
- height = h;
- }
- public int width;
- public int height;
- }
-
-
- class Rectangle
- {
- Shape objshape = new Shape();
- public int getArea()
- {
- return (objshape.width * objshape.height);
- }
- }
- static void Main(string[] args)
- {
- Shape Rect = new Shape();
- Rectangle objRectangle = new Rectangle();
- Rect.setWidth(5);
- Rect.setHeight(7);
-
-
- Console.WriteLine("Total area: {0}", objRectangle.getArea());
- Console.ReadKey();
- }
- }
Program 2:
- class Program
- {
- class Shape
- {
- public void setWidth(int w)
- {
- width = w;
- }
- public void setHeight(int h)
- {
- height = h;
- }
- public int width;
- public int height;
- }
-
-
- class Rectangle : Shape
- {
- public int getArea()
- {
- return (width * height);
- }
- }
- static void Main(string[] args)
- {
- Rectangle Rect = new Rectangle();
-
- Rect.setWidth(5);
- Rect.setHeight(7);
-
-
- Console.WriteLine("Total area: {0}", Rect.getArea());
- Console.ReadKey();
- }
- }