Untitled Flashcards Set

Here are study notes for your midterm exam covering the given concepts:


Midterm Study Notes

1. Interface

  • A blueprint of a class that contains abstract methods (methods without implementation).

  • A class implements an interface using the implements keyword.

  • Multiple interfaces can be implemented by a single class.

  • Example:

    interface Animal {
    void makeSound(); // Abstract method
    }

    class Dog implements Animal {
    public void makeSound() {
    System.out.println("Woof!");
    }
    }

2. Extends

  • Used for inheritance in Java.

  • A subclass (child class) inherits fields and methods from a superclass (parent class).

  • Uses the extends keyword.

  • Example:

    class Animal {
    void eat() {
    System.out.println("Eating...");
    }
    }

    class Dog extends Animal {
    void bark() {
    System.out.println("Barking...");
    }
    }

3. Exception

  • An error-handling mechanism in Java.

  • try, catch, and finally blocks are used to handle exceptions.

  • throw and throws are used for manual exception handling.

  • Example:

    try {
    int result = 10 / 0; // Causes ArithmeticException
    } catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero!");
    }

4. This

  • Refers to the current instance of the class.

  • Used to differentiate instance variables from parameters when they have the same name.

  • Can be used to call other constructors (this()).

  • Example:

    class Example {
    int x;
    Example(int x) {
    this.x = x; // "this" refers to the instance variable
    }
    }

5. Super

  • Refers to the parent class (superclass).

  • Used to call the parent class constructor or methods.

  • Example:

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

    class Dog extends Animal {
    void makeSound() {
    super.makeSound(); // Calls method from Animal class
    System.out.println("Dog barks");
    }
    }

6. ADT (Abstract Data Type)

  • A concept, not a specific implementation.

  • Defines a data structure by specifying operations without detailing how they are implemented.

  • Examples: List, Stack, Queue, Map

  • Java implementations: ArrayList, LinkedList, HashMap, etc.

7. Encapsulation

  • The principle of data hiding and protecting fields from direct access.

  • Uses private variables and getter/setter methods.

  • Example:

    class Person {
    private String name; // Encapsulation

    public void setName(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }
    }

8. Wrapper Class

  • Converts primitive types into objects.

  • Common wrapper classes: Integer, Double, Boolean, etc.

  • Example:

    int x = 5;
    Integer y = Integer.valueOf(x); // Boxing
    int z = y.intValue(); // Unboxing

9. Serializable and UID

  • Serializable allows objects to be converted into bytes for file storage or network transfer.

  • UID (Unique Identifier) helps with object version control.

  • Example:

    import java.io.Serializable;

    class Example implements Serializable {
    private static final long serialVersionUID = 1L; // Unique ID
    private String name;
    }

10. Immutable Types

  • Objects whose values cannot be changed after creation.

  • Examples: String, Integer, Double.

  • Example:

    String s = "Hello";
    s = s.concat(" World"); // Creates a new object, does not modify original

11. Constructor Chaining

  • Calling one constructor from another within the same class or superclass.

  • Uses this() for same class, super() for parent class.

  • Example:

    class Example {
    Example() {
    this(10); // Calls parameterized constructor
    }

    Example(int x) {
    System.out.println("Value: " + x);
    }
    }

12. Method Overloading

  • Multiple methods with the same name but different parameters.

  • Improves readability and supports polymorphism.

  • Example:

    class MathOperations {
    int add(int a, int b) {
    return a + b;
    }

    double add(double a, double b) {
    return a + b;
    }
    }

These notes cover definitions, explanations, and examples to help you prepare for your midterm. Let me know if you need further clarifications! 🚀

robot