aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance of all members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance of all members" Watch "Inheritance of all members" New topic
Author

Inheritance of all members

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
If I am not wrong, then
***** all the members in a super class are NOT inherited by the subclass. *****
In case of private members, they are inherited by the sub class, but are not accessible.
In case of static members, they are NOT inherited as they belong to the super class.
Rest of the members are inherited by the subclass.

Considering the above statements, KAM Page number 178 Qestion number 6.1
Which of the following statements are true?
Select all valid answers:
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 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.

I will vote for only options a and b. But the book's answer says that a, b, c are valid options.

How will you people deal with this question??
Bye,
Tualha Khan


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Hi,
All members of a superclass are inherited by the subclass. The private members (variables and methods) are not just accessible from the subclass. This is an example of a class not able to access its own varibles and methods.
The foll.code will help u i think
class A{
int i;
A(){
i=5;
}
private void geti(){
System.out.println(i);
}
void seti(int j){
this.i=j;
geti();
}

}

class B extends A{
}


class subprivate{
public static void main(String[] args){

B b = new B();
b.seti(7);
}

}


thanks.


Sun Certified Programmer for Java 2 Platform
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
That's OK!!
But are static members of a class not a part of the class?!? They are ,So they should also be inherited, but they are not because they arepart of the class as a whole.
I wish to know what happens to the static members in this story!!!
Bye,
Tualha Khan
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
Folks,
My last shot on this topic:

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

Excerpt from JLS (Java Language Specifications from SUN]
LHS


Hungson Le<BR>
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
it doesn't matter how many shots you take on any topic, hungson if you can't see the question first.
static methods are inherited too, tualha. the static methods just don't participate in run-time polymorphism.
see the code below and compile it and see for yourself-
//code
class b{
static String r = "It sure does!";
}
class a extends b{
public static void main(String args[]){
System.out.println(new a().r);
System.out.println(a.r);
}
}
//code ends
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
Anshuman ,
You're right. I didn't pay attention to what you said
Please look at a similar hot topic:
"Can static methods be inherited?" - stater Judy Yu
You said "private methods are inherited", which is absolutely wrong.
Please look at the JLS from Sun, private methods are not inherited,
but included in the subclass.
If you trust the bartender here, Jane Griscti did say that

basically, as 'static' methods are not inherited

I totally agree with her.
This is the shot after my last shot =)
LHS
[This message has been edited by Hungson Le (edited February 02, 2001).]
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Static methods and private methods are inherited.
 
 
subject: Inheritance of all members