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());
}
}
tiny example of OOP :
CAR class
------