File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes khalid mughal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "khalid mughal" Watch "khalid mughal" New topic
Author

khalid mughal

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
Which of the following statements are true?
a) In java the extends clause is used to specify inheritance.
b) The subclass of a non-abstract class can be declared abstract
c) all the members of the superclass are inherited by the subclass .
d) A final class can be abstract.
e) A class in which all the members are declared private cannot be declared public.
The correct answers given are a,b,c . Can any body can explain why c is correct? private members are not inherited.
Swati Jambhale
Greenhorn

Joined: Jan 26, 2001
Posts: 22
Hello
All memebers are inherited by subclass. private members are only accessible from the class in which it is declared so they are inaccessible from subclasses .
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
So that's what i am saying private members are not inherited. So all members of subclasses are not inherited.So answer c is wrong.Correct me I am wrong
duchang
Greenhorn

Joined: Feb 18, 2001
Posts: 3
All members of a parent class is inherited by the subclass. The only reason why the subclass can't access certain inherited members is due to the access controls imposed on the parent class. The inability to access members of the parent class does not imply that the subclass did not inherit all parent members
assume the following...(each class definition belongs to its respective .java file)
public BaseClass {
private int m_Number = 0;

public BaseClass() {
m_Number = 10;
}
public int getNumber() {
return m_Number;
}
}

public SubClass extends BaseClass {
public SubClass() {
super();
}
public static void main(String[] args) {
SubClass sc = new SubClass();
System.out.println("num="+ sc.getNumber());
}
}

///////// output
num=10
Hence, this proves that all members of parent class is passed on to sub classes.
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100

" A class in which all the members are declared private cannot be declared public. "
Can someone elaborate on this PLEASE ??? Maybe some code examples ??
Thanks in advance.
Wasim Ahmed
Ranch Hand

Joined: Jan 31, 2001
Posts: 90
Zahid, the statement is wrong. You can declare class as public even all members are private.
Consider the following example.
public class Aclass
{
private int i =0;
private String k ="Hello Zahid";
private Aclass()
{
System.out.println("i =" + ++i);
System.out.println(k);
}
public static void main( String argv[] ){
Aclass a = new Aclass();
}

}
Output is i=1
Hello Zahid
Rajeshwari, Ramamurthi
Greenhorn

Joined: Feb 21, 2001
Posts: 7
Hi
The accessibility of the members does not affect the accessibility of the class.
This works if you have static inner classe with outer class having all members as private.
In the example given below myInt is the only member variable and it is private. So members of class A are not accessible from outside. But the inner class B can access private variable of class A
public class A{
private int myInt=10;
public B{
public void method1(){
System.out.println(myInt);
}
}
}

I assume inner classes are different from members
correct me if I am wrong
Raji
Originally posted by Zahid, Butt:

" A class in which all the members are declared private cannot be declared public. "
Can someone elaborate on this PLEASE ??? Maybe some code examples ??
Thanks in advance.

Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi
Then answer e should also be true.
Therefore the answers are : a,b,c AND e.
Can someone confirm this.
Thanks in advance.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
answer e is wrong, u just try write down a little and then see wheteher it run's or not
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Zahid,
e) is wrong. Wasim showed this in the current thread.
A class in which all the members are declared private CAN be declared public.
I played a little around with Wasims code. I made some minor changes. You can define public get and set method for the private member variable. The member Variables (and the getXXX and setXXX) are inherited. So you can even change the value of the private member variable inherited from the superclass with the public set XXX() and getXXX() inherited from the superclass.
class BaseClass {
private int m_Number = 0;
public BaseClass() {
m_Number = 10;
}
public int getNumber() {
return m_Number;
}
public void setNumber(int jupp) {
m_Number = jupp;
}
}
public class SubClass extends BaseClass {
public SubClass() {
super();
}
public static void main(String [] args) {
SubClass sc = new SubClass();
sc.setNumber (4);
System.out.println("num=" + sc.getNumber());
}
}

am i right?
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Hi guys...,
is it true that a constructor is also a member of a class ??
thank's
stevie
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi all,
Just want to point out that

  1. private members are not inherited by their subclasses
  2. constructors are not members

  3. See JLS §6.4.2

    6.4.2 The Members of a Class Type
    The members of a class type (�8.2) are classes (�8.5, �9.5),
    interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods
    (�8.4, �9.4). Members are either declared in the type, or
    inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.6).

    Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Sorry I overlooked the word " NOT " in statement e.
