简体   繁体   中英

Java Inheritance hierarchy

Rectangle is Parent class and Square is the child class of rectangle(Parent). Is this relationship correct?

It is obvious that square is more specific in front of rectangle and it can be put in a IS-A relationship with rectangle. But if we see in this way the memory utilization will be more( while creating the object of square in heap both instance variables length and breath will be allocated memory separately like rectangle).

Whereas we can reduce it to half by declaring only one variable in Square class by putting square in parallel to rectangle. So while creating the object of square in heap only one instance variable will be allocated memory.

So which approach will be more efficient?

  1. By putting Square as a child of rectangle.

     Or 
  2. By Putting Square at a parallel branch in inheritance hierarchy in view of effective heap memory utilization?

Please correct me if I am wrong at any point..

Inheritance is best used to express an "is a" relationship between objects.

Here is a bad use of inheritance just for example:

public class List<E> {
    :
}

public class Vector<E> extends List<E> {
    :
}

You might ask, "if Vector uses all the functionality of List, then why is this a bad example of inheritance?" This is suboptimal because a Vector isn't a List. One consequence might be that the List class could evolve in a way that was not important to Vectors or, worse, which creates security vulnerabilities or flaws in Vectors. In a situation such as this, composition would be much better, ie a Vector "has a" List (rather than forcing a Vector to be a List).

A child class really should be an example of its parent (eg. A MountainBike is a Bicycle). If it isn't then it is quite probably true that the relationship is not one which should be expressed by an inheritance hierarchy.

But if we see in this way the memory utilization will be more( while creating the object of square in heap both instance variables length and breath will be allocated memory separately like rectangle

Hierarchies are a tool to encourage code reuse and improve modularity. If, at this stage of a project, you are worried about memory allocation then you should choose a different language than Java. You use Java because you want to gain the design improvements related to abstraction and object orientation.

You can't be sure that is-a approach to design is obvious choice here. You could instead have a Rectangle as base and it will be a square when the width equals height. Although a Square is-a Rectangle, you can model it as with Rectangle attributes too.

But I agree that conceptually it would seem better to model a Square as extending a Rectangle .

Your question about reducing space complexity

This hierarchy is simple right now so you are asking that question but suppose you are building Rectangle with operations that take both width and height. These operations will also be valid to a Square . Ex:

public int calculateArea(int width, int height);

You will likely use the same method if you are extending Square from a Rectangle . Now, to perform this operation, you have to initialize the width and height through a constructor. All you do is set these to be the same and you can get the area. You don't have to override it for any reason. But if Square is separate then how will you have the area calculation behavior? Would you have it as separately inserted just for the space complexity agnostic design considerations.

Everything that is built around width and height can be equally leveraged by a Square if you extend it.

The space saving approach you suggested discards the reusability of your code. That's why, it is preferable to extend Square rather than saving that memory.

So which approach will be more efficient?

 By putting Square as a child of rectangle. Or By Putting Square at a parallel branch in inheritance hierarchy in view of effective heap memory utilization? 

Your efficiency comes at a big price of losing code re-usability. So, answer is, " By putting Square as a child of rectangle. "

But wait...You may be able to save memory too!

If both the width and height are not primitives then references like Width and Height can be made to point to the same object instead of 2 different ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM