Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which members are not inherited in a child class?

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;
    }
}
like image 487
Roledenez Avatar asked Apr 16 '14 08:04

Roledenez


People also ask

Which members of a class Cannot be inherited?

Explanation: Private members of a class can't be inherited. These members can only be accessible from members of its own class only.

What is inherited by child class?

Inheritance allows us to define a class that inherits all the methods and properties from another class. 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.

What is not inherited from base class?

Not all members of a base class are inherited by derived classes. The following members are not inherited: Static constructors, which initialize the static data of a class. Instance constructors, which you call to create a new instance of the class.

What is not inherited?

noninherited (not comparable) (genetics) Not inherited; not passed from parent to offspring.


4 Answers

from JLS for Class Member

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

like image 108
user2758757 Avatar answered Oct 14 '22 02:10

user2758757


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

like image 21
mounaim Avatar answered Oct 14 '22 02:10

mounaim


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).

like image 41
Joop Eggen Avatar answered Oct 14 '22 02:10

Joop Eggen


Constructors and static initializers and instance initializers.

like image 1
Eugene Avatar answered Oct 14 '22 03:10

Eugene