Kratos
Kratos

Reputation: 937

Confusion about class definitions

I am a little bit confused with the next terms. I thing I got it right but programmers all the time say it wrong so I want to be sure.

If we have class A and class B is inner class of class A. I hear a lot of programmers say "class A is parent of class B". For me this is wrong. Class A is outer class of class B. Class A would be parent class only if the class B inherits class A.

This is very confusing because (since I am Android developer) people also use this terms in defining layouts where they say:

Imageview, TextView and Edittext are children of ConstraintLayout and ConstraintLayout is their parent. Even though I agree with this sentence it is contradictory to the previous example with classes. If we follow previous example we should say: ConstraintLayout is outer element and these 3 are its inner elements.

Please can someone share opinion with me or confirm my doubts. Thanks

Layout example in android:

<androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".presentation.main.MainActivity">

     <ProgressBar
        android:id="@+id/progressBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

  <TextView
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:ignore="RtlSymmetry" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 2

Views: 127

Answers (3)

TeWu
TeWu

Reputation: 6536

When we have a class definition like this:

class A extends B {
    static class X {}
    class Y {}
}

Then, following standard terminology [1,2], you can say that:

  • Class A is a subclass of B
  • Class B is a superclass of A
  • Classes X and Y are nested classes of A
  • Class A is enclosing class of X and Y
  • Class X is static nested class of A
  • Class Y is inner class of A

According to Java Documentation, term "parent class" is synonymous to "superclass". In practice though, it is used to mean many different things (e.g. "enclosing class", or "class of a parent field"). Natural language is a messy thing. People use different names to call the same thing, sometimes even when there is standard generally accepted terminology in place. So the only thing you can do, is to be aware that "parent class", depending on a context can sometimes mean "superclass", sometimes "enclosing class", and sometimes something else. If you want to be unambiguous always use term "superclass" instead of "parent class".

Upvotes: 2

MC Emperor
MC Emperor

Reputation: 22977

Some things need to be cleared up:

In the context of classes: suppose you have class A extending class B. Class A is a superclass of class B and B is a subclass of A. Calling superclasses "parent classes" is much less used, but still accurate.

An inner class B of the outer class A is not a child class, as there is no hierarchical relation between those two.

In the context of instances, however, the story is different. If you have a List<List<?>> is List a parent class of itself? No.

But suppose you have a

class Node {
    List<Node> children;

    void addChild(Node node) {
        children.add(node);
    }
}
Node a = new Node();
Node b = new Node();
a.addChild(b);

Now it makes sense to call a the parent node of b. But this example is not in class context, it's instance context.

Those are two different contexts, and one should not mangle them.

Upvotes: 2

Christopher Schneider
Christopher Schneider

Reputation: 3915

Imageview, TextView and Edittext are children of ConstraintLayout and ConstraintLayout is their parent. Even though I agree with this sentence it is contradictory to the previous example with classes. If we follow previous example we should say: ConstraintLayout is outer element and these 3 are its inner elements.

You are thinking that parent/child is terminology exclusively used when talking about classes. That is incorrect. Many programming languages don't even have classes.

What you're describing with the UI elements is more akin to a tree data structure. In a tree, there are parent and child nodes. So ProgressBar is a child of ConstraintLayout in the tree. I don't know the history behind it, but I'd bet a tree data structure was thought of before classes even existed.

In the context of Java classes, there is also a parent (superclass) or child (subclass) relationship. In this case: class B extends A, B is the child class, and A is the parent.

Inner classes are a completely different thing. An inner class is not a child of its enclosing class and doesn't seem relevant to this discussion. Anyone calling an inner class a child is using wrong terminology. I personally have never heard them referred to as child classes of their enclosing class.

Upvotes: 1

Related Questions