Skip to main content

Compiler problem: "protected" behaves very strange

5 replies [Last post]
keksi
Offline
Joined: 2010-08-17
Points: 0

Hi guys

This is a very strange compiler problem I could break down to two tiny classes:

package p1;
public class Class1
{
protected static Class1 OnlyInstance;
public static void go()
{
OnlyInstance = new Class1();
OnlyInstance.doSomething();
}
</pre>
<pre> protected void doSomething(){}

}
</pre>

<pre>package p2;

import p1.Class1;
public class Class2 extends Class1
{
public static void go()
{
OnlyInstance = new Class2();
OnlyInstance.doSomething();
}
</pre>
<pre> public static void go2()

{
new Class2().doSomething();
((Class1)new Class2()).doSomething();
}
}

Class2 doesn't compile, saying in 2 places that the method doSomething() is not visible. But Class2 is a subclass of class1 and should therefore see all its protected methods.

It's a mystery to me. Any clues?

Many thanx
Keksi

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mohana_sundaram
Offline
Joined: 2012-08-02
Points: 0

Hi,
You create two classes as Class1 and Class2.
You told Class2 is a subclass for Class1.
But in program u does not mention which is the super class.
Using "main" only differ as super and sub class...
So declare main function to class1..Then only it will compile otherwise compiler confused which is the main class......

keksi
Offline
Joined: 2010-08-17
Points: 0

mohana_sundaram wrote:
Hi,
You create two classes as Class1 and Class2.
You told Class2 is a subclass for Class1.
But in program u does not mention which is the super class.

When the superclass is not specified, it is automatically set to "Object". Hence the superclass of "Class1" is "Object". The superclass of "Class2" is of course "Class1".

mohana_sundaram wrote:
Using "main" only differ as super and sub class...
So declare main function to class1..Then only it will compile otherwise compiler confused which is the main class......

Adding a main method to Class2 doesn't change anything - it still doesn't compile.

Thanx for your contribution
Keksi

mohana_sundaram
Offline
Joined: 2012-08-02
Points: 0

I just Re-edit the code...Try this.....

/**
*
* @author mohan
* Concept of protected Access specifier is to access the protected method by same class members and sub class members
*
*/

class Class1 //Parent class
{
protected void doSomething(){
System.out.println("check1");
}
}

class Class2 extends Class1 //Child class
{
public void dosome(){
System.out.println("check2");
}
}
class Class3
{
public static void main(String args[])
{
Class2 c2=new Class2(); //Instance for sub class "Class2"
Class1 c1=new Class1(); //Instance for sub class "Class1"
c2.doSomething(); //Access base class(Class1)method using sub class(Class2)instance object  
c1.doSomething(); //Access base class(Class1)method using base class(Class1)instance object
}
}
keksi
Offline
Joined: 2010-08-17
Points: 0

Dear Mohan

You re-edited too much... you're missing one important point: the classes Class1 and Class2 have to be in different packages.

Best regards
Keksi

mohana_sundaram
Offline
Joined: 2012-08-02
Points: 0

I try....this...check it and tell......

Parent class

package pack1;

public class Class1 //Parent class
{

protected static void doSomething(){
System.out.println("check1");
}


}

Child class

package pack2;
import pack1.Class1;
public class Class2 extends Class1 //Child class
{
public void dosome(){
System.out.println("check2");
}
public void go(){
Class2.doSomething();
Class1.doSomething();
}
}

Main class

package final1;
import pack2.Class2;
public abstract class Test1 {
public static void main(String args[])
{

new Class2().dosome();
new Class2().go();
}

}