BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-21-2009, 06:35 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Aug 2009
Model: Tour
PIN: N/A
Carrier: Verizon
Posts: 84
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Properties/Methods of Parent Class not available to Child Class?

Please Login to Remove!

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 06:46 PM.
Offline  
Old 10-22-2009, 08:57 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

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;
  }
}
__________________
My other Blackberry is a PlayBook.
Offline  
Old 10-22-2009, 11:45 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Aug 2009
Model: Tour
PIN: N/A
Carrier: Verizon
Posts: 84
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Offline  
Old 10-22-2009, 05:45 PM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

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.
__________________
My other Blackberry is a PlayBook.
Offline  
Closed Thread


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-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.