• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Protected members -- Access through inheritance

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am a bit confused about access of protected members through inheritance. I know that protected members can be accessed through inherintance and reference if the access is done in the same package. When accessing through an outside package, access can only be granted through inheritance.

I am following the SCJP Study Guide by Kathy Sierra and Bert Bates, and I have typed the examples in Figure 1-4 on page 37. Here they are:
---------------------------------------------
package a;

public class SportsCar {

static protected void goFast() {
System.out.println("Package a protected goFast");
}

public void doStuff(){
goFast();
}
}
--------------------------------

Second file:
--------------------------------
package b;

import a.SportsCar;

public class Convertible {

public void doMore(){
goFast();
}
}
---------------------------------

The compiler shows an error on the second file line goFast(); saying it is undefined

The book says that it should work, but it is not working

Also, to use the goFast method through inheritance in another class part of package (a), I had to make the goFast method static but I don't know why this is?

Thanks for your help.

marifer514
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marifer514:
... When accessing through an outside package, access can only be granted through inheritance...


Correct. But are you using inheritance here? (Hint: You're importing a.SportsCar, but that just makes it "visible." To actually inherit from SportsCar, you need something more.)

Originally posted by marifer514:
...Also, to use the goFast method through inheritance in another class part of package (a), I had to make the goFast method static but I don't know why this is? ...


That shouldn't be. With the inheritance detail corrected, your code works fine for me without goFast being static.
[ May 23, 2007: Message edited by: marc weber ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the second file, and use extends to inherit SportsCar. The following code will not produce error

Second file:
--------------------------------
package b;

import a.SportsCar;

public class Convertible extends SportsCar{

public void doMore(){
goFast();
}
}
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Otherwise make goFast() method public and access it directly without
inheritance either with complete reference to that method as
a.SportsCar.goFast();

or

import static a.SportsCar.*;

...
goFast();
...


Thanks,
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vidya SIngh:
...use extends to inherit SportsCar...


So much for my "hint." :roll:
 
Maria Copeland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for your help. Extending it worked, don't worry about the hint

I am still having the same error when removing the static key work for goFast(); It only appears when I use the inheritance of SportsCar in a subclass from the same package (a). The error is:
Cannot make a static reference from a non-static member.

Here is the code:

package a;

public class SportsCar {

protected void goFast() {
System.out.println("Package a protected goFast");
}

public void doStuff(){
goFast();
}
}

----------------

package a;

public class Convertible extends SportsCar{

public void doThings(){
SportsCar sc = new SportsCar();
sc.goFast();
}

public void doMore(){
SportsCar.goFast(); ///error happens here
}
}



Thanks again
 
Vidya Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the convertible class, you need to change function doMore()

SportsCar.goFast(); //this is incorrect

You would have to create an instance of SportsCar, and use it like

SportsCar sc = new SportsCar();
sc.goFast();
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marifer514:
...The error is:
Cannot make a static reference from a non-static member...


The reason is that SportsCar is a class. So when you call SportsCar.goFast(); it means use the class method (static method) goFast.

But instead of being static (associated with the class itself), goFast is an instance method (associated with a particular instance). So as Vidya pointed out, you need to create an instance of SportsCar in order to call that method.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic