File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call subclass from class

 
nani aman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have a problem about calling datamember subclass from class. My superclass is Security.java while my subclass is Confidential with data member count. How can i call count from Security class?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless count is static, I believe the superclass will need a reference to an instance of the subclass.

 
nani aman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i declare count in security as protected. Can i use in the same way you told before?
 
nani aman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more if you don't mind. i organize my subclass first then my superclass. How can i invoke the subclass to generate by itself n then getCount? For the time being in each superclass and subclass have their own main method.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You previously said that count was in the subclass!

Please post your code, it will save confusion.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To marc,
How would your base class work if someone else wanted to subclass it? Say: class Secret extends Security
-Barry
 
nani aman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Barry, i done the classes independantly then after it well organize, i want to combine as the Security class can view the results of Confidential class so i have declaration count in both classes. Is protected int count in Security can call as same way as marc told before?


Thanks a lot.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
To marc, How would your base class work if someone else wanted to subclass it? Say: class Secret extends Security -Barry


I'm not sure I'm following, unless you're suggesting that some security be added so that another extension of the superclass couldn't be used to access that variable in the subclass. If so, then I expect this would be done with private modifiers on the superclass method and on the superclass's refernce to the subclass. (?)

Or did you have something else in mind?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nani aman:
...i have declaration count in both classes...

Sounds like trouble... You need to post your code, as Barry suggested.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To marc,
I was more thinking about your method getConfCount. The base class could be extended by any class (Security was just a random choice of name, could be KingKong if you want).
[ October 22, 2004: Message edited by: Barry Gaunt ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
To marc, I was more thinking about your method getConfCount. The base class could be extended by any class (Security was just a random choice of name, could be KingKong if you want).

Hmmm... The argument could be the superclass type, so it would then accept any subclass. In this case, the getCount method would also need to be in the superclass.

???
 
nani aman
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the superclass codes

codes:

public class Security extends JFrame implements ActionListener{
protected int count;
}

codes

there is no operation on count here, i declare to access the subclass count.

This is the subclass codes

codes

public class Confidential extends Security {

int count;

public void elementCount(){

try{
while(!stack.empty()){
count++;
System.out.println("Count = "+ count);
System.out.println("Element = "+ stack.peek());
stack.pop();
};
}
catch(EmptyStackException ese){
System.out.println("Error : " + ese);

}

}

}

i can run independantly both classes, but now i want to combine these 2 class by access count from Security class.

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would ask:
  • Is there a reason for using inheritance, or can these be combined into a single class?
  • Why is count defined in both classes? (Which one do you want updated by the subclass method?)
  • How is count being used? If it really does belong in the superclass, should it be static?
  •  
    nani aman
    Ranch Hand
    Posts: 33
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is there a reason for using inheritance, or can these be combined into a single class?
    I am not sure of using the inheritance here because the security class call count and view it. I do that way because i have other 3 class which will be perform count in their own way and also view the count result in security class. Can we call that reason for me to use inheritance?

    Why is count defined in both classes? (Which one do you want updated by the subclass method?)
    The count which will be updated values is in confidential class.Security is for viewing.

    How is count being used? If it really does belong in the superclass, should it be static?
    these is the code that will calculate the count which is in confidential class. If count is static in security, it could not retrive result count from confidentiality right?

    public void elementCount(){

    try{
    while(!stack.empty()){
    count++;
    System.out.println("Count = "+ count);
    System.out.println("Element = "+ stack.peek());
    stack.pop();
    };
    }
    catch(EmptyStackException ese){
    System.out.println("Error : " + ese);

    }
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    From Bruce Eckel's Thinking in Java (2nd ed., p. 294)...

    "...you should use [inheritance] sparingly... One of the clearest ways to determine whether you should use composition or inheritance is to ask whether you'll ever need to upcast from your new class to the base class. If you must upcast, then inheritance is necessary, but if you don't need to upcast, then you should look closely at whether you need inheritance."

    If all you're trying to do is get your instances to communicate with one another, then inheritance is not what you're looking for.

    On the other hand, if you have 3 or 4 instances of things that count, then these might each subclass some base "counter" class, then override their counting method as appropriate. The subclasses could then each be upcast to the base type so they could all be handled the same way in some separate "viewing" class.

    In any case, defining the same variable in base and subclass is dangerous because variables are not overridden like methods are. These will remain two distinct variables. And with upcasting, you could then get different values depending on whether you try to access these variables directly or using methods.

    (The static modifier basically means that the variable has only one location in memory, and is not associated with any particular instance of that class. Do not confuse this with restricted access, like private. A static variable might be desirable if you were trying to capture a cumulative count from multiple instances, since each instance would update the same variable.)
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic