# Inheritance Problem

• 01-11-2011, 01:51 AM
kazumahits
Inheritance Problem
Quote:

/**
* @(#)Dionzkii.java
*
* Dionzkii Application
*
* @Author
* @Version 1.00 2011/1/10
*/

Class Dionzkii {

Public Static Void Main(string[] Args) {
Class Person
{
String Firstname;
String Lastname;

Person(string Fname, String Lname)
{
Firstname = Fname;
Lastname = Lname;
}

Void Display()
{
System.out.println("first Name : " + Firstname);
System.out.println("last Name : " + Lastname);
}
}

Class Student Extends Person
{
Int Id;
String Standard;
String Instructor;

Student(string Fname, String Lname, Int Nid, String Stnd, String Instr)
{
Super(fname,lname);
Id = Nid;
Standard = Stnd;
Instructor = Instr;
}
Void Display()
{
Super.display();

System.out.println("id : " + Id);
System.out.println("standard : " + Standard);
System.out.println("instructor : " + Instructor);
}
}

Class Teacher Extends Person
{
String Mainsubject;
Int Salary;
String Type; // Primary Or Secondary School Teacher

Teacher(string Fname, String Lname, String Sub, Int Slry, String Stype)
{
Super(fname,lname);
Mainsubject = Sub;
Salary = Slry;
Type = Stype;
}
Void Display()
{
Super.display();

System.out.println("main Subject : " + Mainsubject);
System.out.println("salary : " + Salary);
System.out.println("type : " + Type);
}
}

Class Inheritancedemo
{
Public Void Main(string Args[])
{
Person Pobj = New Person("rayan","miller");
Student Sobj = New Student("jacob","smith",1,"1 - B","roma");
Teacher Tobj = New Teacher("daniel","martin","english","6000","primar y Teacher");
System.out.println("person :");
Pobj.display();
System.out.println("");
System.out.println("student :");
Sobj.display();
System.out.println("");
System.out.println("teacher :");
Tobj.display();
}
}
}
}
• 01-11-2011, 01:59 AM
Fubarable
Quote:

Originally Posted by kazumahits

neither can we. what the heck is your question?
• 01-11-2011, 02:16 AM
sunde887
I was able to get it to compile and run by just separating the classes:

Code:

```class APerson{...} class Student extends APerson{...} class Teacher extends APerson{...} public class Person {   public static void main(String[] args)   {       Teacher tobj = new Teacher(...);       Student sobj = new Student(...);       APerson pobj = new APerson(...);       tobj.display();       sobj.display();       pobj.display();     } }```

as a quick question from me, instead of making a new topic for a small question, if I declare the fields as private, when I build constructors, is it a waste to set them with this.x = y;?
• 01-11-2011, 05:54 AM
goldest
Quote:

Originally Posted by sunde887
as a quick question from me, instead of making a new topic for a small question, if I declare the fields as private, when I build constructors, is it a waste to set them with this.x = y;?

That isn't a waste. In that case you can create your class object from outside the class. You are not accessing your private variables directly as you are using this. So as long as you can create a new object passing the variable value and it compiles and works fine, it's no waste.

Code:

```public class Box {         private int x;         private int y;                 public Box(int x, int y){                 this.x = x;                 this.y = y;         } }```
You can always create a Box object from any class that can see Box.
Code:

`Box b = new Box(4, 6);`
And this will work.

Hope that helps,

Goldest
• 01-11-2011, 10:04 AM
Tolls
Quote:

Originally Posted by kazumahits