This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance of all members

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch.

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
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods and private methods are inherited.
reply
    Bookmark Topic Watch Topic
  • New Topic