1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the issue with duplicating code in classes?
Duplicating code creates opportunities for errors (inconsistencies), makes maintenance harder, and makes the code harder to read.
What is inheritance in OOP?
Inheritance allows a class to inherit properties and behaviors from another class, essentially using a common template to share functionality and keeping the specific code in the subclass.
What are the three pillars of Object-Oriented Programming (OOP)?
The three pillars of OOP are encapsulation, inheritance, and polymorphism.
What does encapsulation enforce in OOP?
Encapsulation enforces that every class should be responsible for itself, typically by using public, protected, default, or private access modifiers.
What is the principle of encapsulation regarding class visibility?
The principle of encapsulation suggests that everything should be private by default, with public or protected methods and properties exposed only when necessary.
What are constructors in OOP?
Constructors are special methods that initialize an object when it is created using the new
keyword. They are important for encapsulation, as the class should manage its own initialization.
What rules govern superclass constructors in OOP?
Superclass constructors must always be called using the super
keyword, and it must be the first statement in the subclass constructor. If none is written, the compiler inserts a call to the no-parameter constructor of the superclass.
How does inheritance relate to the Object
class in Java?
In Java, every class extends the Object
class by default, which provides fundamental methods like .equals()
. You don't need to explicitly extend Object
, as it is inherited automatically unless another class is extended.
Which keyword is used in Java to create a subclass?
extends
Which access modifier allows a subclass to access a superclass's members but restricts access from unrelated classes?
protected
What is the root class that all Java classes inherit from?
Object
What a superclass reference can store?
An instance of any subclass
What will happen if a subclass doesn't explicitly call super() in its constructor?
The superclass's default constructor will be called automatically
What is the primary disadvantage of arrays in Java?
Arrays have a fixed size, making it difficult to add or remove elements once created.
What is the benefit of using ArrayList over an array in Java?
ArrayLists can dynamically grow and shrink in size, making them more flexible than arrays for tasks where the number of elements is unknown.
What is the syntax for importing a specific class from a package in Java?
The syntax for importing a specific class is import packageName.ClassName;
.
What is the syntax for importing all classes from a package in Java?
The syntax for importing all classes from a package is import packageName.*;
.
Why is comparing strings in Java using ==
a bad practice?
The ==
operator checks for object identity, not equality. It compares memory addresses, not the actual content of the string.
How should strings be compared in Java?
Strings should be compared using the .equals()
method, which checks for content equality rather than object identity.
What does the super
keyword do in Java?
The super
keyword refers to the superclass and is used to call the superclass's constructor or methods.
What is encapsulation in Java?
Encapsulation is the practice of hiding an object's internal state and requiring all interactions to be performed through methods, ensuring that the object is responsible for its own state.
What is the default access level for members of a class in Java if no access modifier is specified?
The default access level is package-private, meaning that the members are accessible only within the same package.
What is polymorphism in Java?
Polymorphism is the ability of an object to take many forms, meaning that a subclass object can be treated as an object of its superclass type.
What is the purpose of a constructor in a Java class?
A constructor initializes a new instance of a class, setting up its initial state and allocating memory for the object.
What is the role of a protected
access modifier in Java?
A protected
access modifier allows access to a class member by its own class, subclasses, and classes in the same package.
What happens if a superclass does not have a constructor in Java?
If no constructor is explicitly defined in the superclass, Java automatically provides a default no-argument constructor.
What is an example of a design pattern in Java?
An example of a design pattern is the iterator pattern, which provides a standardized way to iterate over elements of a collection. \