Define a class name Plant , Define another class name PalmTree which is extended form Plant
belongs to collection: Computer Programming-2 (CS2301) java examples Inheritance – Part 1
All Answers
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Pleas enter the plant name :");
String PlantName=input.nextLine();
System.out.println("Pleas enter the Fruet name :");
String FruetName=input.nextLine();
System.out.println("Pleas enter the Hight :");
int Hight=input.nextInt();
PalmTree tree1=new PalmTree();
PalmTree tree2=new PalmTree(PlantName,FruetName,Hight);
System.out.println(tree2);
System.out.println(tree1);
}
}
class plant
{
private String Name;
private int hight;
public plant()
{
Name="";
hight=1;
}
public plant(String name,int hight)
{
Name=name;
this.hight=hight;
}
public String getName() {
return Name;
}
public void setName(String newName) {
this.Name = newName;
}
public int getHight() {
return hight;
}
public void setHight(int newHight) {
this.hight = newHight;
}
public String toString() {
return "Tree Name is : " + this.Name + " And its hight is: " + this.hight;
}
}
class PalmTree extends plant
{
private String fruetName;
public PalmTree()
{
super();
fruetName="";
}
public PalmTree( String PlantName,String FruetName,int Hight)
{
super(PlantName,Hight);
this.fruetName= FruetName;
}
public String toString() {
return "Plant Name is : "+this.getName() + " Fruet Name is :"+this.fruetName+" ,And its hight is: " + this.getHight();
}
}
total answers (2)