private-methods

Overriding private methods in Java

谁说我不能喝 提交于 2019-11-26 02:12:45
问题 As succinctly described here, overriding private methods in Java is invalid because a parent class\'s private methods are \"automatically final, and hidden from the derived class\". My question is largely academic. How is it not a violation of encapsulation to not allow a parent\'s private method to be \"overridden\" (ie, implemented independently, with the same signature, in a child class)? A parent\'s private method cannot be accessed or inherited by a child class, in line with principles

JavaScript private methods

戏子无情 提交于 2019-11-25 23:15:10
问题 To make a JavaScript class with a public method I\'d do something like: function Restaurant() {} Restaurant.prototype.buy_food = function(){ // something here } Restaurant.prototype.use_restroom = function(){ // something here } That way users of my class can: var restaurant = new Restaurant(); restaurant.buy_food(); restaurant.use_restroom(); How do I create a private method that can be called by the buy_food and use_restroom methods but not externally by users of the class? In other words,