Java Exam Notes: Classes, Methods, Inheritance, and Interfaces
Chapter 6 and Chapter 7: Methods and Classes
--------------------------------------------
1. Overloading Methods:
- Define multiple methods with the same name but different parameter lists.
- Example: A method `test` with overloads for int, double, and no parameters.
2. Objects as Parameters:
- Pass objects to methods to access their properties and methods.
- Example: Comparing two objects for equality using a method.
3. Argument Passing:
- Java uses call-by-value for primitive types.
- For objects, references are passed, so changes affect the original object.
4. Returning Objects:
- Methods can return objects, e.g., a method incrementing an object's value.
5. Recursion:
- Methods can call themselves. Example: Calculating factorial recursively.
6. Access Control:
- Keywords: public, private, protected, and package-private.
- Encapsulation ensures controlled access to class members.
7. Static Members:
- Declared using `static` keyword.
- Shared among all objects of a class.
8. Final Keyword:
- Used to declare constants, prevent inheritance, and prevent method overriding.
9. Nested and Inner Classes:
- Nested: Defined inside another class; useful for logical grouping.
- Inner: Associated with an instance of the enclosing class.
Chapter 8: Inheritance
----------------------
1. Inheritance Basics:
- Enables creating new classes from existing ones (reuse and extension).
2. Using super:
- Access parent class methods or constructors.
3. Multilevel Hierarchy:
- Inherit from a class that itself inherits another class.
4. Constructor Execution:
- Parent class constructors are called before child class constructors.
5. Method Overriding:
- Redefine a method in a subclass for specific behavior.
6. Dynamic Method Dispatch:
- Determines the method to call at runtime based on the object type.
7. Abstract Classes:
- Declared using `abstract`; cannot be instantiated.
8. Final with Inheritance:
- Prevent inheritance by marking classes or methods as final.
9. Object Class:
- The root of all Java classes; provides basic methods like `toString`, `equals`.
Chapter 9: Interfaces
---------------------
1. Interfaces:
- Define a contract that implementing classes must follow.
2. Default Methods:
- Introduced in Java 8; methods with default implementation in interfaces.
3. Static Methods in Interfaces:
- Can be called without an instance of the implementing class.
4. Private Methods in Interfaces:
- Used to share code between default and static methods in an interface.