views:

130

answers:

3

Please can anyone tell me what is the error in the following piece of code?

Question is Create a class person which has

  • A variable ‘name’ which stores the name of the person.
  • A constructor that takes a single argument that is used to initialize the name variable
  • A method getName() which displays the name.
  • A protected method setName() that takes one argument which is used to modify the name variable.

Create a subclass of the above class called student, which contains

  • A variable to store PRN of a student
  • A variable to store course the student belongs to
  • A method, which displays all the details of the student i.e, name, prn and course.

Program :

class Person
{
 String name;
 Person(String s)
 {
  name=s;
 }
 void getName()
 {
  System.out.println("Name is "+name);
 }
 void setName(String sa)
 {
  name=sa;
 }
}
class subPerson extends Person
{
 //String sa;
 int Prn;
 String course;
 subPerson(String s,int P,String co)
 {
  name=s;
  Prn=P;
  course=co;
 }
 void displayal()
 {
  System.out.println("Name is ");
  System.out.println("PRN is "+Prn);
  System.out.println("course is "+course);
 }
}
class Inher
{
 public static void main(String args[])
 {
  int area,volumea;
  subPerson h1 = new subPerson("Abhishek",20,"MBA");
  h1.displayal();
 }
}
+6  A: 

Person's constructor takes a String. Since subPerson extends Person, its constructor will invoke a constructor of Person. By default it'll use the no-arg constructor, but since Person doesn't have one, it won't work.

Try changing subPerson's constructor to this:

 subPerson(String s,int P,String co)
 {
  super(s);
  Prn=P;
  course=co;
 }
Laurence Gonsalves
And all class must be stored in separated files.
mykhaylo
As an aside: your capitalization is a bit haphazard, which makes your code hard to read. In Java, classes usually have a capitalized name (eg: SubPerson) while methods, fields, and local variables (including parameters) usually start with a lowercase letter.
Laurence Gonsalves
mykhaylo: actually, that's not true. A .java source file can have only one public top-level class, which needs to have a name consistent with the source file, but it can contain multiple non-public top-level classes. (though I don't recommend doing this in general)
Laurence Gonsalves
@mykhaylo, that is for public classes only.
Thorbjørn Ravn Andersen
+1  A: 

I assume that compiles (I'm not going to check that), then the fundamental problem is that in the displayal() method, you don't actually print out the name...

System.out.println("Name is ");

should actually be something like

System.out.println("Name is " + name);

Aside from that, there are some problems with not following typcial java coding conventions. While the code may compile and do what is desired, most java guys will likely get hung up on "not following naming conventions" instead of trying to fix the problem because the code looks unusual.

Mainguy
A: 

I'd also recommend that you pay more attention to names. They matter a great deal and deserve careful thought.

"subPerson" as a class name leaves me quite cold. Aside from the poor camel case style, the assignment explicitly calls for a class Student. Why did you go with "subPerson"?

I would advise against the "displayal" (sic) method as well. The proper idiom is to override the toString() method in Object.

I'd write it like this:

/**
 * Person
 * User: Michael
 * Date: Sep 27, 2009
 * Time: 10:00:00 AM
 */
public class Person
{
   private String name;

   public static void main(String[] args)
   {
      Person s = new Student("Foo Bar", "35", "Intro To Java");

      System.out.println(s);
   }

   public Person(String name)
   {
      if ((name == null) || (name.trim().length() == 0))
         throw new IllegalArgumentException("name cannot be blank or null");

      this.name = name;
   }

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      if ((name == null) || (name.trim().length() == 0))
         throw new IllegalArgumentException("name cannot be blank or null");

      this.name = name;
   }

   @Override
   public String toString()
   {
      return "Person{" +
      "name='" + name + '\'' +
      '}';
   }
}

class Student extends Person
{
   private String prn;
   private String course;

   Student(String name, String prn, String course)
   {
      super(name);

      this.prn = prn;
      this.course = course;
   }

   @Override
   public String toString()
   {
      return "Student{" +
      "name='" + getName() + '\'' + 
      ", prn='" + prn + '\'' +
      ", course='" + course + '\'' +
      '}';
   }
}
duffymo