Results 1 to 6 of 6
  1. #1
    kazumahits is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default 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();
    }
    }
    }
    }
    Can't determined whats the problem... please help :D

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kazumahits View Post
    Can't determined whats the problem... please help :D
    neither can we. what the heck is your question?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by sunde887 View Post
    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
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Quote Originally Posted by kazumahits View Post
    Can't determined whats the problem... please help :D
    The problem is that that code will not compile for any number of reasons.
    You have capitalised loads of keywords, for starters.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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

  1. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 03:55 PM
  2. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  3. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  4. Inheritance method problem
    By Chasingxsuns in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 08:30 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM

Tags for this Thread

Posting Permissions

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