Start New Discussion within our Software Development Community

Hi,
I have a doubt in overriding the methods in java.
Sample program For overriding .This program executes fine.

import java.io.*;
import java.util.*;
	class one
	{
		public void show(){
			System.out.println("I am in class one-Super Class");
		}
	}
	class two extends one
	{
		public void show(){
			super.show();
				System.out.println("The subclass two");
		}
	}
	class ex_override
	{	
		public static void main(String args[])throws IOException{
			two obj=new two();
			obj.show();
		}
	}

When i declared the method as static .I cannot able to implement super keyword.
For ex:A same program with static keyword.

import java.io.*;
import java.util.*;
	class one
	{
		public static void show(){
			System.out.println("I am in class one-Super Class");
		}
	}
	class two extends one
	{
		public static void show(){
			super.show();
				System.out.println("The subclass two");
		}
	}
	class ex_override
	{	
		public static void main(String args[])throws IOException{
			two obj=new two();
			obj.show();
		}
	}

Error:
ex_override.java:12: non-static variable super cannot be referenced from a static context
super.show();
^
1 error

Thank you,

With Regards,
prem

static methods/variables are associated with class-wide situation which has one copy only while non-static, i.e. member methods/variables belong to each instance. Each instance/object has its own copy of its methods/variable.
You may use super's class name to replace the handle "super":

one.show();

to meet your purpose.
In other word, one may use the class name instead of instance variable as the handle to call a static method.

This article has been dead for over six months. Start a new discussion instead.