This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing inherited protected instance variable question

 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Awesome!! Thanks.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4684
336
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic