Calling Subclass Methods

Calling Subclass Methods

When a class is instantiated, the subsequent object is actually an instance of the most derived class in the class hierarchy. This means that even if a class is referenced by a superclass reference, it should still have the behavior and state of the subclass available to it at run time. For example, consider this code:

 1.    public class Base {2.    }3. 4.    public class Derived extends Base {5.      public String getSquare () {6.        return "Result";7.      }8.    }

The method getResult() is defined in the derived class. However, suppose that you had a reference to an object of type Base whose runtime class was Derived:

 Base myObject = new Derived();

The method getResult() can be called using the reference myObject, but not directly. If you try to execute myObject.getResult(), you will get an exception since myMethod() does not exist in the class Base. However, Java’s reflection mechanism lets you call the subclass method:

 1.   Base b = new Derived();2. 3. // Call the method on the derived class4. Class baseClass = b.getClass();5. Method m = baseClass.getMethod("getResult", new Class {});6. String result = baseClass.invoke(m, new Object[] {});7. System.out.println(result);

The subclass is instantiated on Line 1. On Line 4, the runtime class of the object b is obtained, and is used to get the “getResult()” method on Line 5. Finally, the method is invoked and printed out on Lines 6-7.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing