This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about super key

 
Kranthi Kondapaka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks guys(especially paul),its basic thing but i got good refreshment of my knowledge.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic