What is the significance of using protected declaration during inheritance? Show with the help of an example.
Advertisements
प्रश्न
What is the significance of using protected declaration during inheritance? Show with the help of an example.
कोड लेखन
लघु उत्तर
Advertisements
उत्तर
- When data members of a base class are protected, relative courses can access them.
- Therefore, giving them to the derived class using the input functions is unnecessary.
- Example:
public class Shape { protected double height; protected double width; public void setValues(double height, double width) { this.height = height; this.width = width; } } // end of class Shape public class Rectangle extends Shape { public double getArea() { return height * width; // the child class is able to access the protected members of the parent class as shown above. } }
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