I know understand exactly why its wrong.
Thanks everyone.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hello Jane Grisci,
still have some doubt about
"private members are not inherited by their subclasses"
In my code above I clearly can get and set the private member m_number from the Subclass-object. So it must be in the object. It must be inherited.
-- main method---
SubClass sc = new SubClass();
sc.setNumber (4);
System.out.println("num=" + sc.getNumber());
-- main method ---
I don�t really know about such internals, but when you create the subclass object there must be memory space alocated for the private member variable m_number.
I apreciate every coment

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
As Jane said:
From the JLS 8.2 Class Members

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.

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21831
When you CONSTRUCT an instance of a subclass, you first construct each of it's superclasses (including it's private variables). However that is NOT the same as inheriting. If it were inherited you would not need to use a method of the superclass to get at the variable.
I can create an instance of the superclass in ANY class (including non-child classes) and use the getters and setters to use the private variables, that doesn't mean that I inherited them.
[This message has been edited by Cindy Glass (edited March 15, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Om Sonie
Ranch Hand

Joined: Feb 04, 2001
Posts: 30
So the answer c is wrong?
Om
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
C is wrong.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I agree with Cindy and with the "standard" way of interpreting 'private'. Over a year ago I wrote to Rasmussen and what he meant by 'inherited' is that the value was in memory somewhere and indirectly accessible.
For me it's inherited only if you can override it (method) or hide it (variable) in a subclass.
Simple?

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Hi Tony & Cindy...,
If final method can not be override,
then why if i give them "private" access modifier :
private final aMethod(){}
I still can make a "NEW" method with the same signature (in sub-class) ??
note that i'm using "NEW" instead of "override", in this state I only create a new method with the same signature.
but the soul is why "private" can change the behavior of final method ???
thank's in advance
stevie
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Stevie,
The JLS says final methods cannot be overridden. Because this is a language rule the compiler doesn't bother to check the superclass for final methods that have the same signature as a subclass method; it just ignores them.
However, when a method is not final the compiler specifically checks the superclass method signatures ... if it finds one that is the same it checks to make sure the overriding rules are not being broken.
The compiler in a sense creates a lookup table of the methods in the superclass ignoring any that are 'private' because the rules state these aren't inherited, and ignoring any that are 'final' because the rules state that these can't be overridden. Then, as each subclass method is compiled it checks the lookup table for the superclass methods and applies the overriding rules if it finds a superclass method with the same signature of the subclass method.
So, in your example, a <code>private final</code> method would not be in the superclass lookup table and the compiler would consider a method with the same signature in a subclass to be legal.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited March 15, 2001).]
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
ask james gosling of sun,the best person to clear your doubt's , because in this group sun certified say's private memeber's not inherited whereas khalid mughal say's they are inherited,person with a lot of experience.
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Thank's Jane...,
it's clear now.
regards
stevie
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
so at the end it means only a & b are true. Plaese correct me if i am wrong
Naveen Arumugam
Greenhorn

Joined: Mar 10, 2001
Posts: 23
Hi All
This is from Sun Tutorial:
----------------------Tutorial snippet--------------------------
Understanding Inheritance
A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these members as is, or it can hide the
member variables or override the methods.
What Members Does a Subclass Inherit?

Rule: A subclass inherits all of the members in its superclass that are accessible to that subclass unless the subclass explicitly hides a
member variable or overrides a method. Note that constructors are not members and are not inherited by subclasses.

The following list itemizes the members that are inherited by a subclass:
Subclasses inherit those superclass members declared as public or protected.
Subclasses inherit those superclass members declared with no access specifier as long as the subclass is in the same package as the superclass.
Subclasses don't inherit a superclass's member if the subclass declares a member with the same name. In the case of member variables, the
member variable in the subclass hides the one in the superclass. In the case of methods, the method in the subclass overrides the one in the
superclass.
-----------Tutorial ends-------------------------------------
I think that private members are not inherited.
Rgdsd

