• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Accessing inherited protected instance variable question

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following 2 classes (Dog.java extends Animal.java), please help me with the 2 questions listed below them.



Question 1 - Why do I get the following compile error for line 5 but get no error for line 4 in Dog.java?

C:\SCJP\CODEPR~1>javac pkgb\Dog.java
pkgb\Dog.java:9: <identifier> expected
System.out.println("1-protected Animal instance variable i = " + i);
^
1 error

Question 2 - How do I access the inherited instance variable i in the main method of Dog.java?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Lawrence wrote:
Question 1 - Why do I get the following compile error for line 5 but get no error for line 4 in Dog.java?

C:\SCJP\CODEPR~1>javac pkgb\Dog.java
pkgb\Dog.java:9: <identifier> expected
System.out.println("1-protected Animal instance variable i = " + i);
^
1 error


Statement(except declaration statement) must be inside a block/method .

Edward Lawrence wrote:
Question 2 - How do I access the inherited instance variable i in the main method of Dog.java?


way - 1:
create a instance of subclass/Dog and then access the protected variable of super class which stay in another package.

way - 2:
make the protected variable i as static in super class

hope this helps
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome!! Thanks.
 
Sheriff
Posts: 6888
1312
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't forget to UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise.
I've added to the code tags for you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic