Dusk
Dusk

Reputation: 2201

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

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?

Upvotes: 4

Views: 2263

Answers (5)

Dangerosking
Dangerosking

Reputation: 438

Classes are in a way, the object's templates while an instances of a classes are the objects themseles. Objects are defined by their type and are 'built' using that template, what objects are, their properties and methods and all of their attributes depend on that template. Think of the classes as "molds", and objects as what comes out of those molds.

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55720

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.

Upvotes: 6

Ben
Ben

Reputation: 2801

In Java Context :

Object: That is the Class Instance: The thing created when you use the class.

EX: (to use the above car example) In the below example "Car" is the Object and myInstanceOfCar is the Instance.

class Car
  private String color;

  public static void main(String[] args)
  {

    Car myInstanceOfCar = new Car();
  }
}

Upvotes: 1

cletus
cletus

Reputation: 625037

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.

Upvotes: 10

Carl Manaster
Carl Manaster

Reputation: 40336

"General" means, "describes what things of this sort are like; what qualities they share." "Concrete" means, "what is particular to this one; what differentiates it from others of its type."

Upvotes: 1

Related Questions