Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

Protected keyword usage in Java Inheritance

Program to demonstrate protected keyword usage in Java Inheritance.

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 :

© 2021 Learn Java by Examples Template by Hubberspot