• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on inheritence

 
Jitender kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is inheritence of a class confined only to the non-static members functions of a class or static functions too?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static members are inherited, but static methods cannot be overridden.

You should write some code to test this.
 
Jitender kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not find this statement in K&B book and so, I thought I would point out. Sorry, if I overlooked into the book.
 
chandu sekhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jitender,

for more clarification, a small example :

package mypackage;

class Parent
{
//Parent(String str){}
public static void XYZ(){ System.out.println("Parent XYZ");}

}

public class Child extends Parent
{
public static void XYZ(){System.out.println("Child XYZ");}
public static void main(String[] args)
{
Parent ch = new Child();
ch.XYZ(); // Special Line
}

}

if u change the statement at special line

1. Parent ch = new Child();
ch.XYZ(); // Special Line

Output is ----> Parent XYZ

2. Parent ch = new Child();
Parent.XYZ(); // Special Line

Output is -----> Parent XYZ

3. Child ch = new Child();
Child.XYZ(); // Special Line

Output is -----> Child XYZ

try some more combinations, so that u can understand better. hope it helps.


Chandu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic