Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: display methods with inheritance

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Tulsa
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default display methods with inheritance

    Hey guys, sorry if this has been answered on this forum already. I searched around a bit and couldn't find the specifics of what I am confused about.

    Basically I've created a small program to test my overall understanding of encapsulation, inheritance, and polymorphism. I'm using Planets in the solar system as the theme, basically making a superclass "Planet" that has subclasses "Mercury", "Venus", etc. Here is code for my parent class "Planet", and one subclass "Mercury". I'm using mass, temperature, and distance from the sun as the 3 variables that every planet inherits.


    Parent class "Planet"
    public class Planet
    {
    	private double mass;
    	private double temperature;
    	private long distance;
     
    	//Planet constructor
    	public Planet(double size, double temp, long dist)
    	{
    		mass = size;
    		temperature = temp;
    		distance = dist;
    	}
     
    	//default constructor
    	public Planet()
    	{
     
    	}
     
    	//display method
    	public void display()
    	{
     
    	}
     
    	//get mass
    	public double getMass()
    	{
    		return mass;
    	}
     
    	//set mass
    	public void setMass(double size)
    	{
    		mass = size;
    	}
     
    	//get temp
    	public double getTemp()
    	{
    		return temperature;
    	}
     
    	//set temp
    	public void setTemp(double temp)
    	{
    		temperature = temp;
    	}
     
    	//get dist from sun
    	public long getDistance()
    	{
    		return distance;
    	}
     
    	//set distance from sun
    	public void setDistance(long dist)
    	{
    		distance = dist;
    	}
    }

    And the Mercury class...

    public class Mercury extends Planet
    {
     
    	public Mercury(double mass, double temperature, long distance)
    	{
    		setMass(mass);
    		setTemp(temperature);
    		setDistance(distance);
    	}
     
    	public void display()
    	{
    		System.out.println("Planet Mercury has a mass of " + getMass() + " Earths.");
    		System.out.println("Its average temperature is " + getTemp() + " degrees Fahrenheit.");
    		System.out.println("Its distance from the sun is " + getDistance() + " Kilometers.");
     
    	}
     
    }

    Finally here is the PlanetTest class which is the main method....

    public class PlanetTest 
    {
     
    	public static void main(String[] args) 
    	{
    		Planet mercury = new Mercury(0.55, 350, 57910000);
    		mercury.display();
     
    	}
     
    }


    Basically I understand how to inherit functionality from the Planet superclass, which in this case I am inheriting the getters and setters. I want to be able to also inherit a display method from the superclass, but I am unsure of how to go about it. Does each child class need a display method, or is my approach incorrect? I feel as though my understanding of polymorphism is lacking a bit.

    Any help or tips are much welcome and appreciated!

    Thanks for you time,
    Todd


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: display methods with inheritance

    The first thing, are you using a good example? I can easily change your code to:
    public class Planet {
        private String name;
        private double mass;
        private double temperature;
        private long distance;
     
        public Planet(String n, double size, double temp, long dist) {
            name = n;
            mass = size;
            temperature = temp;
            distance = dist;
        }
     
        public void display() {
            // laziness, couldn't be bothered including all the info
            System.out.println(name + " has a mass of: " + mass);
        }
     
        public static void main(String[] args) {
            Planet mercury = new Planet("Mercury", 0.55, 350, 57910000);
            mercury.display()
        }
    }
    However, if you want each Planet to have different output then you can have different classes each with a display method that does different output. What you can do is make the Planet class abstract and the display method abstract too.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    ddotreprah (November 20th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Tulsa
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: display methods with inheritance

    Sweet, making the display and class abstract is what I was looking for. My example is a little weak, I sort of came up with it on the fly. In my original Java class we used a parallelogram, triangle, square, etc... as the example.

Similar Threads

  1. Generic Inheritance
    By nogaerez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 19th, 2011, 08:31 AM
  2. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  3. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM