The difference between Classes, Objects, and Instances

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

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

16条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 06:34

    The concept behind classes and objects is to encapsulate logic into single programming unit. Classes are the blueprints of which objects are created.

    Here an example of a class representing a Car:

    public class Car {
    
        int currentSpeed;
        String name;
    
        public void accelerate() {  
        }
    
        public void park() {
        }
    
        public void printCurrentSpeed() {
        }
    }
    

    You can create instances of the object Car like this:

    Car audi = new Car();
    Car toyota = new Car();
    

    I have taken the example from this tutorial

提交回复
热议问题