examples of OOP object oriented programming in java

0

class vs Object vs Instance

class: is the class, example: Human

object: is a copy of the category, example: h1

instance: is a live version of the classification, meaning it is given values, ie: h1.name="adnan"

 

An example of a class and its functions, creating an object from it, and creating an instance of it:

import java.util.Scanner;

class Human
{
    String name;
    String eyeColor;
    String skinColor;
    int height;
    int weight;
    
    public void walk()
    {
        System.out.println("walking...");
    }
    public void breath()
    {
        System.out.println(name+" is breathing...");
    }
}

class Main {
  public static void main(String[] args) {
  Human obj; //creating object from class Human
  obj=new Human(); //creating instance of class Human
  obj.name="adnan";
  obj.eyeColor="brown";
  obj.breath();
  
  Human obj2; //creating object from class Human
  obj2=new Human(); //creating instance of class Human
  obj2.name="omar";
  obj2.eyeColor="green";
  obj2.breath();
  
}
}

 

---------------------------------------------------------------------------------------

 

Same as the previous example after adding the set and get methods for the encapsulation encapsulation, also with the addition of constructors, and also adding the tostring method

The constructor is a special case of functions, it has no return value, and its name is the same as the name of the class, and an infinite number of constructor functions can be made for a single class, and the difference between them is the number and type of input data parameters

import java.util.Scanner;

class Human
{
    private String name;
    private String eyeColor;
    private String skinColor;
    private int height;
    private int weight;
    private boolean gender;
    
    public Human()
    {
        name="";
        eyeColor="";
        skinColor="";
        height=0;
        weight=0;
        gender=true;
    }
    
    public Human(String name, String eyeColor, String skinColor, int height, int weight, String gender)
    {
        this.name=name;
        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 setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    
    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(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
  obj=new Human("adnan","brown","white",185,92,"male"); //creating instance of class Human
  obj.breath();
  System.out.println(obj.toString()); //printing object information using the tostring() function
  
  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 a simple java program that contains one employee class, and contains employee properties and get and set as well as constructors for the class

The constructor is a special case of functions, it has no return value, and its name is the same as the name of the class, and an infinite number of constructor functions can be made for a single class, and the difference between them is the number and type of input data parameters

package classesExamples;

public class Employee {

 private int id;
 private String name;
 private double basicsalary;
 private double allowances;
 private boolean is_male;
 
 public Employee()
 {
  id=0;
  name="";
  basicsalary=0;
  allowances=0;
  is_male=true;
 }
 public Employee(String b, double c, double d)
 {
  name=b;
  basicsalary=c;
  allowances=d;
 }
 
 public Employee(int a, String b, double c, double d,String gender)
 {
  id=a;
  name=b;
  basicsalary=c;
  allowances=d;
  
  if(gender=="male")
   is_male=true;
  else is_male=false;
 }
 
 public int getId()
 {
  return id;
 }

 public String getName()
 {
  return name;
 }
 public double getSalary()
 {
  return basicsalary;
 }
 public double getAllowances()
 {
  return allowances;
 }

 public String getGender()
 {
  if(is_male==true)
  return "male";
  else return "female";
 }
 
 public void setId(int i)
 {
  id=i;
 }

 public void setname(String x)
 {
  name=x;
 }
 public void setbasicSalary(double v)
 {
  basicsalary=v;
 }
 public void setAllowances(double f)
 {
  allowances=f;
 }

public class EmployeeManagement {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Employee emp=new Employee();
  
  int empid = emp.getId();
  System.out.println(empid);
  
Employee emp2=new Employee(1,"adnan",1000,120,"male");
  
System.out.println("employee info is: \n----------------------------");

System.out.println("id:"+emp2.getId());
System.out.println("name:"+emp2.getName());
System.out.println("basic salary :"+emp2.getSalary());
System.out.println("allowances:"+emp2.getAllowances());
System.out.println("total salary:"+(emp2.getSalary()+emp2.getAllowances()));
System.out.println("gender :"+emp2.getGender()); 
 }
}

 

Example of inheritance 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
  obj=new Human("adnan","brown","white",185,92,"male"); //creating instance of class Human
  obj.breath();
  System.out.println(obj.toString()); //printing object information using the tostring() function
  
  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

tiny example of OOP :

CAR class

------

class Car
{
    String brand;
    String boardNumber;
    String engineSize;
    public void printInfo()
    {
        System.out.println(brand+"\n"+boardNumber+"\n"+engineSize);
    }
}
public class Main
{
	public static void main(String[] args) {
	    Car c1=new Car();
	    Car c2=new Car();
	    Car c3=new Car();
	    Car c4=new Car();
	    Car c5=new Car();
	    
	    c1.brand="toyota";
	    c1.boardNumber="2342";
	    c1.engineSize="1.6 liters";
	    
	    c2.brand="honda";
	    c2.boardNumber="2534";
	    c2.engineSize="1.5 liters";
	    
	    c3.brand="mazda";
	    c3.boardNumber="4554";
	    c3.engineSize="1.6 liters";
	    
	    c4.brand="bmw";
	    c4.boardNumber="3334";
	    c4.engineSize="2.4  liters";
	    
	    c5.brand="mercedes";
	    c5.boardNumber="4354";
	    c5.engineSize="2.5 liters";
	    
	    c1.printInfo();
	    c2.printInfo();
	    c3.printInfo();
	    c4.printInfo();
	    c5.printInfo();
	   
	}
}

OOP example in java: monster game

write java class Monster, which contains properties of monster of a video game:

import java.util.Scanner;

class Monster
{
    private int type; //monster is either animal, or zombiy, or solider
    private int speed;
    private int ammo; //number of bullets
    private int lives;
    private static int counter=0; //contains the number of current monsters

    public void setType(String t)
    {
     if(t=="animal")
     type=1;
     else if(t=="zombiy")
     type=2;
     else if(t=="solider")
     type=3;
     else System.out.println("invalid entry");
    }
    
    public String getType()
    {
        String s="";
        if(type==1)
        s="animal";
        else if(type==2)
        s="zombiy";
        else if(type==3)
        s="solider";
        return s;
    }
    public void setSpeed(int speed)
    {
        //we will suppose that the monster speed should be between 1 to 3
        if(speed>=1 && speed <=3)
        this.speed = speed;
        else System.out.println("invalid speed entry");
    }
    public int getSpeed()
    {
        return speed;
    }
    public void setAmmo(int ammo)
    {
        if(ammo >=0 && ammo <=100)
        this.ammo = ammo;
        else System.out.println("invalid ammo entry");
    }
    public int getAmmo()
    {
        return ammo;
    }
    public void setLives(int lives)
    {
        if(lives >=1 && lives <=3)
        this.lives = lives;
        else System.out.println("invalid lives entry");
    }
    public int getLives()
    {
        return lives;
    }
    private static void increaseCounter()// we have putted the modifier private to not allow any programmer from outside this class to call this function
    {
        counter++;
    }
    public static int getCounter()
    {
        return counter;
    }
    
    public Monster()
    {
        type=2;
        speed=1;
        ammo=100;
        lives=3;
        increaseCounter();
    }
    
}

class Main {
  public static void main(String[] args) {
  Monster m1=new Monster();
  System.out.println("monster information: "+m1.getType()+"  "+m1.getAmmo()+"   "+m1.getLives()+ "  "+ m1.getSpeed());
  m1.setType("animal");
  System.out.println(m1.getType());
  
  System.out.print("the number of current monsters: "+Monster.getCounter());
}
}

total contributions (2)

New to examplegeek.com?

Join us