Saturday 9 July 2022

How to call super class method from sub class overriding method?

Yes, you can call the super class method from subclass overriding method using super keyword.

 

Syntax

super.methodName(arguments); 

 

MethodOverridingDemo1.java

package com.sample.app;

public class MethodOverridingDemo1 {
	
	public static class A{
		public void welcomeUser() {
			System.out.println("Hiiii!!!!!");
		}
	}
	
	public static class B extends A{
		@Override
		public void welcomeUser() {
			super.welcomeUser();
			System.out.println("Hello...........");
		}
	}
	
	public static void main(String[] args) {
		B b = new B();
		b.welcomeUser();
	}

}

 

Output

Hiiii!!!!!
Hello...........

 

Unlink constructor, super class method call doesn’t need to be first statement in the overriding method.

public static class B extends A {
	@Override
	public void welcomeUser() {
		System.out.println("Hello...........");
		super.welcomeUser();

	}
}

 

MethodOverridingDemo2.java

package com.sample.app;

public class MethodOverridingDemo2 {

	public static class A {
		public void welcomeUser() {
			System.out.println("Hiiii!!!!!");
		}
	}

	public static class B extends A {
		@Override
		public void welcomeUser() {
			System.out.println("Hello...........");
			super.welcomeUser();

		}
	}

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

}

 

Output

Hello...........
Hiiii!!!!!

 

You can call super class method any number of times from the sub class method.

 

MethodOverridingDemo3.java

package com.sample.app;

public class MethodOverridingDemo3 {

	public static class A {
		public void welcomeUser() {
			System.out.println("Hiiii!!!!!");
		}
	}

	public static class B extends A {
		@Override
		public void welcomeUser() {
			super.welcomeUser();
			System.out.println("Hello...........");
			super.welcomeUser();
			super.welcomeUser();
			super.welcomeUser();

		}
	}

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

}

 

Output

Hiiii!!!!!
Hello...........
Hiiii!!!!!
Hiiii!!!!!
Hiiii!!!!!

 

You can call any non-private method of super class from the subclass.

 


public static class B extends A {
	public void demo() {
		welcomeUser();
		welcomeUser();
	}
}

SuperclassMethodCallDemo.java

package com.sample.app;

public class SuperclassMethodCallDemo {
	public static class A {
		public void welcomeUser() {
			System.out.println("Hiiii!!!!!");
		}
	}

	public static class B extends A {
		public void demo() {
			welcomeUser();
			welcomeUser();
		}
	}
	
	public static void main(String[] args) {
		new B().demo();
	}
}

Output

Hiiii!!!!!
Hiiii!!!!!

Is it mandatory to call super class method from overriding method?

No, it is optional.

 

MethodOverridingDemo4.java

package com.sample.app;

public class MethodOverridingDemo4 {
	public static class A {
		public void welcomeUser() {
			System.out.println("Hiiii!!!!!");
		}
	}

	public static class B extends A {
		@Override
		public void welcomeUser() {
			System.out.println("Hello...........");
		}
	}

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

Output

Hello...........

You may like

Interview Questions

float vs double in Java

Quick guide to race condition in Java with examples

How to read the file content using an iterator in Java?

Implement a Closeable iterator in Java

Quick guide to generate random data in Java

What are the different cache eviction strategies?





No comments:

Post a Comment