package com.mtitek.inheritance; public class SuperClass { protected Integer addOperation(Integer value1, Integer value2) { return value1 + value2; } protected Integer multiplyOperation(Integer value1, Integer value2) { return value1 * value2; } }
package com.mtitek.inheritance; public class SubClass extends SuperClass { @Override public Integer multiplyOperation(Integer value1, Integer value2) { return (value1 * value2) * 10; } // new method: not defined in the parent class public Integer subtractOperation(Integer value1, Integer value2) { return (value1 - value2) * 10; } }In the example above, the class
SubClass
inherits from the class SuperClass
.SuperClass
and can use them as if they were defined in SubClass
.multiplyOperation
).@Override
annotation is recommended for overridden methods to ensure compile-time checking.SubClass
after inheritance:SubClass (effective methods): protected Integer [inherited from SuperClass].addOperation(Integer value1, Integer value2) { ... } protected Integer [inherited from SuperClass].multiplyOperation(Integer value1, Integer value2) { ... } public Integer [overridden in SubClass].multiplyOperation(Integer value1, Integer value2) { ... } public Integer [defined in SubClass].subtractOperation(Integer value1, Integer value2) { ... }Overridden methods from the superclass are still accessible using the
super
keyword from within the subclass.protected
and the subclass is in a different package,
the inherited method can only be accessed within the subclass itself or its subclasses.
It cannot be directly invoked on subclass instances from outside the inheritance hierarchy
(e.g., subClassInstance.protectedMethod()
from an unrelated class in a different package).public
method as protected
).
public class TestClass { SuperClass superClassRef; // Can reference SuperClass or any of its subclasses SubClass subClassRef; // Can only reference SubClass or its subclasses }In the example above,
superClassRef
can reference instances of SuperClass
or SubClass
(polymorphism).subClassRef
can only reference instances of SubClass
or its subclasses.public class TestClass { public static void main(String[] args) { SuperClass superClass1 = new SuperClass(); // OK: Same type SuperClass superClass2 = new SubClass(); // OK: SubClass IS-A SuperClass SubClass subClass1 = new SubClass(); // OK: Same type SubClass subClass2 = new SuperClass(); // Compiler error: Type mismatch: cannot convert from SuperClass to SubClass SubClass subClass3 = (SubClass) new SuperClass(); // Runtime error: ClassCastException: class SuperClass cannot be cast to class SubClass } }
ArrayStoreException
when storing incompatible objects.class Parent { } class Child extends Parent { public void childMethod() { } }
Child[] childArray = new Child[1]; Parent[] parentArray = childArray; // Legal assignment (covariance) parentArray[0] = new Parent(); // Runtime error: ArrayStoreExceptionThis exception is Java's runtime safety mechanism to prevent
ClassCastException
when accessing array elements.childArray[0].childMethod()
- calling a method that doesn't exist in the Parent class.targetVar = sourceVar;
),
the compiler verifies that the target variable's type is the same as or a superclass of the source variable's type.package com.mtitek.inheritance; public class TestClass { public static void main(String[] args) { TestClass testClass = new TestClass(); TestClass testClass1 = testClass; // OK: Same type Object obj = testClass; // OK: Object is superclass of TestClass (upcasting) TestClass testClass2 = obj; // Compiler error (implicit downcasting not allowed, requires explicit cast): Type mismatch: cannot convert from Object to TestClass String str = obj; // Compiler error: Type mismatch: cannot convert from Object to String } }
Object obj1 = new String("Hello"); // Implicit upcasting Object obj2 = (Object) new String("Java"); // Explicit upcasting - unnecessary but validUpcasting is always safe because a subclass instance IS-A superclass instance. The compiler accepts it, and no runtime exceptions occur. However, you can only access methods and fields defined in the superclass through the upcasted reference.
Object obj = "Hello"; // String object referenced by Object variable String str1 = obj; // Compiler error (implicit downcasting not allowed): Type mismatch: cannot convert from Object to String String str2 = (String) obj; // OK (explicit downcasting): succeeds at runtime Object obj2 = new Integer(42); String str3 = (String) obj2; // Runtime error: ClassCastException: class Integer cannot be cast to class StringImplicit downcasting is always rejected by the compiler for safety.
ClassCastException
.instanceof
to check type compatibility before downcasting.if (obj instanceof String) { String str = (String) obj; }
public class TestClass { public static void main(String[] args) { SuperClass superRef1 = new SuperClass(); // Reference type: SuperClass, Object type: SuperClass SuperClass superRef2 = new SubClass(); // Reference type: SuperClass, Object type: SubClass SubClass subRef1 = new SubClass(); // Reference type: SubClass, Object type: SubClass // Runtime: Method resolution based on actual object type superRef1.multiplyOperation(1, 2); // Executes SuperClass.multiplyOperation() superRef2.multiplyOperation(1, 2); // Executes SubClass.multiplyOperation() (overridden) subRef1.multiplyOperation(1, 2); // Executes SubClass.multiplyOperation() // superRef2.subtractOperation(1, 2); // Compiler error: The method subtractOperation(int, int) is undefined for the type SuperClass subRef1.subtractOperation(1, 2); // OK: subtractOperation exists in SubClass and subRef1's reference type is SubClass } }Accessing Superclass Methods:
super
keyword.
This is only available within the subclass code, not from external callers.public class SubClass extends SuperClass { @Override public Integer multiplyOperation(Integer value1, Integer value2) { Integer superResult = super.multiplyOperation(value1, value2); return superResult * 10; } }