aspose file tools*
The moose likes Java in General and the fly likes Non-virtual method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Non-virtual method in Java" Watch "Non-virtual method in Java" New topic
Author

Non-virtual method in Java

Asisranjan Nayak
Greenhorn

Joined: Mar 14, 2011
Posts: 6
Hi

Could you please tell me, in Java, how to declare and define non-virtual method in sub class which hides instance method from super class?

Below is the details of the requirements:


class Sup{

//Super class method with public access specifier
public void display(){
//body
}
}

class Sub extends Sup{

//Requirement: declare private non-virtual method with name and signature matches to super class method "display"

//I was trying to define method as below but i'm getting compile time error "This static method cannot hide the instance method from Sup
/*private static void display(){
//body
}*/

}


Why i want this because i am trying to simulate same as in C++. Below is sample code in C++.

------------------------------------------------------------------------------
#include <iostream>

class A
{
public:
virtual void add()
{
std::cout << "BASE";
}

};

class B:public A
{
private:
void add()
{
std::cout << "DERIVED";
}

};

int main()
{
A *p = new B();//Super class pointer reference refers to instance of sub class
p->add();//Invoke sub class method at run time by overriding mechanism/virtual mechanism
std::cout << p;
}

The output of the above c++ program is something like below:

DERIVED0xbc0010
---------------------------------------------------------------------------

Here in C++, public virtual method defined in super class and override private non-virtual method which defined in subclass.Where as in Java , as per my understanding we can't reduce visibility of inherited method from super class and all methods except final and static are by default virtual. Then my question is how to define non-virtual method in sub class with private access specifier as the way in C++.


My overall aim is how to achieve the functionality as in C++ (sample code written above).

Please feel free to write comments/feedbacks/solutions to this.

Thank you so much for spending time to understand my requirements and sharing your thoughts.


Asis
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

You can't reduce the visibility of the method, but I am not sure the difference in behavior here. Why do you need the method in the derived class to be private? You could do:

And the expected output would be "Child".


edit: Forgot to extend Super. Doh! Thanks Stuart for pointing it out!


Steve
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
You've forgotten the 'extends Super' on the Sub declaration
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Asisranjan Nayak wrote: override private non-virtual method

You cannot override a private method, end of. Nothing outside of the class has visibility to it.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Stuart A. Burkett wrote:You've forgotten the 'extends Super' on the Sub declaration

Quite right, I edited it to fix (and noted the edit so your post doesn't seem out of place).
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

James Boswell wrote:
Asisranjan Nayak wrote: override private non-virtual method

You cannot override a private method, end of. Nothing outside of the class has visibility to it.

I think from the context of the post he worded that poorly and the intent was "override [the parent method] with a private non-virtual method". He doesn't seem to want to override a private method, he wants to override a public method with a private method (reducing its visibility in the child class).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

Asisranjan Nayak wrote:My overall aim is how to achieve the functionality as in C++ (sample code written above).

DON'T. Java is NOT C++, and there are many things that are not compatible.

However: A nice, simple piece of advice I was given on the subject of your thread when I was making the transition: All Java methods are virtual, unless they're final or static.

Hopefully, you can work out what's needed from there.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Asisranjan Nayak
Greenhorn

Joined: Mar 14, 2011
Posts: 6
Hi All

Thank you very much for your nice comments.


Steve,

You are right. I want to override parent method with private non virtual method. As Winston said, "All Java methods are virtual, unless they're final or static." But Java does not allow to override parent method with private non-virtual method (private static, or private static final, or private final).

In C++, as we know we can override parent method with private non-virtual method where as Java does not allow to do same, WHY ?



Asis
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Asisranjan Nayak wrote:In C++, as we know we can override parent method with private non-virtual method where as Java does not allow to do same, WHY?

Counter-question: why is it needed? What does it provide that using a public method to override the parent doesn't? If you can't come up with a reason, then Occam's Razor suggests it is best not to include. The sample you provided in C++ works like the public method example I gave as a response. So what is the difference?
Asisranjan Nayak
Greenhorn

Joined: Mar 14, 2011
Posts: 6
Hi Steve

I am sorry to disturb you on this topic but I had discussion with my colleagues regarding this, We could not able to find exact reason Why java has different behavior than C++ in case of overriding.

Note: In case of overriding C++ does not depends on access specifier where as Java depends. Why this? Could you please clarify my doubt?



Thanks a lot.
Asis
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Asisranjan Nayak wrote:Hi Steve

I am sorry to disturb you on this topic but I had discussion with my colleagues regarding this, We could not able to find exact reason Why java has different behavior than C++ in case of overriding.


You aren't disturbing me at all. There are two ways to ask why: Why do this thing? or Why not do this thing? If there is no compelling reason to do a thing, then there is a compelling reason not to do it (it isn't needed so is only a source of confusion, possible bugs, yet-another-rule-to-make, or 'feature creep').

Java is not C++, it shouldn't be compared to C++ because it is not the same or a child language. Java and C++ had different creators, with different design philosophies, so they created different designs for their languages. Why they did one thing and not another could be rather arbitrary, just a decision that they had to make and they chose one way instead of the other. If you have a burning curiosity to know then you should find the folks who developed Java as a language and ask them, but chances are this was just one thing in a million that followed naturally from whatever design philosophy they had (so they answer would be something non-satisfying like 'because it felt right'...).

Note: In case of overriding C++ does not depends on access specifier where as Java depends. Why this? Could you please clarify my doubt?

If you have an Animal, and it has a public method walk(), then you have a Bear which extends Animal (and therefor is-an Animal) but its walk() method is private, then you could have a case were a Bear would not have the same methods as an Animal, from a third-party perspective. So how can a Bear be an Animal if it doesn't have all the Animal functionality? This would break the Liskov Substitution Principal. If you reference it from an Animal variable, then you would suddenly have access to the walk() method, and from your description above, you would expect it to call the Bear's private walk() method. So, what is the benefit of hiding the walk() method if a single cast gets you public access?

So take the statement I made earlier: is there a compelling reason to do this? If you can't come up with one, then there is a compelling reason not to do it. I can't think of a reason to do it myself, so I would suggest that it should be done. Perhaps in C++ there is a reason, or perhaps the folks who wrote the C++ language specs didn't have the same philosophy.
Asisranjan Nayak
Greenhorn

Joined: Mar 14, 2011
Posts: 6
Hi Steve

Thanks for your reply. I think your last comments will clear my doubt.

Now, I am going through "Liskov Substitution Principal" principle. I will update you soon.


Thanks
Asis
Asisranjan Nayak
Greenhorn

Joined: Mar 14, 2011
Posts: 6
I got answer of my question from "Liskov Substitution Principal", "Covariance and Contravariance"
 
Don't get me started about those stupid light bulbs.
 
subject: Non-virtual method in Java
 
Similar Threads
Doubt with Inheritence
Method Overriding
Problems??
JVM crash
Question about inheritance...