• 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

Questions

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i had doubt please clear me

consider a class is having the protected abstract method

class A
{
protected abstract a();
}

in another package, some other class extends the class A but declares the same method in its super class (i.e)

class B extends A
{
abstract a();//is it legal to declare like this and have its own access modifier

}

suppose if B implements the abs method then is there is any constraint in access modifier like one we have while overriding the method.

please do reply....
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi paramesh

if u give simply void a() in the class B then compiler will throw Weaker access privilege error.
(because default is more restrictive than Protected)

U can give protected or public.

I hope u clear
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE

suppose if B implements the abs method then is there is any constraint in access modifier like one we have while overriding the method.

one of the rules for overriding is

-> Overrided method should not have less restrictive access


Then in your sammple program some points to be noted

-> class A should be public then only it will be know to B

-> No return type specified

-> If one method is abstract then calss should be abstract
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could find some mistakes in your Question

1. If you are going to have two separate packages for class A and Class B then the class which you want to use must be public

2. Return types is not given for the methods

3. Both classes must be abstract since they have abstract methods

Answer to your Qn:

Whether the super class belongs to the same package or not you must not give weaker access previlage to the overriden methods.

The given below code will compile and execute

1.
package packA;
public abstract class A
{
protected abstract void a();
}

2.

package packB;
import packA.A;

abstract class B extends A
{
protected abstract void a();
}
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


According to JLS u cannot narrow the scope of a method
in a subclass,And specialy when its the case like this that
the method is abstract it is at the discretion of the coder

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
1) r u saying the access modifier should not to be more restrictive while implemeting the abstract method

2) if that is the case then since the protected method will become the private when extending outside the package, so is the private is only access modifier we need to give while implementing the abstract method outside the package.


clear my doubt please
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to JLS u cannot narrow the scope of a method
in a subclass,And specialy when its the case like this that
the method is protected abstract it is at the discretion of
the coder

 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. protected method will not become private when you inherit that in a different package.

2. For the non-inherited classes in the other package cannot access the protected method which is inherited by one class in that package

3. public, protected, default(not keyword), private is the order less restrictive to more restrictive
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi baiju

For the non-inherited classes in the other package cannot access the protected method which is inherited by one class in that package



is the above point says that the method is private which is accessed by that particular class only.not even by the other class in the same package which means the method is private not even the default access


if i am wrong clear me
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you said is correct no other class other than the inherited class in that package can access the super class protected methods when the child class is in different package

But child class of the derived class can access the above method which means it is not become private but it behaves like private for other classes in that package

conclusion:

A protected method can be accessed thru inheritance in dfferent packages and derived classes of that classes, etc....
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it baiju
thank u very much
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome
reply
    Bookmark Topic Watch Topic
  • New Topic