简体   繁体   中英

Java - Inheritance and Polymorphism

The instructions are these: Create a Main class which will contain the main method. Implement the following inside the main method:

– create objects from the regular classes using the overloaded constructors

– test all instance methods (ie setters, getters, overloaded, overridden)

– a parent reference assigned to a child object

– a method call which passes a polymorphic object

And I got stuck in my Parent Class which is the Rainforest. What should I do next?

Rainforest Class

//INHERITANCE                                                      
  package com.ancient;                                              
  public class Rainforest                                                
  {                                         
  public static void main(String[] args)                                 
  }

Rainforest Class - Inherits Parent and Child

Mammal Class (Parent Class)

//PARENT CLASS                                                     
  package com.ancient;                                              
  public class Mammal                                                    
  {
//INSTANCE VARIABLES
private String type;
private double speed;

//CONSTRUCTOR
public Mammal(String type, double speed)
{
    this.type = type;
    this.speed = speed;
}

//SETTERS
public void setType(String type) 
{
    this.type = type;
}

public void setSpeed(double speed) 
{
    this.speed = speed;
}

//GETTERS
public String getType() 
{
    return type;
}

public double getSpeed() 
{
    return speed;
}

//OVERRIDDEN
public void hunt(int Food)//int Food is the quantity - how many does the eagle he hunts?
{
    System.out.println("The Bald eagle preys " + Food + "animals within a day to survive.");
}

}

Mammal Class - Part 1 Mammal Class - Part 2

Bird (Child Class)

package com.ancient;                                                
 public class Bird extends Mammal                                        
 {                                                                  
 public Bird(String type, double speed) 
{
    super(type, speed);
}

//OVERRIDDING
public void hunt(int Food)//int Food is the quantity - how many does the eagle he hunts?
{
    System.out.println("The Bald eagle will prey " + Food + "animals within a day to survive.");                                                  
}                                                                        
}

Bird - Child Class

My Project content Project content > src > package (library) > regular (2) and main classes (1)

Now, the problem here is: my constructor is undefined. Should I make a new package and insert Rainforest class to it? OR there's something wrong with my object in Mammal and Bird Class?

在Mammal类中有一个接受参数的构造函数,但是您试图在不传递参数的情况下实例化对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM