Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in line about the difference between instance and object in context of Java

Tags:

java

oop

Could anyone please what the following is saying about instance and object:

If class is the general representation of an object, an instance is its concrete representation.

I know concrete means non-abstract. So what's actually general representation and concrete representation?

like image 884
Dusk Avatar asked Dec 02 '22 07:12

Dusk


2 Answers

Car is a general representation having attributes (wheels, doors, colour, etc) and behaviour (start, stop, brake, accelerate, change gears, etc), also called a class.

Bob's Ford Focus (red, license plate LH 12 233) is an instance of class Car, also called an object.

like image 173
cletus Avatar answered Dec 31 '22 02:12

cletus


My best advise would be to drop the dictionary.. looking up what concrete means and than trying to apply the definition to understanding what an author meant when he or she used concrete representation to describe an instance of an object is just wrong.

Look for other explanations of what objects, classes and instances of objects are, and I'm sure you'll find many with great examples.

Basically you could think of the class as a "recipe" or as a "template" (although I'm reluctant to say template for fear of causing confusion) and an instance as an "embodiment" of said recipe or template.. hence the concrete representation.

So you have the following, which is a class (the recipe):

class Human
{
  private string Name;
  private int Age;

  public void SayHello()
  {
      // run some code to say hello
  }

  public Human(string name, int age)
  {
     Name = name;
     Age = age;
  }
}

And these are instances (objects)..

 Human mike = new Human("Mike", 28);
 Human jane = new Human("Jane", 20);
 Human adam = new Human("Adam", 18);

They are embodiments, or concrete representations of our Human class.

like image 27
Mike Dinescu Avatar answered Dec 31 '22 01:12

Mike Dinescu