Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Private methods in super class

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers!

Pls look into the below code:-

class Base {
private void method(int iBase) {
System.out.println("Base.method");
}
}

class Over extends Base {
public static void main(String args[]) {
Over o = new Over();
int iBase = 0;
o.method(iBase);
}
public void method(int iOver) {
System.out.println("Over.method");
}
}

Ans : Over.method is printed as ouput.

Per my understanding method(int iBase) is declared private in Base class, hence, it cannot be overridden. We can still overload the method. But in class Over(child of Base), Base class' private method is being overridden as it has the same return type and signature. So the compiler should complain. But actually it does not complain.

Pls clarify.

Thanks!

Neha Monga
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

Neha

The Over class does not know,that a method called method(int) defined in Base class.Because the method is private.
In the Over class you defined a new method which has same signature,in the Base class.

The method which are declared as private cannot be accessed from out side of the class
if you have doubt in that try with this

Base b=new Over();
b.method(10);

You will get compile time error


Thanks
Anil Kumar
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha,

the method(ibase) being private is not visible to the sub-class, so for it the method does not exist.

Private methods are visible only within the same class.

Gitesh
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Anil!

I tried using reference of Base class and Over class for Over type object. It gives compiler error when Base class reference is used to invoke amethod(int i) and gives output when Over class reference is used.

Thanks a bunch for explaining.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha,
try to understand the concept that the method(int i)(thats declared private in the base class) doesnt exist for the derived class.
i think this should clear your doubt.
in any case if you are not clear with this,i would love you to explain.
[ May 23, 2007: Message edited by: debasmita pattnayak ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to add one thing. in method overriding you can increase the visibility. as you have done in your example. you made your method public in child class which was private in base class.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private methods are visible only within the same class.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He neha,


listen carefully this thig,



tha thing is U can give access modifier to overriden method in child clas as exceedind accessability................


I thik U got the point...
 
The knights of nee want a shrubbery. And a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic