• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about super key

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package pack;
import java.io.*;
import java.util.*;
class Samp1
{
public Samp1()//default constructor
{}
String name;
int empid;
float sal;
public Samp1(String name,int empid,float sal)
{
this.name=name;
this.empid=empid;
this.sal=sal;
}
public void disp(String name,int empid,float sal)
{
System.out.println("Name of the Employee:"+name);
System.out.println("Employee ID of the Employee:"+empid);
System.out.println("Salary of the Employee:"+sal);
}
}
class SubSamp1 extends Samp1
{
public static void main(String[] args) throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the EmpId:");
int empid=Integer.parseInt(br.readLine());
System.out.println("Enter the Name:");
String name=br.readLine();
System.out.println("Enter the Salary:");
float sal=Float.parseFloat(br.readLine());
super.Samp1(name,empid,sal);
}
}
In the about program i am getting an error at super key and the error is

"Cannot use super in a static context ".
Can anyone give me the solution for this problem
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to call a constructor from a static context. You need to create an instance of your class to call the constructor.
 
Kranthi Kondapaka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
but its the subclass of samp1,then what is the need to create the instance when m extinding the superclass.

According to my knowledge when we extend a class then we can directly use the methods and variable of the base class unless they are declared private
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beginner question.

super.Samp1(name,empid,sal);



Samp1() is the constructor for Samp1 class. Constructors cannot be called directly, as if they were ordinary methods. A constructor can only be called indirectly, by using the "new" operator.

What are you trying to achieve with this line?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but its the subclass of samp1,then what is the need to create the instance when m extinding the superclass.



Since you only have the constructor you want to use in your super class you will need to either create an instance of the super class:


or add a simmilar constructor to your subclass and call that.



According to my knowledge when we extend a class then we can directly use the methods and variable of the base class unless they are declared private


Well, that's partically true. A couple of points though: a constructor is neither a method or a variable. It is a special way of creating an instance of an object. Also, since a constructor is called to create a specific instance you can't use them in a static context. "static" remember applies to the class, not the object instance.
 
Kranthi Kondapaka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys(especially paul),its basic thing but i got good refreshment of my knowledge.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic