Hi all,

What is the difference in inheriting a class and instantiating a class, as both allows to inherit properties from the base class.I know there is a difference in both, but i want a clear idea on this. Kindly, can anyone help.Thanks in advance.

Recommended Answers

All 8 Replies

You inherit from a superclass when defining a subclass with the extends keyword. You instantiate an instance of a class at runtime when you use the new operator.

Let me give u an eg.
Suppose an architect want to design a building for his client based on one of his previous design(inheritance).He studies blueprint of the old design and creates the new design by making minor changes to the old one.This is called inhereting

When he actually makes the building based out of his new design thats whats called instantiating.

ishaan: you're explanation is either too vague, or incorrect. the way you explain it, building that first building would not be instantiating.

I beg to differ. I think that's a really good analogy. I'll use it myself in future.

even a class that is inheriting still needs to be instantiated. so they're both instantiations.

to me, it sounds as if the first example is not instantiating, only the second one, while both 'buildings' are 'instances'.

I read it as:
A blueprint is like a class definition. Creating a new blueprint by taking an existing one and modifying it is like creating a new class definition that inherits from the existing one.
Reading either blueprint and making a building as described by the blueprint is like instantiating an instance of a class.

The blueprints aren't instantiated because a blueprint is a design for a building not the building itself.

Just in case this analogy has gone too far;

Java attempts to mimic the real world using objects e.g. Cars

A class is a description of a car. Is defines everything about it.
When you 'instantiate' (create) this Car object, you use the class to determine what the car looks like, how it behaves etc.

Inheritance is used when you have a group of things that share some common attributes e.g. wheels, doors, engine etc.

Quick note, these values do not have to be the same for every car, they simply have to be present. SO a car can have 4 wheels or 6 wheels, as long as it has wheels

The key to inheritance is there are lots of cars, but there are also different TYPES of car e.g. sports car, 4x4, pickup. Each of these cars has certain attributes that other cars don't have e.g. a 4x4 may have heavy duty tyres.

Inheritance allows us to treat every Car as a Car whilst still maintaining the different types of Car.

Simplest way to explain is: A Parent class has a series of common attributes and methods found in every Child class. Each Child class has access to these attributes and methods, but also has some custom methods of its own that no-one else knows about.

Hope this helps

I don't know if anyone else noticed, but this question was asked just the other day. Please double check for a thread asking the same thing before posting a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.