1/157
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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.
extends keyword
The extends keyword is used to inherit a class in Java.
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.
final keyword
The final keyword prevents a class from being subclassed (inherited).
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.
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.
super keyword
The super keyword is used to refer to the immediate parent class's constructor, methods, or fields from a subclass.
Abstract and final class
No, a class cannot be both abstract and final. Abstract requires the class to be extended, while final prevents extension.
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass, often achieved through method overriding or overloading.
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.
Class in OOP
A blueprint for creating objects, defining attributes (fields) and behaviors (methods).
Abstraction
Showing only essential features while hiding the complex implementation.
Inheritance in OOP
When a class (subclass) inherits fields and methods from another class (superclass).
Access modifiers in Java
public (accessible everywhere), protected (accessible in same package and subclasses), private (accessible only within the class).
Composition vs Aggregation
Composition has strong ownership (lifespan linked), aggregation is weak ownership (independent lifespan).
Java Virtual Machine (JVM)
It executes bytecode and ensures Java's platform independence.
JDK vs JRE
JDK is for development (includes compiler), JRE is for running programs (includes JVM and libraries).
main method in Java
It's the entry point of a Java program.
new keyword in Java
It creates new objects and allocates memory on the heap.
Stack vs Heap memory
Stack stores method calls and local variables; heap stores objects and is managed by the garbage collector.
Wrapper classes in Java
To allow primitive types to be used in collections like ArrayList.
Strong typing in Java
Variables must be declared with a type, reducing errors and increasing reliability.
Multiple classes in one Java file
Yes, but only one can be public and the file must be named after it.
Fridge f = new Fridge(50);
It creates a new Fridge object with an initial value of 50 and assigns its reference to variable f.
Primitive types passed in Java
Yes, changes to them inside a method do not affect the original variable.
Reference types passed in Java
By value of the reference — the reference is copied, but both point to the same object.
Shallow copy
A copy of the object reference — changes to one affect the other.
Deep copy
A new object with duplicated fields — changes don't affect the original.
Constructor overloading
Defining multiple constructors with different parameters to offer flexible object initialization.
Singleton Pattern
A design pattern ensuring only one instance of a class exists globally.
Method overloading in Java
Method overloading allows multiple methods with the same name but different parameters (number or type) in the same class.
Static variable in Java
A static variable belongs to the class, not instances, is shared across all objects, and is stored in class memory.
Constructor chaining
Constructor chaining is calling one constructor from another within the same class or across a class hierarchy.
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.
Multilevel inheritance
Multilevel inheritance occurs when a class inherits from a child class, which itself extends another class.
Hierarchical inheritance
Hierarchical inheritance is when multiple child classes inherit from a single parent class.
Java and multiple inheritance with classes
Java avoids multiple inheritance with classes to prevent ambiguity (e.g., the Diamond Problem).
Java support for multiple inheritance
Java supports multiple inheritance through interfaces, where a class can implement multiple interfaces.
Method overriding in Java
Method overriding allows a child class to provide a specific implementation of a method already defined in its parent class.
Abstract class in Java
An abstract class cannot be instantiated, can have both abstract and concrete methods.
Interface in Java
An interface is a blueprint with abstract methods and constant variables, defining a contract that implementing classes must follow.
Default methods in interfaces (Java 8+)
Default methods in interfaces provide a default implementation, allowing classes to use them without overriding.
abstract class
Use an abstract class for shared fields or partial implementation.
interface
Use an interface for defining a contract or supporting multiple inheritance.
Mediator design pattern
The Mediator pattern reduces direct communication between objects, forcing them to collaborate via a mediator object, promoting loose coupling.
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.
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).
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.
super keyword in inheritance
The super keyword calls the parent class's method or constructor, used in method overriding or constructor chaining.
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.
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.
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.
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".
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); }.
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.
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.
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.
constructor in UML class diagram
A constructor in UML is written as constructorName(parameterName: parameterType). Example: Person(initialName: String).
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.
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.
How are interfaces denoted in a UML class diagram?
Interfaces are written with <
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.
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).
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.
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.
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.
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.
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.
What is a con of the Observer pattern?
Subscribers are notified in random order, which may lead to unpredictable behavior in some cases.
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)).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Exception in Java
An event that disrupts the normal flow of a program, typically caused by unexpected runtime situations.
Common Causes of Exceptions
Invalid user input, null references, arithmetic errors.
Difference between Error and Exception
Errors are serious and often unrecoverable (e.g., OutOfMemory), while Exceptions are recoverable and can be handled in code.
Checked Exception
Exceptions checked at compile time that must be handled or declared (e.g., IOException).
Unchecked Exception
Exceptions not checked at compile time, such as NullPointerException or ArrayIndexOutOfBoundsException.
Finally Block
Executes code regardless of whether an exception occurred, usually for cleanup.
Throws Keyword
When a method might throw a checked exception that it doesn't handle itself.
Generics in Java
A way to create classes, interfaces, and methods that operate on typed parameters.
Syntax of a Generic Class
class Box
Wildcard ? in Generics
Represents an unknown type, useful for reading from collections.
Difference between ? extends T and ? super T
? extends T means subtype of T; ? super T means supertype of T.
Java Generics and Primitives
No, only reference types. Use wrappers like Integer for int.
Type Erasure in Java
Generic type information is removed at runtime.
Decorator Pattern
A structural pattern that adds new behaviors to objects dynamically by wrapping them.
Inheritance vs Decorator
Inheritance is static and doesn't allow behavior modification at runtime.
Key components in Decorator pattern
Component Interface, Concrete Component, Base Decorator, Concrete Decorators.
Benefit of Decorator pattern
Allows dynamic composition of responsibilities without subclass explosion.
Decorator vs Adapter
Decorator adds behavior; Adapter changes interface for compatibility.
Template Method pattern
A behavioral design pattern that defines the skeleton of an algorithm in a superclass but lets subclasses override specific steps.