inheritance in java with examples

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());
}
}

All Contributions

example of inheritence in java:

SimpleGeometricObject is the parent class, 

Circle and Rectangle are child classes.

import java.util.Date;
class SimpleGeometricObject {
  private String color = "white";
  private boolean filled;
  private Date dateCreated;
  
    public SimpleGeometricObject() {
    dateCreated = new java.util.Date();
  }
 public SimpleGeometricObject(String color, boolean filled) {
    dateCreated = new Date();
    this.color = color;
    this.filled = filled;
  }
 public String getColor() { return color; }
 
 public void setColor(String color) 
	{this.color = color;}
    
   
 public boolean isFilled() 
{    return filled;      }

 public void setFilled(boolean filled)   {   this.filled = filled;}
  
  public Date getDateCreated()   
  {
    return dateCreated;
  }
  
  public String toString() {
    return "created on " + 
             dateCreated + 
            "\ncolor: " + color + 
             " and filled: " + 
              filled;
  }

}

class Circle extends SimpleGeometricObject { 
  private double radius; 
  public Circle() { } 
  public Circle(double radius) 
  { this.radius = radius; } 
  public Circle(double radius, String 
                color, boolean filled) 
  { this.radius = radius; 
    setColor(color); 
    setFilled(filled); } 
  public double getRadius() 
  { return radius; }
  public void setRadius(double radius) 
  { this.radius = radius; } 
    
    public double getArea() 
  { return radius * radius *  
     Math.PI; }
 public double getDiameter() 
 { return 2 * radius; } 
 public double getPerimeter() 
 { return 2 * radius * Math.PI; }      
 public void printCircle() {
  System.out.println("The circle is created " + super.getDateCreated() + " and the radius is " + radius);
//or //  System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); 
 
     
 }

}

class Rectangle extends SimpleGeometricObject { 
  private double width, height; 
  public Rectangle() { } 
  public Rectangle( double w, double h)  
  { this.width = w; this.height = h; }  
  public Rectangle( double w, double h, String color, boolean filled)  
  {
      this.width = w; this.height = h;   
    setColor(color); setFilled(filled);   
  } 
  public double getWidth() 
  { return width; } 
public void setWidth(double width) 
  { this.width = width;    } 
 public double getHeight() 
  { return height; } 
 public void setHeight(double height)  
 { this.height = height; } 
 public double getArea() 
 { return width * height; } 
 public double getPerimeter() 
 { return 2 * (width + height); } 
}

public class Main
{
	public static void main(String[] args) {
	Circle circle = new Circle(1); 
    System.out.println("A circle " + circle.toString());   
    System.out.println("The color is " + circle.getColor()); 
    System.out.println("The radius is " + circle.getRadius()); 
    System.out.println("The area is " + circle.getArea()); 
    System.out.println("The diameter is " + circle.getDiameter()); 
    Rectangle rectangle = new Rectangle(2, 4); 
    System.out.println("\n A rectangle " + rectangle.toString()); 
    System.out.println("The area is " + rectangle.getArea()); 
    System.out.println("The perimeter is " + rectangle.getPerimeter()); 
	}
}

total contributions (1)

New to examplegeek.com?

Join us