Skip to content

Patika-Getir-Java-BootCamp/hw-1-zeynepucuncuoglu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Review Assignment Due Date

HW#1

1 – Why Do We Need to Use OOP? Major OOP Languages?

Why?

  • Encapsulation: Bundles data and methods together.
  • Abstraction: Hides complex implementation.
  • Inheritance: Promotes code reusability.
  • Polymorphism: Allows flexibility in method calls.

Major OOP Languages

Java, C++, Python, C#, Swift, Kotlin.


2 – Interface vs Abstract Class

Feature Interface Abstract Class
Methods Only abstract methods (Java 7). Default/static methods allowed (Java 8+). Can have both abstract & concrete methods.
Fields Only constants (public, static, final). Can have instance variables.
Constructor No constructor. Can have constructors.
Usage Used for capability-based design (e.g., Runnable). Used for common base functionality (e.g., Animal).
Inheritance A class can implement multiple interfaces. A class can extend only one abstract class.

3 – Why Do We Need equals() and hashCode()? When to Override?

  • equals(): Used to compare object contents, not memory reference.
  • hashCode(): Used for efficient storage in hash-based collections (e.g., HashMap).
  • Override When? When using objects as keys in hash-based collections or defining custom equality logic.

4 – Diamond Problem in Java? How to Fix It?

Problem

  • Occurs in multiple inheritance when a class inherits from two classes/interfaces with the same method signature.

Solution

  • Java prevents multiple class inheritance.
  • Use interfaces: Java 8 allows default methods, but if a conflict occurs, the subclass must override it.

5 – Why Do We Need Garbage Collector? How Does It Run?

Why?

  • Frees memory by removing unused objects to prevent memory leaks.

How?

  • Runs automatically in JVM. Uses algorithms like Mark and Sweep, Generational GC, G1 GC.

6 – Java static Keyword Usage

  • Variable: Shared across all instances (static int count).
  • Method: Can be called without an object (Math.sqrt()).
  • Block: Runs once when class is loaded (static {} block).
  • Nested Class: Can exist without an outer class instance (static class Helper).

7 – What Is Immutability? Why and Where to Use It?

Definition

  • Object state cannot be changed after creation.

Why?

  • Thread safety, caching, and reducing unintended side effects.

How?

  • Declare fields private final, remove setters, and return new objects instead of modifying existing ones.

Where?

  • String, Wrapper classes (Integer, Double), java.time API.

8 – Composition vs Aggregation

Feature Composition Aggregation
Definition Strong association (part cannot exist without whole). Weak association (part can exist independently).
Example Car and Engine (engine is destroyed if car is destroyed). Department and Professor (professor can exist without department).

9 – Cohesion vs Coupling

Feature Cohesion Coupling
Definition How strongly related methods & data are within a class. Degree of dependency between modules/classes.
Good Practice High cohesion is good (single responsibility). Low coupling is good (independent components).

10 – Heap vs Stack

Feature Heap Stack
Usage Stores objects, dynamically allocated memory. Stores method calls, local variables, function execution context.
Access Speed Slower Faster
Lifespan Exists till GC removes it. Exists until method execution completes.

11 – Exception and Types of Exceptions

Definition

Unwanted events that disrupt program execution.

Types

  1. Checked Exception – Must be handled (IOException, SQLException).
  2. Unchecked Exception – Runtime errors (NullPointerException, ArithmeticException).
  3. Error – System-level issue (OutOfMemoryError, StackOverflowError).

12 – Clean Code Summary

Simple, readable, maintainable, and efficient.

  • Meaningful names.
  • Small functions.
  • Follows SOLID principles.
  • No unnecessary complexity.

13 – What Is Method Hiding in Java?

  • If a subclass defines a static method with the same signature as a static method in the parent class, the subclass method hides the superclass method instead of overriding it.
class Parent { 
    static void display() { 
        System.out.println("Parent"); 
    } 
}

class Child extends Parent { 
    static void display() { 
        System.out.println("Child"); 
    } 
}

14 What is the difference between abstraction and polymorphism in Java ?

1. Abstraction

Definition:
Abstraction is the process of hiding implementation details and showing only the essential features of an object.

Key Points:

  • Focuses on what an object does rather than how it does it.
  • Achieved using abstract classes and interfaces.
  • Helps in reducing complexity and increasing reusability.

Example in Java:

abstract class Animal {
    abstract void makeSound(); // Abstract method (no implementation)
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Bark");
    }
}

2. Polymorphism

Definition:
Polymorphism allows objects of different classes to be treated as objects of a common super class. It enables a single interface to represent different data types.

Key Points:

  • Two types: Compile-time (method overloading) and Run-time (method overriding).
  • Improves code flexibility and reusability.
  • Supports dynamic method dispatch.

Example in Java:

class Animal {
    void makeSound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Runtime Polymorphism
        myAnimal.makeSound(); // Output: Bark
    }
}

Key Differences

Feature Abstraction Polymorphism
Definition Hiding implementation details Multiple forms of the same method
Purpose Reduce complexity Increase flexibility
Achieved by Abstract classes & interfaces Method overloading & overriding
Example Abstract makeSound() method in Animal makeSound() behaves differently in Dog

About

hw-1-zeynepucuncuoglu created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •