2 Replies - 718 Views - Last Post: 26 April 2013 - 07:22 AM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 381
  • Joined: 29-September 12

Abstract and inheritance help

Posted 25 April 2013 - 09:56 PM

So i have the abstract class Person:
public abstract class Person 
{
	private String name;
	private String gender;
	private int age;
	
	public Person()
	{
		this.name = "";
		this.age = 0;
		this.gender = "";
	}
	
	public Person(String name, String gender, int age)
	{
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	public void printName()
	{
		System.out.println("Some Name");
	}
	public void printAge()
	{
		System.out.println("Some Age");
	}
	public abstract void printGender();

	public String getName()
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public String getGender()
	{
		return gender;
	}

	public void setGender(String gender) 
	{
		this.gender = gender;
	}

	public int getAge() 
	{
		return age;
	}

	public void setAge(int age) 
	{
		this.age = age;
	}
}



and two classes extend Person:
public class Man extends Person
{
	
	public Man()
	{
		super();
	}
	
	public void printGender() 
	{
		System.out.println("Man");
	}
	
}



public class Woman extends Person
{
	public Woman()
	{
		super();
		
	}		
	public void printGender() 
	{
		System.out.println("Woman");
	}
}



Driver here:
import java.util.ArrayList;

public class PersonTester 
{
	public static void main(String[] args)
	{
		Man man = new Man();
		Woman woman = new Woman();
		
		ArrayList<Person> pArray = new ArrayList<Person>();
		
		for(Person p : pArray)
		{
			p.printGender();
		}
	}
}



I would want to print
Man
Woman


I tried to print but pArray contain null, is there something wrong?


Edit: Sorry I was being silly, I did not add Man and Woman in the array LOL.

This post has been edited by RozenKristal: 25 April 2013 - 10:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Abstract and inheritance help

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Abstract and inheritance help

Posted 26 April 2013 - 03:56 AM

Use constructor chaining:
 public Person(){
        this("","", 0);
    }

    public Person(String name, String gender, int age){
        this.name = name;
        this.gender = gender;
        this.age = age;
    }


You should consider using an enum type for the gender since they should be either male vs female.
In your derived classes, you should provide constructors that would allow to construct "meaningful" Man and Woman. For now, they will only call the default constructor in the super class.
Was This Post Helpful? 1
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Abstract and inheritance help

Posted 26 April 2013 - 07:22 AM

Oops you forget ever to add the objects into the ArrayList.

import java.util.ArrayList;

public class PersonTester 
{
	public static void main(String[] args)
	{
		Man man = new Man();
		Woman woman = new Woman();
		
		ArrayList<Person> pArray = new ArrayList<Person>();
		
		for(Person p : pArray)
		{
			p.printGender();
		}
	}
}



Should become...
import java.util.ArrayList;

public class PersonTester 
{
	public static void main(String[] args)
	{
		Man man = new Man();
		Woman woman = new Woman();
		
		ArrayList<Person> pArray = new ArrayList<Person>();
	        pArray.add(man);   // added
                pArray.add(woman); // added	
		for(Person p : pArray)
		{
			p.printGender();
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1