Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

subclass methods

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

just look at this scenario..

B extends A and c extends B ..and we were overriding a method in B of Super Class A.
And i doesnt want that method gets used in class C.

With the keyword final we cannot override the method but we can use that overrided method of super class B in the class C.

I want to prohibit even that....Is there any way to do that.

Thank you
sruthi sen
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you can not do that. All child classes will inherit properties of their parent classes, so if you can do that it is against the OOP.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sruthi i think my making the method as private in class B (but the method will override the same method in class A)we can prevent it from getting available in class c.

try it.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method is marked as public in class A, we cannot mark the overriding method in class B as private because we cannot reduce the scope of a method. We can make it final though so that it cannot be further overridden.

If you mark the method in Class A and Class B private, then you cannot invoke the method from anywhere outside the class.

regards,
vijay.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes vijay you are right, my answer was wrong.
 
Sruthi Sen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kiran.......but what if my super class method is Public method..........while overriding we cannot narrow the accessability...?

so. i think if the super class A's method is only of private.....then if we say in class B as final we can stop its inheriting to the sub class C.

i think its not possible with public method....


sruthi
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sruthi Sen:

so. i think if the super class A's method is only of private.....then if we say in class B as final we can stop its inheriting to the sub class C.



If you say final you can not stop from inheriting, it just stops from overriding.
 
Sruthi Sen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kiran.......but what if my super class method is Public method..........while overriding we cannot narrow the accessability...?

so. i think if the super class A's method is only of private.....then if we say in class B as final we can stop its inheriting to the sub class C.

i think its not possible with public method....


sruthi
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a remark: if the method in A is private, you can't even access it in B, so you can't override it either. But you are free to redeclare it. In this case, it is treated as a completely distinct function (ie. no polymorphism would apply).

Regards
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sruthi Sen:
tell me please if all the three class in same package
if no make it protected in A

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just have the class A and Class B in same package

Make the method to default visibility

Have the Class C in another package
-----------------------------------------------------------
eg: package p1;
class A{
void method(){//do something}
}
------------------------------------------
package p1;
public class B extends A{
void method(){ //override}
}
------------------------------------------
package p2;
import p1.B;

class C extends B{
public static void main(String... args){
//can't access method() as its not visible
//outside the package;
}
}
------------------------------------------


Please correct me if wrong.
[ March 20, 2006: Message edited by: Thiyanesh S ]
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thiyanesh,

I think you're not correct. Class C cannot access class B because class B is not public.

Be careful with questions like this in the exam. Some questions ask you : "What is the result ?" Before dig into code logic, you have to evaluate whether the code compile. Class and methods visibility are good targets to starting your checks.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting,
i really missed that, and will edit.
[ March 20, 2006: Message edited by: Thiyanesh S ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic