3 Replies - 223 Views - Last Post: 21 November 2012 - 08:32 AM Rate Topic: -----

#1 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 638
  • Joined: 24-April 10

print() method for objects with inheritence

Posted 21 November 2012 - 08:11 AM

Hello. So after a while of life being life I'm getting back into Java. I have the code below which creates some objects and then prints out the details. What I'm looking for, is the "correct" way to print out object details that has inherited properties, ie, how should a print method like the one below be written, to follow standard procedure?

thanks in advance for any help :bigsmile:

class Test{
    public static void main(String [] args){
        Cat [] cats = new Cat[5];
        GuineaPig [] pigs = new GuineaPig[3];
        Person [] people = new Person[5];
        
        for(int x = 0; x < 5; x++){
            cats[x] = new Cat("Cat "+ x, x + 1, false);
        }
        
        for(int x = 0; x < 3; x++){
            pigs[x] = new GuineaPig("Guinea Pig "+ x, x + 1, false);
        }
        
        for(int x = 0; x < 5; x++){
            if(x > 2)
            people[x] = new Person("Person " + x, x + 10, "200" + x, cats[x- 3]);
            else
             people[x] = new Person("Person " + x, x + 10, "200" + x, pigs[x]);
            
        }
        
        for(int x = 0; x < 5; x++){
            people[x].print();
            System.out.println();
        }
    }    
}

class Person{
        
        private String name;
        private int age;
        private String DOB;
        private Pet ownersPets;
        
        public Person(String name, int age, String DOB, Pet ownersPets){
            this.name = name;
            this.age = age;
            this.DOB = DOB;
            this.ownersPets = ownersPets;
        }
        
        void print(){
            System.out.println("Person Name: " + name);
            System.out.println("Person Age: " + age);
            System.out.println("Person DOB: " + DOB);
            ownersPets.print();
        }
        
    }
    
    class Pet{
       String name;
       int petAge;
       
       public Pet(){
           
       }
       public Pet(String name, int petAge){
            this.name = name;
            this.petAge = petAge;
       } 
       
       void print(){
           System.out.println("Pet Name: " + name);
           System.out.println("Pet Age " + petAge);
       }
    }
    
    class Cat extends Pet{
        private boolean declawed;
        
        public Cat(){
            super();
        }
        public Cat(String name, int petAge, boolean declawed){
            super(name, petAge);
            this.declawed = declawed;
        }
        
        void print(){
            super.print();
            System.out.println("Declawed: " + declawed);
       }
    }
    
    class GuineaPig extends Pet{
        
        private boolean longHair;
        
        public GuineaPig(String name, int petAge, boolean longHair){
            super(name, petAge);
            this.longHair = longHair;
        }
        
        void print(){
            super.print();
           System.out.println("Long Hair: " + longHair);
       }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: print() method for objects with inheritence

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: print() method for objects with inheritence

Posted 21 November 2012 - 08:25 AM

"print()" is not a standard method for objects, so you have a fair bit of freedom here. The normal thing would be to write a toString() and print the returned String (either call the toString method explicitly or just System.out.print() the object)
However, the procedure you're using for print() would work just as well to compose Strings for a toString, so I can't really say I'd change much about it, if it's getting you what you want.
Was This Post Helpful? 0
  • +
  • -

#3 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 638
  • Joined: 24-April 10

Re: print() method for objects with inheritence

Posted 21 November 2012 - 08:32 AM

That's great thanks! I just want to make sure I'm not breaking any conventions or doing seriously frowned upon things, better to get it right the first time, right? Thanks
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: print() method for objects with inheritence

Posted 21 November 2012 - 08:32 AM

No, so far you're good. Carry on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1