Program to demonstrate protected keyword usage in Java Inheritance.
Output of the program :
package com.hubberspot.code;
class Shape{
private int x;
private int y;
protected int area(int w , int h){ // protected
this.x = w; // available to Rectangle
this.y = h;
return x*y;
}
}
public class Rectangle extends Shape{
public static void main(String[] args) {
Rectangle rect = new Rectangle();
int area = rect.area(10, 20); // area method declared protected
System.out.println("The Area of Rectangle is " + area);
}
}
Output of the program :
