In object-oriented programming what is the relationship of a child with its parent called
DescriptionOne of the advantages of an Object-Oriented programming language is code reuse. There are two ways we can do code reuse either by the vimplementation of inheritance (IS-A relationship), or object composition (HAS-A relationship). Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition. Show IS-A Relationship: In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House. It is a key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship. HAS-A Relationship: Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom. Let’s understand these concepts with an example of Car class.
As shown above, Car class has a couple of instance variable and few methods. Maruti is a specific type of Car which extends Car class means Maruti IS-A Car.
Maruti class uses Engine object’s start() method via composition. We can say that Maruti class HAS-A Engine.
RelationsDemo class is making object of Maruti class and initialized it. Though Maruti class does not have setColor(), setMaxSpeed() and carInfo() methods still we can use it due to IS-A relationship of Maruti class with Car class.
If we run RelationsDemo class we can see output like below. Comparing Composition and Inheritance
Summary
Previous: Java Packages What is relationship in objectObject oriented programming generally support 4 types of relationships that are: inheritance , association, composition and aggregation. All these relationship is based on "is a" relationship, "has-a" relationship and "part-of" relationship. In this article we will understand all these relationships.
What is parent and child relationship in coding?It is best to think of the parent/child relationship in OOP as a concept of “inheritance” rather than a concept of containers and contained elements. When manipulating the DOM, a parent is one object, and a child is another object contained within.
What is parent and child class in OOP?Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
Why inheritance is called as is a relationship?An IS-A relationship is inheritance. The classes which inherit are known as sub classes or child classes. On the other hand, HAS-A relationship is composition. In OOP, IS-A relationship is completely inheritance. This means, that the child class is a type of parent class.
|