Which members are not inherited in a child class?
Asked Answered
A

6

8

I'm trying to answer the following question:

A child class would not inherit certain members of the parent class. Name three such members.

I know private members are not inherited to child classes and default members are not inherited outside of the package. Can anyone complete the answer?

Edited:- I believe that static members are inherited according to below demonstration

public class sup {
    public static void main(String agr[]){
    }

    protected static int staticInt=0;
    protected final int finalInt=3;
    protected int protectedInt=0;
    public String publicString = "";
    private int privateInt=8;
}

class sub extends sup{
    public void del(){
        staticInt=1;
        staticInt=finalInt;
    }
}
Aguilera answered 16/4, 2014 at 8:5 Comment(0)
H
1

None-Answer to make a case for terms usage.

Members which are visible in the child class is answered above. Members being both fields and methods (each having its own namespace).

Inheritance as being part of the child instance, is another question: also invisible private members are "inherited" as such. Static members are part of the class instance and are not inherited (cannot be overriden too). All final methods cannot be overriden.

Arguable constructors are not inherited; you have to define the same signature again in a new child constructor.

Other declarations in a class could be class definitions. There the keyword static has a different meaning, and one may make obvious statements on visibility/inheritance. For instance with respect to non-static inner classes, which have an <outer-class>.this (recursive notion).

Huan answered 16/4, 2014 at 9:40 Comment(0)
S
6

from JLS for Class Member

Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

Stearne answered 16/4, 2014 at 8:27 Comment(2)
Ding ding ding! Finally, the correct answer and from the JLS too :-)Hambrick
@Duncan o darn! :) this is indeed the fully correct answer. how did i miss that... +1Antoine
M
3

from Oracle Java Documentation for Inheritance :

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass

So I think you're missing constructors here . Static Methods and fields are not inherited too, when they are rewritten in subclasses, they just reuse the signature and hide the implementation of the method/field in the parent class.

For Inheritance of static fields and methods, refer to this discussion as steted by Duncan, and this great tutorial Overriding vs Hiding

Magnet answered 16/4, 2014 at 8:16 Comment(2)
See this discussion too: Are static methods inherited in Java?. This disagrees with your final paragraph.Hambrick
@Duncan Thank you man !! I've just discovered the difference between Overriden and Hidden :)Magnet
A
1

Constructors and static initializers and instance initializers.

Antoine answered 16/4, 2014 at 8:7 Comment(9)
Can you explain why static members are not inherited? I guess it's a terminology thing, there are some subtle differences between static and instance methods as discussed here: docs.oracle.com/javase/tutorial/java/IandI/override.htmlHambrick
static members are inherit. I test it using sample programAguilera
Are you talking about visibility ? inheritance and visibility are two different things. static members may visible but can't override them.Poleyn
@Antoine I'm still not convinced that you can say static methods aren't inherited. Do you have any sources to cite where that is explicitly stated?Hambrick
@Duncan I don't get it. The tutorial clearly says that static methods are not inherited and they do hide. I don't think that's a JLS thing..Antoine
@Antoine There is no statement that I've seen that says "static methods are not inherited". I think you may be mistaken - see this question for example: Are static methods inherited in Java? and also mounaim's answer.Hambrick
@Duncan no, I'm not. I'm just saying that since they say that static methods hide the one in the superclass, it must mean that it is not inherited. I do agree that this is not stated per se; but it is auto-implied, at least for me.Antoine
@Antoine You seem to be thinking that inheritance is synonymous with the ability to override. I don't think that's correct.Hambrick
@Antoine See example 8.2-3 from the JLS (docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.2). This states that the static field is inherited.Hambrick
H
1

None-Answer to make a case for terms usage.

Members which are visible in the child class is answered above. Members being both fields and methods (each having its own namespace).

Inheritance as being part of the child instance, is another question: also invisible private members are "inherited" as such. Static members are part of the class instance and are not inherited (cannot be overriden too). All final methods cannot be overriden.

Arguable constructors are not inherited; you have to define the same signature again in a new child constructor.

Other declarations in a class could be class definitions. There the keyword static has a different meaning, and one may make obvious statements on visibility/inheritance. For instance with respect to non-static inner classes, which have an <outer-class>.this (recursive notion).

Huan answered 16/4, 2014 at 9:40 Comment(0)
D
0

you can't inherit a private field,and constructor. you can't inherit a constructor because they are not member of super class. you can invoke a super class constructor form it's sub class. and you can also access a private member of super class can be accessed through public or protected method of super classes.

Downstairs answered 16/4, 2014 at 8:47 Comment(1)
-1 Static members are inherited. See discussions under the other answers.Hambrick
M
0

Constructors, instance initializers, and static initialization blocks are not inherited by the child.

Additionally, some members are inherited but behave differently:

  • Private members are inherited, but can't be directly accessed or overridden
  • Final members are inherited, but can't be overridden
  • Static members are inherited, but if the member is rewritten in the subclass, the member will be hidden instead of overwritten, and it won't exhibit polymorphism.

Example:

class Parent {static String className = "Parent";}
class Child extends Parent {static String className = "Child";}
public class Main {
    public static void main(String[] args) {
        Child  c = new Child();
        System.out.println(c.className);
        Parent p = new Child();
        System.out.println(p.className);
    }
}

It will print "Child" and "Parent" even though both objects are members of the Child class.

Morice answered 12/8 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.