Java OOP Concepts and Design Patterns Flashcards

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

1/157

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.

158 Terms

1
New cards

Encapsulation

Encapsulation is the bundling of data (fields) and methods that operate on that data into a single unit (class), restricting direct access to some components using access modifiers like private and providing controlled access via getters/setters.

2
New cards

extends keyword

The extends keyword is used to inherit a class in Java.

3
New cards

Abstract class vs Interface

An abstract class can have both abstract and concrete methods, while an interface (prior to Java 8) can only have abstract methods. A class can implement multiple interfaces but can only extend one abstract class.

4
New cards

final keyword

The final keyword prevents a class from being subclassed (inherited).

5
New cards

Method overriding

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass, using the same method signature.

6
New cards

Default access modifier

The default (package-private) access modifier, which is applied when no modifier is specified, makes a member accessible only within the same package.

7
New cards

super keyword

The super keyword is used to refer to the immediate parent class's constructor, methods, or fields from a subclass.

8
New cards

Abstract and final class

No, a class cannot be both abstract and final. Abstract requires the class to be extended, while final prevents extension.

9
New cards

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass, often achieved through method overriding or overloading.

10
New cards

this keyword

The this keyword refers to the current object instance and is often used to distinguish instance variables from local variables or to call another constructor in the same class.

11
New cards

Class in OOP

A blueprint for creating objects, defining attributes (fields) and behaviors (methods).

12
New cards

Abstraction

Showing only essential features while hiding the complex implementation.

13
New cards

Inheritance in OOP

When a class (subclass) inherits fields and methods from another class (superclass).

14
New cards

Access modifiers in Java

public (accessible everywhere), protected (accessible in same package and subclasses), private (accessible only within the class).

15
New cards

Composition vs Aggregation

Composition has strong ownership (lifespan linked), aggregation is weak ownership (independent lifespan).

16
New cards

Java Virtual Machine (JVM)

It executes bytecode and ensures Java's platform independence.

17
New cards

JDK vs JRE

JDK is for development (includes compiler), JRE is for running programs (includes JVM and libraries).

18
New cards

main method in Java

It's the entry point of a Java program.

19
New cards

new keyword in Java

It creates new objects and allocates memory on the heap.

20
New cards

Stack vs Heap memory

Stack stores method calls and local variables; heap stores objects and is managed by the garbage collector.

21
New cards

Wrapper classes in Java

To allow primitive types to be used in collections like ArrayList.

22
New cards

Strong typing in Java

Variables must be declared with a type, reducing errors and increasing reliability.

23
New cards

Multiple classes in one Java file

Yes, but only one can be public and the file must be named after it.

24
New cards

Fridge f = new Fridge(50);

It creates a new Fridge object with an initial value of 50 and assigns its reference to variable f.

25
New cards

Primitive types passed in Java

Yes, changes to them inside a method do not affect the original variable.

26
New cards

Reference types passed in Java

By value of the reference — the reference is copied, but both point to the same object.

27
New cards

Shallow copy

A copy of the object reference — changes to one affect the other.

28
New cards

Deep copy

A new object with duplicated fields — changes don't affect the original.

29
New cards

Constructor overloading

Defining multiple constructors with different parameters to offer flexible object initialization.

30
New cards

Singleton Pattern

A design pattern ensuring only one instance of a class exists globally.

31
New cards

Method overloading in Java

Method overloading allows multiple methods with the same name but different parameters (number or type) in the same class.

32
New cards

Static variable in Java

A static variable belongs to the class, not instances, is shared across all objects, and is stored in class memory.

33
New cards

Constructor chaining

Constructor chaining is calling one constructor from another within the same class or across a class hierarchy.

34
New cards

Single inheritance in Java

Single inheritance is when a child class inherits from one parent class, gaining access to its non-private fields and methods.

35
New cards

Multilevel inheritance

Multilevel inheritance occurs when a class inherits from a child class, which itself extends another class.

36
New cards

Hierarchical inheritance

Hierarchical inheritance is when multiple child classes inherit from a single parent class.

37
New cards

Java and multiple inheritance with classes

Java avoids multiple inheritance with classes to prevent ambiguity (e.g., the Diamond Problem).

38
New cards

Java support for multiple inheritance

Java supports multiple inheritance through interfaces, where a class can implement multiple interfaces.

39
New cards

Method overriding in Java

Method overriding allows a child class to provide a specific implementation of a method already defined in its parent class.

40
New cards

Abstract class in Java

An abstract class cannot be instantiated, can have both abstract and concrete methods.

41
New cards

Interface in Java

An interface is a blueprint with abstract methods and constant variables, defining a contract that implementing classes must follow.

