The difference between Classes, Objects, and Instances

前端 未结 16 1769
耶瑟儿~
耶瑟儿~ 2020-11-22 05:32

What is a class, an object and an instance in Java?

16条回答
  •  情深已故
    2020-11-22 06:25

    I like Jesper's explanation in layman terms

    By improvising examples from Jesper's answer,

    class House {
    // blue print for House Objects
    }
    
    class Car {
    // blue print for Instances of Class Car 
    }
    
    House myHouse = new House();
    Car myCar = new Car();
    

    myHouse and myCar are objects

    myHouse is an instance of House (relates Object-myHouse to its Class-House) myCar is an instance of Car

    in short

    "myHouse is an instance of Class House" which is same as saying "myHouse is an Object of type House"

提交回复
热议问题