Inheritance

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

27 Terms

1
New cards

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.

2
New cards

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.

3
New cards

What are the three pillars of Object-Oriented Programming (OOP)?

The three pillars of OOP are encapsulation, inheritance, and polymorphism.

4
New cards

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.

5
New cards

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.

6
New cards

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.

7
New cards

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.

8
New cards

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.

9
New cards

Which keyword is used in Java to create a subclass?

extends

10
New cards

Which access modifier allows a subclass to access a superclass's members but restricts access from unrelated classes?

protected

11
New cards

What is the root class that all Java classes inherit from?

Object

12
New cards

What a superclass reference can store?

An instance of any subclass

13
New cards

What will happen if a subclass doesn't explicitly call super() in its constructor?

The superclass's default constructor will be called automatically

14
New cards

What is the primary disadvantage of arrays in Java?

Arrays have a fixed size, making it difficult to add or remove elements once created.

15
New cards

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.

16
New cards

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;.

17
New cards

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.*;.

18
New cards

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.

19
New cards

How should strings be compared in Java?

Strings should be compared using the .equals() method, which checks for content equality rather than object identity.

20
New cards

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.

21
New cards

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.

22
New cards

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.

23
New cards

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.

24
New cards

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.

25
New cards

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.

26
New cards

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.

27
New cards

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. \