42
New cards

Default methods in interfaces (Java 8+)

Default methods in interfaces provide a default implementation, allowing classes to use them without overriding.

43
New cards

abstract class

Use an abstract class for shared fields or partial implementation.

44
New cards

interface

Use an interface for defining a contract or supporting multiple inheritance.

45
New cards

Mediator design pattern

The Mediator pattern reduces direct communication between objects, forcing them to collaborate via a mediator object, promoting loose coupling.

46
New cards

type promotion in Java method overloading

Type promotion occurs when Java converts a smaller data type (e.g., byte) to a larger one (e.g., int) to match a method parameter when no exact match is found.

47
New cards

static block in Java

A static block executes once when a class is loaded, before the main method, and is used for one-time initialization (e.g., setting up constants).

48
New cards

difference between abstract and concrete methods

Abstract methods have no implementation and must be overridden; concrete methods have a body and can be used or overridden optionally.

49
New cards

super keyword in inheritance

The super keyword calls the parent class's method or constructor, used in method overriding or constructor chaining.

50
New cards

behavior contract in Java

A behavior contract, defined by an interface, is a set of rules (methods) that implementing classes must follow, ensuring consistent behavior.

51
New cards

Diamond Problem in multiple inheritance

The Diamond Problem occurs when a class inherits from two classes that have a common ancestor, causing ambiguity about which method to use.

52
New cards

constructor chaining in Java

Constructor chaining in Java allows multiple constructors to call a main constructor to avoid code duplication. It uses this() to invoke another constructor within the same class, ensuring consistent initialization logic.

53
New cards

ternary operator in Java

The ternary operator (?:) is a compact if-else alternative. Syntax: variable = (condition) ? value_if_true : value_if_false. Example: String result = (num > 0) ? "Positive" : "Non-positive".

54
New cards

enhanced for loop in Java

The enhanced for loop syntax is: for (Type variable : collection) { ... }. It iterates over arrays or collections. Example: for (String car : cars) { System.out.println(car); }.

55
New cards

UML in software development

UML (Unified Modeling Language) is used for planning, visualizing, and documenting object-oriented systems. It aids in system design, software documentation, and team collaboration.

56
New cards

types of UML diagrams

1. Class Diagram (Structural): Describes classes and their relationships. 2. Use Case Diagram (Behavioral): Shows system interactions with users or external systems.

57
New cards

class attributes in UML class diagram

Class attributes in UML are written as attributeName: attributeType, with visibility indicated (e.g., + for public, - for private). Example: -name: String.

58
New cards

constructor in UML class diagram

A constructor in UML is written as constructorName(parameterName: parameterType). Example: Person(initialName: String).

59
New cards

arrow in UML class diagram

An arrow in a UML class diagram shows the direction of a relationship. For example, a Book class with an arrow to Person indicates Book knows its author, but Person does not know about Book.

60
New cards

inheritance in UML class diagram

Inheritance is shown with an arrow with a triangle head pointing to the parent class. Example: Engine inherits from Part.

61
New cards

How are interfaces denoted in a UML class diagram?

Interfaces are written with <> above the interface name. Example: <> Subscriber.

62
New cards

What is the Observer design pattern?

The Observer pattern is a behavioral design pattern where a Publisher (Subject) notifies multiple Subscribers about state changes, allowing dynamic, one-way communication.

63
New cards

What problem does the Observer pattern solve?

It solves the issue of notifying dependent objects efficiently without wasteful polling (e.g., customers checking a store) or spamming uninterested parties (e.g., mass emails).

64
New cards

How does the Observer pattern achieve loose coupling?

Subscribers implement a common interface (e.g., with an update() method), allowing the Publisher to notify them uniformly without knowing their internal details.

65
New cards

What are the main components of the Observer pattern?

1. Publisher: Maintains a list of subscribers and notifies them of changes. 2. Subscriber Interface: Declares the update() method. 3. Concrete Subscribers: Implement the interface to react to notifications.

66
New cards

When should you use the Observer pattern?

Use it for dynamic dependencies (unknown or changing subscribers), GUI event handling (e.g., custom button actions), or when a flexible subscription mechanism is needed.

67
New cards

What is a key difference between the Observer and Mediator patterns?

Observer: Direct communication from Publisher to Subscribers. Mediator: Indirect communication through a central coordinator, reducing direct dependencies.

68
New cards

What is a pro of the Observer pattern?

It allows introducing new subscriber classes without changing the publisher's code, supporting loose coupling and flexibility.

69
New cards

What is a con of the Observer pattern?

Subscribers are notified in random order, which may lead to unpredictable behavior in some cases.

70
New cards

What is the difference between List and ArrayList in Java?

