what is instantiation in object oriented programming code example
Example 1: What is instantiation OOP?
Instantiation means you creating an object of that class.
There are some cases where you don’t want that your class’ instance will be created.
Now, you may have doubt that Why do I don’t want to create an object of that class?
So here are some reasons:
A class contains only static methods or it is final. i.e. Math class in Java
A class contains an abstract method(s) which means implementation of the method
is not done. Now if you don’t have any implementation(logic) is done then there
is no meaning of using that method. So If you create an object and then call
that method. It is useless. That’s why it is not possible to instantiate an
abstract class.
Example 2: What is instantiation OOP?
It is the creation of a new instance of an object, with respect to OOP.
Suppose we have a programming class called “Dog”. To instantiate this class we create a new object of type Dog. We would do something like this:
// this is instantiation
Dog myDog = new Dog();
// set properties of the dog
myDog.setName(“Fido”);
myDog.setAge(3);
myDog.setBreed(“Airedale”);
// other instantiation methods, if the constructor is overloaded
Dog bigDog = new Dog(“Rover”, 4, “Great Dane”);