Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot