protected in java

/* similar to private keyword, but also lets both:
  - subclasses
  - classes in same package
  access the variable, method or constructor */

class Superclass {
  protected int myNumber = 5;
}

class Subclass extends SuperClass {
  // has access to myNumber
}

class InAnotherPackage {
  // doesn't have access to myNumber
}

3.67
6
Phoenix Logan 186120 points

                                    //The protected keyword is an access modifier used for attributes, 
//methods and constructors, 
//making them accessible in the same package and subclasses.

class Person {
  protected String fname = "John";
  protected String lname = "Doe";
  protected String email = "[email protected]";
  protected int age = 24;
}

class Student extends Person {
  private int graduationYear = 2018;
  public static void main(String[] args) {
    Student myObj = new Student();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);
    System.out.println("Email: " + myObj.email);
    System.out.println("Age: " + myObj.age);
    System.out.println("Graduation Year: " + myObj.graduationYear);
  }
}

3.67 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
protected use in java protected in java protected hava what is protected in java with example java whats a protected method java protected example java protected methods protected and protected in java how does protected work in java use of protected keyword in java protected in ja declare a protected method java protected and private java what is protected method in java what protected in java what does protected mean java protected in java meaning what is a protected in java why protected is used in java protected java protected function in java protected members in java protected private in java why we create protected methods in java protected in jaa protected method ajva how to declare a protected method in java when do you use protected java java what does protected do what is a protected class in java protected en java what is protected void in java what are protected in java protected hjava when to use protected java java protected class how to use protected java where to use protected in java how to use protected method in java is it good to use protected in java private protected java java what is protected protected or private java protected java example protected in java example what is protected keyword in java why protected in java is used protected void java protected java définition what does the protected keyword do in java when to use protected in java java protected nedir protected variables java java protected modifier java protected private what does protected in java m] protected meaning java why we use protected in java what does a protected class in java mean what does protected in java do what is keyword: protected in java how to use protected in java protected jaca when should i use the protected keyword in java java what does protected mean java protected definition what is a protected variable in java what does protected do in java java private protected java protected meaning Private Protected in Java protected method in java java protected keyword what does protected mean jva protected em java protected keyword java protected keyword in java class protected java how to java protected protected variable example In java Protected member of class can be accessed by derived class public private protected java what does protected mean in java package protected in java why we use protect keyword in get() node what is protected used for in java protected jvava what is protected class in java how to access a protected method in java private public protected java protected int java print protected field java public private protected in java how to access protected variable in child class java java protectefd java protected attribute using protected in java protected jva java protected member java private and public¨ how can a protected member be accessed in java what is a protected variable java protected access modifier on constructor java public protected private protected vs private java protected java meaning com example protected java portected access modifier what is a protected method java what are public private protected in java java public vs default java access modifiers visibility modifiers java how to access protected method in java where protected member cannot be accessed java how made protected method in java protected method can be accessed by accessing a method of protected class protected in java definition java protected vs private class a protected int method protected java access modifier package private attribute java what is protected in java What i the use of protected in java access protection in packages in java access protection in java package access specifier in java access protected variable java java protected access modifier create protected variable in java protect class java what is a proteted in java what is protected access modifier in java protected variable in java uml protected variable in java protected access modifier protected in java use a mehod with protected access protected access modifiers protected variable can be used outside of its class protected variable java java protect protected access modifier portected java has protected access java protected and private in java can i assign protected variables in subclasses Variables, methods and constructors which are declared protected can be accessed by any class in the same package protectred class methods why we use protected subclass accessing protected variable java public class protected method protected access specifier protected variables in subclass in other package java what is protected java protected accesor java A Java variable that is protected can also be protected java instance variables a protected members of the class is accessible in java protected access modifier java protected declaration in java what is protected access specifier in java protected member variables java package protection in java can there be protected class in java protected function java access control protection in java how to access protected variable in a class protected access modifier for class in java how to access protected methods in java protected methods java protected class java java protected variable protected method java protect in java protected usage in java can we access protected member derived class in java protected java protected access specifier in java protected in java protected class in java what are protected variables in java java protected method java protected
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source