Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in inheritance

 
Dhivya rajagopal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above program displays the following error. can't we use the input in the sub classes.why?
cannot find symbol
symbol: variable a
location: class javaapplication7.inheritance1

cannot find symbol
symbol: variable b
location: class javaapplication7.inheritance1
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
  • Properly indent your code.
  • Tell us exactly what the error was. "There was an error" is not useful.
  •  
    Campbell Ritchie
    Sheriff
    Posts: 48418
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would suggest you choose appropriate names for your classes, and capitalise them correctly. Calling a top-level class “inner” is by no means informative. And “inheritance1” is hardly better.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20495
    54
    Chrome Eclipse IDE Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can't put statements directly in a class body. They must go in a constructor, method or initializer block.
     
    Randall Twede
    Ranch Hand
    Posts: 4363
    2
    Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can't put statements directly in a class body. They must go in a constructor, method or initializer block.

    i don't see that he did that? maybe he edited it. anyway the problem looks obvious to me. both a and b are local variables of class inner. make them member variables and things will probably work as intended.

    i take it back. the code makes no sense.
    if you eliminate lines 19-23 it will probably work as expected
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20495
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Randall Twede wrote:
    You can't put statements directly in a class body. They must go in a constructor, method or initializer block.

    i don't see that he did that? maybe he edited it.

    Method add did not exist in the original post. Its code was put directly in the class. The method was "wrapped" around it afterwards.


    Dhivya rajagopal, please do not edit posts after it has been replied to. It can makes these replies look ridiculous; you can see the confusion it can cause right here.
     
    Sebanti Sanyal
    Ranch Hand
    Posts: 58
    C++ Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    a and b are neither local variables, nor class members - they are non-existent as far as the second add() method is concerned. Perhaps your solution should be something like this:

     
    Dhivya rajagopal
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi, Sorry to all for editing the post. Hereafter i won't do that. Thank you
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic