1. Member
Join Date
Jan 2011
Posts
2
Rep Power
0

Inheritance Problem

/**
* @(#)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();
}
}
}
}

2. Originally Posted by kazumahits
neither can we. what the heck is your question?

3. I was able to get it to compile and run by just separating the classes:

Java 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;?
Last edited by sunde887; 01-11-2011 at 02:39 AM.

4. 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.

Java 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.
Java Code:
`Box b = new Box(4, 6);`
And this will work.

Hope that helps,

Goldest

5. Moderator
Join Date
Apr 2009
Posts
13,215
Rep Power
23
Originally Posted by kazumahits
The problem is that that code will not compile for any number of reasons.
You have capitalised loads of keywords, for starters.

6. What Tolls is pointing out is the old adage: "A problem clearly stated is half solved". That means that before you can write your novel, you must first learn to write. In this case, review the java syntax guidelines!
Java is CASE sensitive.

Oh yeah, don't ever code in something like MS Word. Its horrible, and it caps your words!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•