top of page

Why protected methods

VISIT WEBSITE >>>>> http://gg.gg/y83ws?7976014 <<<<<<






What is reason for hiding the method? Improve this question. Hariharan Hariharan 1 1 gold badge 12 12 silver badges 25 25 bronze badges. Because protected is visible to the class itself like private and its subclasses. But SedanCar is a subclass of AbstractVehicle.

If you create a method in your SedanCar class, in that method you can call speedFactor. But not using object. Show 1 more comment. Active Oldest Votes. Basically understand this: You have a variable of type AbstractVehicle which is declared in another package vehicle. Improve this answer. I'm kind of surprised that it can't be called from a class that extends AbstractVehicle. Yes, it's inside a static method, but the method is enclosed by a class that extends AbstractVehicle.

It kinda makes sense because static methods don't participate in inheritance, but it still surprised me.

Yes, virtually it will be SedanCar. Back in my SCJP for 1. It isn't quite as surprising to see this now. I have tried creating another class in car package, if the protected method is overriden in SedanCar I am able to access it. Otherwise it is restricted. Show your new code if this is not satisfactory and you want more explanation. Show 4 more comments. Ouney well if it is a non-static method within SedanCar , then you should be able to access it.

That is what i expected as well but it is not so : — Ouney. Ouney well, the example I provided worked although I did need to move the Main to another package to get the error provided in the question - because protected is also package private. So, you are saying that if SedanCar is in different package than AbstractVehicle, then you can safely invoke this Assume this code to be in a non static method.

Add a comment. Jan Koester Jan Koester 1, 10 10 silver badges 26 26 bronze badges. Only way to access protected data of superclass in subclass is through inheritance below are two code snippets package nee; import parentdata.

In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name Q. Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

In terms of OOP, delimiting of the internal interface from the external one is called encapsulation. So the internal interface is exposed. All developers are civilized — they use the coffee machine as intended. So the coffee machine failed two days later. The same in programming. If a user of a class will change things not intended to be changed from the outside — the consequences are unpredictable.

The code constantly undergoes development and improvement. If we strictly delimit the internal interface, then the developer of the class can freely change its internal properties and methods, even without informing the users.

For users, when a new version comes out, it may be a total overhaul internally, but still simple to upgrade if the external interface is the same. People adore using things that are simple.

At least from outside. We want to make this open-source project available for people all around the world. Tutorial map. A real-life example For instance, a coffee machine. But most of the time get Protected fields are inherited. So protected fields are naturally inheritable. Let's explore why the protected methods are useful with an example below. Let's say we have a Base Printer class which has a public instance method Print.

Based on the users choice I would like to do one of the below:. Assume that we implement a factory pattern in order to get the Printer object based on the user preference and we will call the Print method.

Get the input data to be printed from the user We are making use of the protected FillCatridge method to inject the behavior of the Printer chosen by the user.


Recent Posts

See All

Comments


bottom of page