0
example of inheritence in java:
import java.util.Scanner;
class Creature{
private String eyeColor;
private String skinColor;
private int height;
private int weight;
private boolean gender;
public Creature()
{
eyeColor="";
skinColor="";
height=0;
weight=0;
gender=true;
}
public Creature(String eyeColor, String skinColor,
int height, int weight, String gender)
{
this.eyeColor=eyeColor;
this.skinColor=skinColor;
this.height=height;
this.weight=weight;
setGender(gender);
}
public void setGender(String gender)
{
if(gender=="male")
this.gender=true;
else if(gender=="female")
this.gender=false;
else
System.out.println("error entering gender! it should be either male or female.");
}
public String getGender()
{
if(gender==true)
return "male";
else return "female";
}
public void setEyeColor(String eyeColor)
{
this.eyeColor=eyeColor;
}
public String getEyeColor()
{
return eyeColor;
}
public void setSkinColor(String skinColor)
{
this.skinColor=skinColor;
}
public String getSkinColor()
{
return skinColor;
}
public void setHeight(int height)
{
this.height=height;
}
public int getHeight()
{
return height;
}
public void setWeight(int weight)
{
this.weight=weight;
}
public int getWeight()
{
return weight;
}
public void walk()
{
System.out.println("walking...");
}
public void breath()
{
System.out.println(" is breathing...");
}
public String toString()
{
return " Eye color: "+eyeColor+", Skin color: "+skinColor+",
Height: "+height+", Weight: "+weight+", gender: "+getGender();
}
}
class Human extends Creature
{
private String name;
public Human()
{
super("","",0,0,"male");
name="";
}
public Human(String name, String eyeColor, String skinColor,
int height, int weight, String gender)
{
super(eyeColor,skinColor,height,weight,gender);
this.name=name;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
/*public void walk()
{
System.out.println("walking...");
}
public void breath()
{
System.out.println(name+" is breathing...");
}*/
/*public String toString()
{
return "name: "+name+", Eye color: "+eyeColor+", Skin color: "+
skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
}*/
}
class Animal extends Creature
{
private int price;
public Animal()
{
super("","",0,0,"male");
price=0;
}
public Animal(int price,String eyeColor, String skinColor,
int height, int weight, String gender)
{
super(eyeColor,skinColor,height,weight,gender);
this.price=price;
}
public void setPrice(int price)
{
this.price=price;
}
public int getPrice()
{
return price;
}
/*public void walk()
{
System.out.println("walking...");
}
public void breath()
{
System.out.println(name+" is breathing...");
}
public String toString()
{
return "name: "+name+", Eye color: "+eyeColor+", Skin color: "+
skinColor+", Height: "+height+", Weight: "+weight+", gender: "+getGender();
}*/
}
class Main {
public static void main(String[] args) {
Human obj; //creating object from class Human
//creating instance of class Human
obj=new Human("adnan","brown","white",185,92,"male");
obj.breath();
//printing object information using the tostring() function
System.out.println(obj.toString());
Human obj2; //creating object from class Human
obj2=new Human(); //creating instance of class Human
obj2.setName("reem");
obj2.setGender("female");
obj2.setEyeColor("green");
obj2.setSkinColor("white");
obj2.setHeight(160);
obj2.setWeight(55);
obj2.breath();
System.out.println();
//printing the object info using get functions:
System.out.println(obj2.getName());
System.out.println(obj2.getGender());
System.out.println(obj2.getEyeColor());
System.out.println(obj2.getSkinColor());
System.out.println(obj2.getHeight());
System.out.println(obj2.getWeight());
}
}
example of inheritence in java:
SimpleGeometricObject is the parent class,
Circle and Rectangle are child classes.