How to Get Protected Attributes in Java


In this example we will show you how to get protected attributes in Java.

Source Code

package com.beginner.examples;

class Person {
  protected String name = "Lucky";
  protected int age = 16;
}

class LuckyGirl extends Person {

  public static void main(String[] args) {
    LuckyGirl l = new LuckyGirl();
    // get protected attributes
    System.out.println("Name: " + l.name);
    System.out.println("Age: " + l.age);
  }
}

Output:

Name: Lucky
Age: 16
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments