This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Overriding package-private abstract methods.

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com;
public abstract class AbsClass {
abstract void Method();
}
-----------------------------------------
package org;
class Try_8_4_6 extends com.AbsClass {
void Method() {}
}
On compiling Try_8_4_6 , I get an error.
Is it not possible to override an abstract method that has default access in a different package ?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, it's not possible. It has nothing to do with being abstract - it is not possible to override any method if that method is not accessible. Since the method is defined with package-level access, it is only accessible within that same package, and can only be overridden within that package.
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com;
public abstract class AbsClass {
abstract void Method();
}
------------------------------------------------
package org;
import com.*;
public abstract class Try_8_4_6 extends AbsClass {
}
class Derived extends Try_8_4_6 {
public void Method() { }
public static void main(String[] args) {
System.out.println("hello world ");
}
}
Using java.exe ( JDK1.2.2 ) on Win32 platform :
C:\org>javac Try_8_4_6.java
Try_8_4_6.java:7: class org.Derived must be declared abstract. The package-priv
ate abstract method void Method() in superclass class com.AbsClass is inaccessib
le and cannot be overridden.
class Derived extends Try_8_4_6 {
^
1 error
Using the latest jikes compiler :
C:\org>jikes Try_8_4_6.java
C:\org>
Compiles without error.
My point is :
If a class declares a method as abstract with default access, any class that extends it outside the package MUST be abstract.
Therefore, DO NOT use abstract default access methods if the class has to be extended.
Otherwise, the method will remain abstract outside the package.

 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak,
Very good point in my opinion. Eventhough the package-private abstract method from one public file which is inside a package is not known to the other class which is in other package and wants to extend the before said public class, it HAS TO BE said as abstract THOUGH there is NO ABSTRACT METHODS to override and make it as an non-abstract class.
regds
maha anna
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!