+ 4

what makes a difference between extending a class and making an object of class and applying it

3rd Mar 2019, 12:11 PM
Rajb957
Rajb957 - avatar
3 odpowiedzi
+ 6
Extending a class to another with the keyword extends is called inheritance Making an object of an existing class with the keyword new is called instantiation. Classes are the blueprints and objects are the results or instances of a class. In inheritance we apply the properties of a class to another class. The extended class is called the super class and the applied 1 is known as the sub class. A good example to explain inheritance is "us". Suppose that our parents are the super class and you are the subclass. You get all of the properties of your parents like complexion, eye colour etc. Hope you get it. A good example to understand instantiation is making cars from a blue print. We have 1 blueprint, so we can result many cars. Here the blueprint is the class and cars the objects.
3rd Mar 2019, 12:33 PM
Seniru
Seniru - avatar
+ 3
To put them both into the same example, let's say you have: class Truck extends Car. Both of them are still blueprints, and they're both going to have the same methods and properties as a car, it's just that Truck is going to have a few different methods and properties. Rather than rewriting the Truck class from scratch and repeating all of the information that's in the Car class, we just extend Car to Truck, so it inherits all of the those methods and properties. Now let's say we actually want to build a truck. We create an object of the class Truck in order to assign it specific values for the properties and methods. So the Truck class may have a property called color, but for the Truck blueprint, it doesn't matter what color it is, we just need to know that it has one. When we create the object, we're assigning the specific values that that object needs or works with. So for our new Truck t object we will set the String color = 'red'; because we're building an actual Truck, not just a blueprint for one.
7th Mar 2019, 6:41 PM
Adam Neidlinger
+ 2
Thank you it's very helpful
6th Mar 2019, 8:38 AM
Rajb957
Rajb957 - avatar