A subclass only inherits public and protected members of its parent class, and package-private members if the subclass is in the same package as the parent class is.
Fred Kleinschmidt wrote:No, a subclass inherits all members of its superclass. It is just that the private members are not directly accessible. For example, a JPanel inherits background from its superclass hierarchy (it is actually a member of Component). You cannot access background directly from a JPanel instance, but you can access it via the getBackground() and setBackground() methods.
Members of a class that are declared private are not inherited by subclasses of that class.
Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.
Ramsin Khoshaba wrote:My confusion arises because the way Java depicts OO is unnatural and difficult to grasp.
Fred Kleinschmidt wrote:This is really a nit about symantics for the word "inherit". I contend that when I call myPanel.setBackground() it sets the background member of Container.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
My confusion arises because the way Java depicts OO is unnatural and difficult to grasp.
I want to be comfortable with inheritance to the point where I have no trouble conceiving a more-or-less long hierarchy.
Basically, I want to be comfortable with OO.
I'm posting this because I want to immediately start programming Android apps, but I'm feeling very frustrated over these fundamental concepts.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Inheritance
In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
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.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass.
In all instances where there is a discrepancy, the JLS is definitive.Fred Kleinschmidt wrote:. . . the JLS and Oracle's Java Tutorials differ in what they say . . .
Why does such a concept have to have a real‑world counterpart? Why should any real‑world counterpart have to correspond exactly to Java®?Ramsin Khoshaba wrote:. . . What does membership, i.e. being a member, in Java exactly correspond to in the real world? . . .
Do you mean members or methods?I've had thoughts about whether private members should be only used for the internal implementation of an object; private members should not correspond to attributes of the object being modeled, . . .
Correct. Because the field i has package‑private access (=unofficial but useful name for default access), it cannot be inherited by a class in package b.Ramsin Khoshaba wrote:. . . If I have the following, . . . A.i is not visible, because i is not inherited by b.B and therefore i is not a member of C.
Please tell us where you got that “dictionary definition” from, so we can have a look at it and decide whether it is incorrect. A programming language has its own “dictionary definitions”, and in the case of Java®, those are in the Java® Language Specification (=JLS). If the definitions differ, then the JLS provides the “correct” definition, and you must disregard the other definitions.But (according to the above dictionary definition) i is a member of an instance of C . . .
Campbell Ritchie wrote:Please tell us where you got that “dictionary definition” from, so we can have a look at it and decide whether it is incorrect. A programming language has its own “dictionary definitions”, and in the case of Java®, those are in the Java® Language Specification (=JLS). If the definitions differ, then the JLS provides the “correct” definition, and you must disregard the other definitions.
No.Ramsin Khoshaba wrote:. . . should 'name' be declared protected, according to good design?!
Ramsin Khoshaba wrote:My professor has told me that everything is inherited, i.e. even private members of a superclass are members of its subclass!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There is nothing difficult about the Java® version of OO. There is something difficult about every version of OO, not only Java®. It is one of those things about programming you will have to learn. It may take a long time to learn but once learnt it will provide you with a valuable skill.Ramsin Khoshaba wrote:. . . Java made OO difficult and unnatural. . . .
No, the value of a class is a class. What you describe later is a variable, which should hold an instance of that class. Or more precisely a reference to an instance. Classes are called reference types and Java® doesn't have the concept that classes represent categories or sets, which some other languages, e.g. B, RVM_Forth, RuthR, do. You probably haven't heard of those languages.Ramsin Khoshaba wrote:Should I think of a class as a reference type, instead of as a set (or category)?
If yes, then a value of a class is a reference.
You cannot convert the types of objects. Once an object has a particular type, it retains that type for ever. But most objects have several types. Your Employee object is also a Person object and an Object object. If you make your class implement an interface, e.g. Comparable, then its instances are also Comparable objects. You can cast the reference to any type which the instance is already; if you try to cast to the wrong type you will get an error, maybe at compile‑time or maybe at runtime (in which case it is visible because you suffer a class cast exception).That is, a variable of type MyClass may store a value of type MyClass, or null. This value may be converted to a superclass type, and still reference an object of type MyClass.
What about static members?Instance attributes and actions that may be accessed through a value of class T are said to be members of class T.
That only applies to instance methods. You can hide static methods but not override them, and that can lead to a lot of confusion.An instance method m() visible to class T is a member of T. Overriding from subclass C the otherwise-inherited method m() gives rise to a new member method m() of class C. Overriding means replacing the implementation of an overridden method.
They do not exist in the subclass, so the compiler sees the methods you write as “new” methods.Only methods that would otherwise be inherited can be overridden.
I'm still not sure though why e.g. private methods or uninherited methods with default access cannot be overridden.
YesAm I on the right track?!
And good luck with the application.Sorry if I seem to be in a rush... I'm a bit perfectionist, plus I'm applying for a student part-time job.
Ramsin Khoshaba wrote:Should I think of a class as a reference type, instead of as a set (or category)?
I'm still not sure though why e.g. private methods or uninherited methods with default access cannot be overridden.
Sorry if I seem to be in a rush... I'm a bit perfectionist...
plus I'm applying for a student part-time job.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ramsin Khoshaba wrote:OBS! In Java's terms an instance is an object;
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:But in the other link, it says that fields are members of a class, not members of an object. That is different. The private name field is a member of the superclass only. You do not talk about members of an object. But there is a name field in there. It should be private to the Person class, which means that it is not directly accessible to the subclass (Employee) and is not a member of Employee.
Winston Gutkowski wrote:2. Inheritance is dependent on visibility. Again from the JLS:
"...A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class..." (my emphasis).
As Winston has already told you, obviously whoever teaches that sort of thing is mistaken.Ramsin Khoshaba wrote:. . . There are many teachers who teach member inheritance in terms of accessiblity: private members are inherited but not accessible to subclasses, for example. // . . .
Right. You have a package‑private field i. Remember that is different from a private field. As you correctly surmise, that is not a member of class B and is not inherited so you cannot find it, because B is in a different package. It would be much easier to understand if you gave “real” names to your classes and packages rather than one‑letter names. Even though your other two classes are in the same package, you are calling things outwith your current package, so i doesn't exist, so the compiler cannot find i and the code won't compile. Because i is not inherited by class B, it cannot appear in C. Try adding a constructor to B like this:-Again, that should fail to compile.
// . . .
Ramsin Khoshaba wrote:Whereas, conforming to JLS, A.i is not inherited in the first place, so it's not a member of C, even though it's a "member" of an object of type C. For some languages, such as PHP, it seems consistent to use the other model. Because PHP has no packages, and private methods are implicitly final anyway. I still don't understand the JLS.
Btw, thanks for your time...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Fun thread.
Oracle wrote:A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.
...
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
"Il y a peu de choses qui me soient impossibles..."
Campbell Ritchie wrote:The two tablets of stone have unfortunately been lost.
They had the ten commandments on one side and the SOLID principles on the other. O is the first thing you quoted. Before that you had named L. A lot of people no longer fully believe in O.
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:But, even allowing for the fact that we, as imperfect corporeal beings, do not create divine writ, I'd like to know what others have found useful as, say, the short list of guidelines.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't get me started about those stupid light bulbs. |