Give an example of a class and an example of an object. Describe what a class is, what an object is, and how they are related. Use your examples to illustrate the descriptions.
Class example:
public class Cat{
private String name;
public Cat(String name){
this.name = name;
}
public void showCatName(){
System.out.println("Cat name is: " + name);
}
}
Object example:
Cat myCat = new Cat("Tommy");
A class is a description of an entity using a programming language. In order to use the functionality of a class (most often), you need to create its object. An object is a data type that allows you to use the functionality described in the classes. The relationship between a class and an object can be described as: the object is an instance of the class.
Comments
Leave a comment