Sunday, 12 December 2021

Java: How to call base class method from subclass overriding method?

Using ‘super’ keyword, you can call the base class method from the subclass overriding method.

class B extends A {
	public void print() {
		super.print();
		System.out.println("In class B");
	}
}

 

Find the below working application.

 

CallingSuperClassMethodDemo.java

 

package com.sample.app.overriding;

public class CallingSuperClassMethodDemo {

	static class A {
		public void print() {
			System.out.println("In class A");
		}
	}

	static class B extends A {
		public void print() {
			super.print();
			System.out.println("In class B");
		}
	}

	public static void main(String args[]) {
		B b = new B();
		b.print();
	}

}

 

Output

In class A
In class B

 

  

You may like

Interview Questions

Set a system property in command line

How to get the capacity of ArrayList in Java?

volatile reference vs Atomic references defined in java.util.concurrent.atomic package

Check whether I have JDK or JRE in my system

How to check whether string contain only whitespaces or not?

No comments:

Post a Comment