Explore core Java object-oriented concepts essential for backend development, with questions covering classes, inheritance, encapsulation, and polymorphism.
Which keyword is used in Java to create a new object from a class?
Explanation: The 'new' keyword is required in Java to instantiate a new object from a class. 'create' and 'make' are not valid Java keywords, while 'instance' relates to the idea but is not the keyword used for object creation.
What keyword allows a class in Java to inherit properties and methods from another class?
Explanation: 'extends' enables a class to inherit from a superclass. 'implements' is for interfaces, and 'inherits'/'inheritsfrom' are not valid Java keywords, making 'extends' the only correct choice.
Which access modifier restricts member variables to be accessed only within the class itself in Java?
Explanation: 'private' restricts access strictly to the same class, ensuring encapsulation. 'protected' allows subclass and package access, 'public' allows access everywhere, and 'default' gives package-level access, none of which fully encapsulate the field.
What is runtime polymorphism in Java typically achieved through?
Explanation: Runtime polymorphism is mainly achieved by method overriding, where a subclass provides a specific implementation of a superclass method. Method overloading involves compile-time polymorphism, while variable shadowing and constructor chaining do not relate to runtime polymorphism.