Questions and Answers
All non-instantiable classes are abstract.
False
Inheritance has what kind of relationship?
Is-a relationship
What is a process that combines the data and the operations on that data?
Encapsulation
Composition has what kind of relationship?
Signup and view all the answers
The private members of the superclass can be accessed directly by the subclass.
Signup and view all the answers
All data members of the superclass are also data members of the subclass.
Signup and view all the answers
What happens in single inheritance?
Signup and view all the answers
What happens in multiple inheritance?
Signup and view all the answers
Does Java support multiple inheritance?
Signup and view all the answers
What type of inheritance does Java support?
Signup and view all the answers
What is an overriding method?
Signup and view all the answers
What is an overloading method?
Signup and view all the answers
What happens during method overloading?
Signup and view all the answers
If a subclass defines a class method with the same signature as a superclass method, the subclass method hides the one in the superclass.
Signup and view all the answers
A subclass inherits all of the public and protected members of its superclass, no matter what package the subclass is in.
Signup and view all the answers
A subclass can have its own private data members and its own constructor.
Signup and view all the answers
When we instantiate a subclass object, this object cannot directly access the private instance variables of the superclass.
Signup and view all the answers
The methods of the subclass can directly access the private members of the superclass.
Signup and view all the answers
The constructors of the subclass can directly initialize only the instance variables of the subclass.
Signup and view all the answers
What keyword is used to call a constructor of the superclass?
Signup and view all the answers
Invoking a superclass constructor must be the first line of the subclass constructor.
Signup and view all the answers
The superclass must be properly initialized, and it must be initialized before any new items are created and initialized in the subclass.
Signup and view all the answers
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the default constructor of the superclass.
Signup and view all the answers
If the superclass does not have a default constructor, we will get a compile-time error.
Signup and view all the answers
A subclass constructor invokes a superclass constructor either explicitly (super) or implicitly.
Signup and view all the answers
What is constructor chaining?
Signup and view all the answers
A subclass cannot access the private members of the superclass.
Signup and view all the answers
A subclass can directly access what member of a superclass?
Signup and view all the answers
In an inheritance hierarchy, the public and protected members of a superclass are directly accessible in a subclass, across any number of generations.
Signup and view all the answers
In Java, if we define a class and do not use the reserved word extends to derive it from an existing class, then the class is automatically considered to be derived from the Object class.
Signup and view all the answers
What class does the toString method come from?
Signup and view all the answers
What happens when we define a class and don't use the keyword 'extends' to derive it from an existing class?
Signup and view all the answers
What is the supreme class in Java?
Signup and view all the answers
Study Notes
General Concepts
- Non-instantiable classes are not necessarily abstract; this statement is false.
- Inheritance depicts an "is-a" relationship, signifying a specialized version of a class.
- Composition reflects a "has-a" relationship, indicating ownership or part-whole association.
Encapsulation
- Encapsulation combines data and its operations, promoting modularity in design.
Class Relationships in Inheritance
- Public and protected members of a superclass are accessible to subclasses, irrespective of package location.
- Private members of a superclass are inaccessible directly from subclasses.
- Subclasses inherit all data members and methods from superclasses, unless the methods are overridden.
Types of Inheritance
- Single inheritance involves a subclass derived from a single superclass.
- Multiple inheritance, where a subclass extends multiple superclasses, is not supported in Java.
- Java allows a class to implement multiple interfaces while extending one class.
Method Overriding and Overloading
- Overriding occurs when a subclass method has the same header as a superclass method, but a different body.
- Overloading is characterized by methods sharing the same name but differing in parameters.
- Method overloading allows for multiple versions of the same method based on argument types.
Constructors and Initialization
- The
super
keyword is used to invoke a superclass's constructor. - A subclass constructor must call a superclass constructor as its first statement.
- If not explicitly invoked, Java automatically calls the default constructor of the superclass.
- A compile-time error occurs if a superclass lacks a default constructor.
Access Modifiers and Member Visibility
- Subclasses cannot access private members of the superclass; this is a fundamental restriction.
- Protected members can be accessed directly by subclasses.
- Members of the superclass remain accessible at any level in the inheritance hierarchy.
Object Class
- Every class in Java, unless specified otherwise, is implicitly derived from the Object class.
- The
toString
method originates from the Object class, serving as a default way to represent object information.
Constructor Chaining
- Constructor chaining allows a series of constructors to be invoked up to the Object class, reinforcing the inheritance relationship and ensuring proper initialization across all levels.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of Java class relationships, including concepts like encapsulation, inheritance types, and member accessibility. This quiz explores key principles that govern object-oriented programming in Java, helping you grasp how classes interact.