BlackBerryForums.com : Your Number One BlackBerry Community
     

»Sponsored Links


BlackBerryApps.com Best Sellers



Reply
 
LinkBack Thread Tools
  (#1 (permalink)) Old
doni49 Offline
Thumbs Must Hurt
 
Posts: 82
Join Date: Aug 2009
Model: Tour
PIN: N/A
Carrier: Verizon
Default Properties/Methods of Parent Class not available to Child Class? - 10-21-2009, 07:35 PM

Given the following code, why is "NextKey" not available to methods in MyClass2?

I always thought that when you extend a class, all of the member methods and properties would be part of the second class--otherwise, what's the point of extending an existing class?

Code:
class MyClass1{
  public int getNextKey(){
    return _NextKey;
  }
  public void setNextKey(int val){
    _NextKey = val;
  }
  MyClass1(){
  }
  private int _NextKey;
}

class MyClass2 extends MyClass1{
  MyClass2(){
    super();
  }
  public void doSomething(value){
    _NextKey = value;
  }
}


Don

Handspring Visor -> Handspring Visor Deluxe -> Palm Treo 650 on VZW -> Palm Treo 700p on VZW -> Blackberry Tour 9360 on VZW

Last edited by doni49 : 10-21-2009 at 07:46 PM.
   
Reply With Quote
Sponsored Links
Please Login or Register to Remove these Advertisements!

  (#2 (permalink)) Old
hrbuckley Offline
CrackBerry Addict
 
Posts: 831
Join Date: Jan 2006
Model: 9000
OS: 4.6.0.303
Carrier: Rogers CA
Default 10-22-2009, 09:57 AM

Quote:
Originally Posted by doni49 View Post
Given the following code, why is "NextKey" not available to methods in MyClass2?

I always thought that when you extend a class, all of the member methods and properties would be part of the second class--otherwise, what's the point of extending an existing class?
Try:
Code:
class MyClass1{
  public int getNextKey(){
    return _NextKey;
  }
  public void setNextKey(int val){
    _NextKey = val;
  }
  MyClass1(){
  }
  protected int _NextKey;
}

class MyClass2 extends MyClass1{
  MyClass2(){
    super();
  }
  public void doSomething(int value){
    _NextKey = value;
  }
}


BPS/Zarafa
   
Reply With Quote
  (#3 (permalink)) Old
doni49 Offline
Thumbs Must Hurt
 
Posts: 82
Join Date: Aug 2009
Model: Tour
PIN: N/A
Carrier: Verizon
Default 10-22-2009, 12:45 PM

HRB,

Thanks for that. After posting this, I found a tutorial on Sun's web site that talks about protected vs private. I'll use protected, but the reason I was TRYING to use private is that I've got other classes in the same file and I didn't want those other classes to use the other classes to have access to these properties/mthods.

I really thought that "extending" a class copied the class as sort of a starting point and that you just added new stuff or override stuff in the parent.

Oh well.


Don

Handspring Visor -> Handspring Visor Deluxe -> Palm Treo 650 on VZW -> Palm Treo 700p on VZW -> Blackberry Tour 9360 on VZW
   
Reply With Quote
  (#4 (permalink)) Old
hrbuckley Offline
CrackBerry Addict
 
Posts: 831
Join Date: Jan 2006
Model: 9000
OS: 4.6.0.303
Carrier: Rogers CA
Default 10-22-2009, 06:45 PM

All object oriented languages I've used observe the same semantics, private: avaialble to no other classes, public: available to all other classes, protected: private except for "children" for which it is public.


BPS/Zarafa
   
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2009 BlackBerryFAQ.com, BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of Research In Motion Limited.