ANaveenS
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
thanks to all, especially Cindy and Jane.
Now I have a much clearer concept...
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi All
Firstly, I think this discussion is doing everyone a great deal of GOOD.
In you post earlier you stated a Tutorial snippet which contained the following statement:
" A subclass inherits all of the members in its superclass that are accessible to that subclass unless the subclass explicitly hides a member variable or overrides a method. "
I do not understand what is meant by :
" unless the subclass explicitly hides a member variable "
Can someone please explain.
Thanks in advance.
Mafalda Alabort
Greenhorn

Joined: Mar 02, 2001
Posts: 24

Hi all,
and what about static members? are they inherited?
Thanks
Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
Hi ALL
Good point Mafalda, I Think they are NOT inherited as they are specific to a Class, and only one instance of them exist once that class is instantiated.
Could someone correct me if I am wrong.
Thanks in advance.
Pam Doucette
Greenhorn

Joined: Mar 13, 2001
Posts: 27
I believe static member variables are inherited if they have an appropriate access modifier (protected, public or (default)). Any objects instantiated from the subclass could access the variable through a reference to the subclass name.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi again.
Pam is right. Static member variables are inherited (see example below).
To add some confusion to this thread:
Mughal/Rasmusen state in page 181 that "A subclass canNOT override variable members, but it can SHADOW them".
Shadowing seems to me very similar to overriding.
If you define a member variable with the same name as in the superclass the member in the superclass cannot be accessed directly by the subclass but with super (seems to work only in methods).
Methods and member variables really behave diferently when overriden/shadowed.
If you asign a superclass object declaration with a subclass object and you access the member variable you get the member variable of the SUPERCLASS. If you call a overriden method the object takes the method of the SUBCLASS. This has to do with Dynamic Method Lookup.
Here is an example (because I am still quite green concerning oo-theory I am using examples):
lass BaseClass2 {
static int s_Number = 3; // static member variables can be inherited
public String whoAmI = "I am the base member variable";
protected String aMethod (){
return "I am the Method from Base";
}
}

public class SubClass2 extends BaseClass2 {
public String whoAmI = "I am the sub member variable";
private String getWhoAmISuper() {
return super.whoAmI;
}
public static void main(String [] args) {
SubClass2 sc = new SubClass2();
// inherited from BaseClass2
System.out.println("s_number=" + SubClass2.s_Number);
//prints s_number=3 ---> inherited from Base
// --- shadowing -----
System.out.println("whoAmI= " + sc.whoAmI);
// prints whoAmI = I am the sub member variable ---> variable from Base shadowed
//accessing shadowed member variables
//System.out.println("whoAmI= " + sc.super.whoAmI); // does not compile
System.out.println ("whoAmI= " + sc.getWhoAmISuper());
//prints "whoAmI = I am the base member variable--> so you can access shadowed member
System.out.println ("***** new object *****");
// new object watch type and constructor
BaseClass2 somewhatMixed = new SubClass2();
System.out.println(somewhatMixed.whoAmI);
// prints I am the base member variable ---> the Type of the Object as declared
System.out.println(somewhatMixed.aMethod());
// prints I am a Method from Sub ---> the Type of the Object as constructed
}
protected String aMethod (){
return "I am the Method from Sub";
}
}
really like this thread
Axel

Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
it seems to me that there are two definitions about overriding.
One states that you override methods. And you cannot override member variables. You shadow member variables.
The other states that you override both.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Hiding has to do with Class to Class access to members. If a member would have been inherited or accessible from a subclass, but that subclass declares a member with the same name (and in the case of methods, the same signature), the access to the superclass in interferred with, it is called hidden. As such overriding is a form of hiding.
Shadowing has to do with scope within a class. If a variable is declared as a member variable and then the SAME name is used in a local scope, the local variable shadows the member variable within that scope (like inside a method).

Obscuring has to do with namespaces. If the same name is used for a variable and for a class which is then used as the TYPE for a variable and for a package it becomes unclear which is intended when using that name. The rules are that in such obscure situations the system will make the presumption in the following order: variable->type->package.
[This message has been edited by Cindy Glass (edited March 16, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi,
There are a couple of articles on Sun's Site that might help:
Shadowing and
Overload Resolution

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
So, that for short if I can access the superclass's member via this, that means the member is inherited.
For hiding, this.member returns the member defined in current class -> no inheritance.
For overriding, this.method is calling the method defined in current class -> no inheritance.
For access modifier as private, call this.member will lead to compilation error -> no inheritance.
Am I right?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Sounds right to me Wong
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: khalid mughal