final, private

L

lsrinu

Suppose I dont want to inherit my super class methods in the sub class,
here I can write all my methods with final, instead of this if I can
declare all the methods are private in this case also these are not
inherited, what is the basic difference between these two, where we use
private, where we use final
 
P

Paul Bilnoski

lsrinu said:
Suppose I dont want to inherit my super class methods in the sub class,
here I can write all my methods with final, instead of this if I can
declare all the methods are private in this case also these are not
inherited, what is the basic difference between these two, where we use
private, where we use final

One example of the difference is that subclasses can still access
non-private, e.g. protected/public, final methods but not private methods.
The access modifier specifies what scopes can call the method, such as
same-class, subclass, same-package, other-package. The final modifier
simply makes it so subclasses cannot override.

--Paul
 
T

Thomas Hawtin

lsrinu said:
Suppose I dont want to inherit my super class methods in the sub class,
here I can write all my methods with final, instead of this if I can
declare all the methods are private in this case also these are not
inherited, what is the basic difference between these two, where we use
private, where we use final

I wouldn't worry about final as applied to methods. Certainly make
member variables final if you can (where final means something
different). Make classes final if you like. For methods it is probably
more hassle than it's worth.

Do restrict access to private if possible. Use private methods where you
can. Private member variables everywhere. Private methods do act as if
they were final, but it rarely makes much difference.

Tom Hawtin
 
C

Chris Smith

lsrinu said:
Suppose I dont want to inherit my super class methods in the sub class,
here I can write all my methods with final, instead of this if I can
declare all the methods are private in this case also these are not
inherited, what is the basic difference between these two, where we use
private, where we use final

The final modifier prevents someone from overriding a method. It does
NOT prevent the method from being inherited; merely from being
overridden.

The private modifier does this, and more. Specifically, private
prevents the method from called OR overridden by any code except the
immediate implementation of that class. Because the private method is
not visible outside the class, the subclass can declare a new method
with that same signature, but it will NOT override the private method
from the superclass. (It's worth noting that Java borrows many concepts
from C++, but this is not one of them; it's a notable difference from
C++'s access specifiers.)

As a side note: whether the private modifier prevents the private method
from being "inherited" is a question of terminology; in the terminology
used by the Java Language Specification, private methods are not
inherited... but don't let that confuse you into thinking that they
can't execute on instances of a subclass. You would just need an
accessible path to get there. For example, a public method in the
superclass may still call the private method, and if it is not
overridden or if it's called via the 'super' keyword from somewhere in
the subclass implementation, then you have the path you need to execute
the private method. You should probably not assume that others are
thinking of the same precise definition of "inherited" if you are
communicating with other people about Java.

Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top