what happens if both superclass and subclass have a field with same name?

Sub class field will hide the Super class field. Hidden super class field in sub class can be accessed using super keyword. Example

class Show {
	int num = 150;
class Display extends Show {
	int num = 250;
	public void display(){
	    System.out.println("num of subclass = " + num);
		System.out.println("num of super class = " + super.num);
public class Main {
	public static void main(String args[]){
		//create Show class object.
		Display obj = new Display();
		//method call


num of subclass = 250      
num of super class = 150

Java interview questions on Inheritance

Please follow and like us:
Content Protection by DMCA.com