Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Polymorphism and Related Concepts in Java

Tech 1

Polymorphism in Java manifests as object polymorphism and behavior polymorphism, occurring under inheritance or implementation relationships.

package polymorphism;

class Person {
    public void move() {
        System.out.println("Person moves.");
    }
}

class CollegeStudent extends Person {
    @Override
    public void move() {
        System.out.println("College student moves quickly.");
    }
}

class Instructor extends Person {
    @Override
    public void move() {
        System.out.println("Instructor moves with effort.");
    }
}

class PolymorphismDemo {
    public static void main(String[] args) {
        Person p1 = new Instructor();
        p1.move(); // Output: Instructor moves with effort.

        Person p2 = new CollegeStudent();
        p2.move(); // Output: College student moves quickly.
    }
}

Polymorphism requires: inheritance/implementation, a parent class reference holding a subclass object, and method overriding. Note that Java’s member variables (attributes) do not exhibit polymorphism—only objects and behaviors do.

Benefits of polymorphism: The right - side object in polymorphic form is decoupled, enabling easier extension and maintenance. Using a parent class type as a method parameter allows accepting any subclass object, enhancing extensibility.

Final Keyword

The final keyword (meaning "final" or "unchangeable") can modify classes, methods, or variables:

  • Class: A final class cannot be inherited.
  • Method: A final method cannot be overridden.
  • Variable: A final variable can only be assigned once.

Constents

A member variable with static final is a constant, used for system configuration:

class Config {
    public static final String ORGANIZATION_NAME = "Chuanshu Education";
}

Constant naming: Use uppercase with underscores (e.g., ORGANIZATION_NAME). At compile time, constants are "macro - replaced" (literal values replace references), ensuring optimal performance.

Abstract Class

The abstract keyword modifies classes (abstract classes) or methods (abstract methods, no body):

abstract class AbstractExample {
    public abstract void performAction();
}

Abstract methods require subclasses to provide implementations (unless the subclass is also abstract).

Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.