AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Java define virtual method8/1/2023 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. Why this? Could you please clarify my doubt? Note: In case of overriding C++ does not depends on access specifier where as Java depends. 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'.). 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. Java and C++ had different creators, with different design philosophies, so they created different designs for their languages. Java is not C++, it shouldn't be compared to C++ because it is not the same or a child language. 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'). ![]() 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. Thank you so much for spending time to understand my requirements and sharing your thoughts. Please feel free to write comments/feedbacks/solutions to this. My overall aim is how to achieve the functionality as in C++ (sample code written above). Then my question is how to define non-virtual method in sub class with private access specifier as the way in C++. ![]() 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. The output of the above c++ program is something like below: Std::cout add() //Invoke sub class method at run time by overriding mechanism/virtual mechanism Why i want this because i am trying to simulate same as in C++. 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 Requirement: declare private non-virtual method with name and signature matches to super class method "display" Super class method with public access specifier 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?īelow is the details of the requirements:
0 Comments
Read More
Leave a Reply. |