Java - EncapsulationWhich of the following is the correct syntax for a getter method for a private String variable named title?Apublic void getTitle() { return title; }Bpublic String setTitle() { return title; }Cpublic String getTitle() { return title; }Dpublic void setTitle(String title) { this.title = title; }Check Answer
Step-by-Step SolutionSolution:Step 1: Identify getter method syntaxGetter returns the variable's value and has return type matching variable type.Step 2: Check optionspublic String getTitle() { return title; } returns String and returns title correctly.Final Answer:public String getTitle() { return title; } -> Option CQuick Check:Getter returns variable value [OK]Quick Trick: Getter returns variable value with matching return type [OK]Common Mistakes:Using void return type for getterConfusing setter syntax with getter
Master "Encapsulation" in Java9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Java Quizzes Classes and Objects - Instance variables - Quiz 8hard Custom Exceptions - Why custom exceptions are needed - Quiz 4medium Exception Handling - Finally block - Quiz 7medium Exception Handling - Try–catch block - Quiz 12easy Exception Handling - Throw keyword - Quiz 11easy Inheritance - Inheritance limitations - Quiz 10hard Interfaces - Multiple inheritance using interfaces - Quiz 11easy Object-Oriented Programming Concepts - Classes and objects - Quiz 11easy Polymorphism - Runtime polymorphism - Quiz 12easy Polymorphism - Runtime polymorphism - Quiz 8hard