• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protectecd (Access Modifier for field)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made package and used all access modifiers for fields.
In super class I have put,
private String lp = "MH-01-1234";
public String make = "Hero";
String model = "Honda";
protected int year = 2007;
The super class is put in "user" package and subclass is put in
"user.java2" package.
I tried to access "year" in sub class but could not. The compilation error it shows is "the field 'year' is protected in parent class" If disable this usage other things work fine.

Kindly guide.

Thanking you
yours
K S Hegde.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K S !

This seems to be your first posting, so

Welcome to the ranch!


To your question:

protected members can be accessed from outside the package only through inheritance. So are you sure, that your SubClass really extends SuperClass?

Perhaps post your in here.


Yours,
Bu.
[ April 21, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should be able to use the protected variable in your subclasses. ive created sample classes based on your code let me know if you done it differently

 
K S HEGDE
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Burkhard Hassel
Here is my code:

package user;
import user.java2.*;
public class AccessModifier
{
private String licensePlate="MH-05-G-2345";
public String make="Hero Honda";
String model="Vector";
protected int year= 2007;

public static void main(String arg[])
{
AccessModifier am = new AccessModifier();
AccessModifierProtected am1 = new AccessModifierProtected();

System.out.println();
System.out.println("This Printout is from AccessModifier class");
System.out.println("The Motorcycle whose license plate number is : " +am.licensePlate);
System.out.println("The Motorcycle is : " + am.make);
System.out.println("And the Model is : " + am.model);
System.out.println("It is manufactured in : " + am.year);

am1.ampd();


}
}

package user.java2;
import user.*;
public class AccessModifierProtected extends AccessModifier
{
public void ampd()
{
AccessModifier am = new AccessModifier();
// System.out.println("The Motorcycle whose license plate number is : " +am.licensePlate);
System.out.println();
System.out.println("This Printout is from AccessModifierProtected class");
System.out.println("The Motorcycle is : " + am.make);
// System.out.println("And the Model is : " + am.model );
//System.out.println("It is manufactured in : " + am.year);
}

}
 
Marshal
Posts: 79261
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to create an instance of the subclass in the superclass. Not a good idea.
You are trying to create an instance of the superclass in the subclass. Not a good idea.

My compiler couldn't find the superclass file when I tried to compile the subclass, and it couldn't find the subclass file when I tried to compile the superclass. You have created a circular dependency, which the compiler cannot resolve.
 
K S HEGDE
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Srikant Ramu,

Thanks for the correct code.

Yours
K S Hegde

Dear Campbell Ritchie,

Thanks for the explanation.

yours
K S Hegde
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic