Assume that a definition of class Automobile has a composite object called my En
ID: 3555322 • Letter: A
Question
Assume that a definition of class Automobile has a composite object called my Engine. Also assuembers of class Engine me the all the data members of class Engine that contain information about the engine such as displacement, horspower, torque, etc. Are all private data members of the engine class. wirte the function prototype that would be required for an object of class Automobile to acces the dtat member horspower of my engine object. Explain your answer, including what kind of a function this is, what class it would be definedin, what its attribute is, why is necessary.
Explanation / Answer
class Engine { private int displacement = 4; } public class { public static void main(String[] args) throws Exception { Field myEngine = Engine.class.getDeclaredField("displacement"); myEngine.setAccessible(true); myEngine.get(new Engine()); System.out.println("myEngine.displacement"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.