Results 1 to 6 of 6
Thread: Inheritance Problem
- 01-11-2011, 12:51 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Inheritance Problem
Can't determined whats the problem... please help :D/**
* @(#)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:16 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 01:39 AM.
- 01-11-2011, 04:54 AM #4
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.
You can always create a Box object from any class that can see Box.Java Code:public class Box { private int x; private int y; public Box(int x, int y){ this.x = x; this.y = y; } }
And this will work.Java Code:Box b = new Box(4, 6);
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-11-2011, 09:04 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 01-11-2011, 03:46 PM #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.
Classes should start with a capital letter.
Variables (fields) should start with a lower case unless final.
Methods should start with a lowercase letter.
Reserved words should start with a lower case letter.
Oh yeah, don't ever code in something like MS Word. Its horrible, and it caps your words!
Similar Threads
-
Inheritance problem
By ZuperZombie in forum Advanced JavaReplies: 0Last Post: 04-02-2010, 03:55 PM -
Inheritance Problem
By g2beastie in forum New To JavaReplies: 4Last Post: 03-25-2010, 08:23 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Inheritance method problem
By Chasingxsuns in forum New To JavaReplies: 3Last Post: 11-10-2009, 08:30 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks