• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Class inheritence

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two classes here:

Accessing.java:

package access_modifier;
import access_modifier.accessed.*;
class Accessing extends Super{
public void absMethod(){}
public static void main(String... asd){
System.out.println(5);
}
}

Super.java:

package access_modifier.accessed;
abstract public class Super{
abstract void absMethod();
}

Now, if Accessing.java is compiled, an error has occurred implying that Accessing is not defined as abstract and did not override abstract method absMethod() of Super class. It is to be noted here that access-modifier of absMethod() in Super class is default (no-access) and it should not be seen from any any class that is in the different package like Accessing.java. Then why does this error occur??
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the Packages, Then try! Because accessed is a sub package of your access_modifier
 
Samir Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changing packages doesn't work. The same compiler error still exists !!
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samir Das wrote:It is to be noted here that access-modifier of absMethod() in Super class is default (no-access) and it should not be seen from any class that is in the different package like Accessing.java. Then why does this error occur??

That is why error occurs. Because sub class SHOULD implement the abstract methods of super class, unless the sub class is also abstract class.

What happens if error DOES NOT occurs and you try like this

Samir Das wrote:Changing packages doesn't work. The same compiler error still exists !!


It worked for me
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samir Das wrote:Changing packages doesn't work. The same compiler error still exists !!



Are you using any IDEs to create the java files? or otherwise, just TextPad or NotePad? This issue is related to the CLASSPATH. Change the CLASSPATH environment variable according to your requirements!

Have a look on this

Hope you get-rid of it!
 
Samir Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using notepad to create java files.

The directory structure for these two files are like this:

c:\java\access_modifier\Accessing.java

and

c:\java\accessed\Super.java

From the directory c:\java I have invoked the following command:

javac access_modifier/Accessing.java

Then a compile error is occurred indicating that " access_modifier.Accessing is not abstract and does not override abstract method absMethod() in accessed.Super "

Now these two .java files are as follows:

Super.java:

package accessed;
abstract public class Super{
private int s;
abstract void absMethod();
static void hellp(){}
}

Accessing.java:

package access_modifier;
import accessed.*;
class Accessing extends Super{
public void absMethod(){}
public static void main(String... asd){
System.out.println(10);
}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you just need to change the method "abstract void absMethod()" in super class to "abstract public void absMethod()" , The access modifier of this method is default, it can be accessed only in the same package of the "super" class.



Samir Das wrote:There are two classes here:

Accessing.java:

package access_modifier;
import access_modifier.accessed.*;
class Accessing extends Super{
public void absMethod(){}
public static void main(String... asd){
System.out.println(5);
}
}

Super.java:

package access_modifier.accessed;
abstract public class Super{
abstract void absMethod();
}

Now, if Accessing.java is compiled, an error has occurred implying that Accessing is not defined as abstract and did not override abstract method absMethod() of Super class. It is to be noted here that access-modifier of absMethod() in Super class is default (no-access) and it should not be seen from any any class that is in the different package like Accessing.java. Then why does this error occur??

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The access modifier of super class is public so class definition is public .Any other class could extend it from everywhere.
But just Same package classes( classes that are in the same package) could implement abstract method cause absMethod() has default access modifier!
it's better to change access modifier of super class to default!

http://www.google.com/profiles/morena1380
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samir,

The problem you are having is because of the declaration of your method abstract void absMethod();
It has no modifier (default access), so it is not visible outside the package accessed.

Change your declaration into

and it will work.
Regards,
Frits
 
Samir Das
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot fellows for your replies. But, my confusion is yet to be solved !!
Well, I know that the problem occurred with my code would be resolved if I would use public modifier with the absMethod of Super class. But, just think about it. What should be the situation if I leave it with no-access modifier. As the subclass "Accessing.java" is in a package different than the Super class, it should not be aware of the absMethod() of Super class. If this method does not exist (or is not inherited, whatever you say) in Accessing.java class, then why does this problem of overriding occur. Should not java compiler treat the absMethod() of Accessing.java as a normal method rather than an inherited one ??
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Samir,

Your Super class is abstract, that means no objects can be created out of it.
  • The rule is: the first concrete subclass must implement all abstract methods of the super-class

  • You are not overriding the method but implementing the method and you can only implement it if it is visible. The method with the default access will only be visible inside the package access_modifier.accessed.

    Does this make sense to you?
    Regards,
    Frits
     
    reply
      Bookmark Topic Watch Topic
    • New Topic