List is an interface defining an ordered collection with methods for adding, removing, and accessing elements. ArrayList is a concrete implementation of List, using a dynamic array for storage, offering fast random access (O(1)) but slower insertions/removals (O(n)).

71
New cards

What are the characteristics of an outer class in Java?

An outer class is a top-level class that can contain variables and methods, can be instantiated directly, can be public or package-private, and cannot be static, as it is not nested.

72
New cards

What is an inner class, and what are its key characteristics?

An inner class is a class defined inside another class. It has access to the outer class's variables and methods, requires an instance of the outer class to be created, is used for logically grouping related code, and is relevant only within the context of the outer class.

73
New cards

How does a static inner class differ from a non-static inner class?

A static inner class does not require an instance of the outer class to be created, cannot access non-static members of the outer class, and behaves like an independent class and is instantiated using Outer.StaticInner.

74
New cards

What is loose coupling, and why is it important?

Loose coupling is a design principle that minimizes dependencies between components by relying on abstractions (e.g., interfaces) rather than specific implementations. It enhances flexibility, scalability, maintainability, reusability, and testability.

75
New cards

What is an example of tight coupling, and why is it problematic?

In tight coupling, a class like OrderProcessor directly depends on a concrete class like PayPalPayment.

76
New cards

Loose Coupling

By depending on a Payment interface instead of a concrete class, OrderProcessor can work with any payment type (e.g., PayPalPayment, CreditCardPayment). This allows easy switching or addition of payment methods without modifying existing code.

77
New cards

Factory Method Pattern

The Factory Method is a creational pattern that defines an interface for creating objects in a superclass but allows subclasses to alter the type of objects created. It separates object creation from usage, promoting flexibility and scalability.

78
New cards

Factory Method Benefits

The Factory Method avoids tight coupling between creator and concrete products, follows the Single Responsibility Principle by isolating creation logic, and supports the Open/Closed Principle by allowing new product types without modifying existing code.

79
New cards

When to Use Factory Method

Use the Factory Method when you want to reuse existing objects to save resources, need to extend object creation logic independently from the code that uses the objects, or want to introduce new product types without breaking existing client code.

80
New cards

Factory Method Structure

The Factory Method includes: Product: An interface/abstract class defining the common interface; Concrete Products: Implementations of the product; Creator: An abstract class declaring the factory method; Concrete Creators: Subclasses that override the factory method to create specific products.

81
New cards

Open/Closed Principle

The Open/Closed Principle states that software entities should be open for extension but closed for modification. The Factory Method supports this by allowing new product types to be added via new creator subclasses without altering existing client or creator code.

82
New cards

Exception in Java

An event that disrupts the normal flow of a program, typically caused by unexpected runtime situations.

83
New cards

Common Causes of Exceptions

Invalid user input, null references, arithmetic errors.

84
New cards

Difference between Error and Exception

Errors are serious and often unrecoverable (e.g., OutOfMemory), while Exceptions are recoverable and can be handled in code.

85
New cards

Checked Exception

Exceptions checked at compile time that must be handled or declared (e.g., IOException).

86
New cards

Unchecked Exception

Exceptions not checked at compile time, such as NullPointerException or ArrayIndexOutOfBoundsException.

87
New cards

Finally Block

Executes code regardless of whether an exception occurred, usually for cleanup.

88
New cards

Throws Keyword

When a method might throw a checked exception that it doesn't handle itself.

89
New cards

Generics in Java

A way to create classes, interfaces, and methods that operate on typed parameters.

90
New cards

Syntax of a Generic Class

class Box { private T value; ... }

91
New cards

Wildcard ? in Generics

Represents an unknown type, useful for reading from collections.

92
New cards

Difference between ? extends T and ? super T

? extends T means subtype of T; ? super T means supertype of T.

93
New cards

Java Generics and Primitives

No, only reference types. Use wrappers like Integer for int.

94
New cards

Type Erasure in Java

Generic type information is removed at runtime.

95
New cards

Decorator Pattern

A structural pattern that adds new behaviors to objects dynamically by wrapping them.

96
New cards

Inheritance vs Decorator

Inheritance is static and doesn't allow behavior modification at runtime.

97
New cards

Key components in Decorator pattern

Component Interface, Concrete Component, Base Decorator, Concrete Decorators.

98
New cards

Benefit of Decorator pattern

Allows dynamic composition of responsibilities without subclass explosion.

99
New cards

Decorator vs Adapter

Decorator adds behavior; Adapter changes interface for compatibility.

100
New cards

Template Method pattern

A behavioral design pattern that defines the skeleton of an algorithm in a superclass but lets subclasses override specific steps.