Monday, July 21, 2025

Java Inheritance

# Understanding Java Keywords: `public`, `void`, `class`, `super`, and `final`

These are all fundamental Java keywords that serve different purposes in the language. Let me explain each one clearly with examples:

## 1. `public` - Access Modifier
Controls the visibility/accessibility of classes, methods, and variables.

```java
public class MyClass {       // Class accessible from anywhere
    public int myVariable;   // Variable accessible from anywhere
    
    public void myMethod() { // Method accessible from anywhere
        System.out.println("Public method");
    }
}
```

## 2. `void` - Return Type
Indicates a method doesn't return any value.

```java
public class Calculator {
    // This method performs an action but returns nothing
    public void printSum(int a, int b) {
        System.out.println("Sum: " + (a + b));
    }
    
    // Contrast with a method that returns a value
    public int calculateSum(int a, int b) {
        return a + b;
    }
}
```

## 3. `class` - Class Declaration
Used to define a new class (blueprint for objects).

```java
class Animal {          // Class declaration
    String name;        // Field
    
    void makeSound() {  // Method
        System.out.println("Some sound");
    }
}
```

## 4. `super` - Parent Class Reference
Used to access parent class members (fields, methods, constructors).

```java
class Vehicle {
    int maxSpeed = 120;
    
    void display() {
        System.out.println("Vehicle display");
    }
}

class Car extends Vehicle {
    int maxSpeed = 180;
    
    void display() {
        super.display(); // Calls parent's display()
        System.out.println("Car max speed: " + maxSpeed);
        System.out.println("Vehicle max speed: " + super.maxSpeed);
    }
    
    Car() {
        super(); // Calls parent's constructor (implicit if not written)
    }
}
```

## 5. `final` - Non-modifiable Declaration
Makes variables constant, methods non-overridable, or classes non-inheritable.

```java
final class FinalClass {    // Cannot be extended
    final int MAX_VALUE = 100; // Constant variable
    
    final void display() {  // Cannot be overridden
        System.out.println("This is final");
    }
}

// This would cause a compile error:
// class ChildClass extends FinalClass {}
```

## Combined Usage Example

Here's an example showing all these keywords working together:

```java
public class Parent {          // public class
    final String FAMILY_NAME = "Smith"; // final variable
    
    public void display() {    // public void method
        System.out.println("Family: " + FAMILY_NAME);
    }
}

public class Child extends Parent {
    private String firstName;
    
    public Child(String name) { // public constructor
        this.firstName = name;
    }
    
    @Override
    public void display() {    // overriding parent's method
        super.display();       // using super to call parent's method
        System.out.println("First name: " + firstName);
    }
    
    // This would cause error because display() is final in Parent
    // public final void display() {}
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child("John");
        child.display();
    }
}
```

## Key Differences Summary

| Keyword | Purpose | Example Use |
|---------|---------|-------------|
| `public` | Makes class/member accessible from anywhere | `public class MyClass` |
| `void`   | Indicates no return value | `public void doSomething()` |
| `class`  | Declares a new class | `class MyClass` |
| `super`  | Refers to parent class | `super.methodCall()` |
| `final`  | Makes something unchangeable | `final int MAX = 100` |

These keywords are fundamental to Java programming and understanding them is crucial for writing proper object-oriented code.

No comments: