-
September 8th, 2009, 12:31 PM
#1
Inheritance
it may seem to be a little bit foolish,,,and i m quiet known about inheritance basics that a superclass can't access its sublass members,,,
but what if i declare any member of a subclass as public even then im not able to access them,,,,and according to public definition it can be called from any code even from outside...
so do the "inheritance" and "public" concepts contradict each other?????
-
September 8th, 2009, 04:54 PM
#2
Re: Inheritance
It's not really clear exactly what you're asking. If you post some example code that you have trouble understanding, maybe we can help you understand it.
It is easier to measure something than to understand what you have measured...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 8th, 2009, 06:50 PM
#3
Re: Inheritance
Ehh you're kind of mixing two different concepts.
Think of inheritance as you would in real life. Children inherit from their parents, and the children's parents inherit from their parents. Parents don't inherit from their children and grandparents don't inherit from their grandchildren.
You are right about public functions, and them being able to be accessed from anywhere including outside of the code. But when you extend class B to class A (ie: class B extends A) you are letting B become the child and A the parent.
Then, your object B inherits all of the functions from class A. Class A has no idea that it has children (must be nice), and since it doesn't know about it's children, then it can't really access them.
However, if you had a static function in B, then you could call it from A. Having a static function applies to the class though and an object. Example:
Code:
class A{
public A(){
B.display();
}
}
class B extends A{
public B(){
super();
}
public static void display(){
System.out.println("Calling class B");
}
}
public class inheritance{
public static void main(String[] args){
A test = new B();
}
}
Hopefully this [sort of] helps!
-
September 15th, 2009, 04:02 PM
#4
Re: Inheritance
in your reply ,i don't think that there is any need to create object of subcls,,,and why hav u used super() here??????
-
September 16th, 2009, 08:43 AM
#5
Re: Inheritance
You always have there, it was just explicitly typed.
Think about it though: there are many times where you want to know or use the child object, and not the parent type.
Let's think about a heirarchy such as a house, where you have a device class, and subclasses of thermostat, refrigerator, oven, hot water tank, lightswitch.
Can you really control all of them the same way? Well, all of them certainly have an on and off function, and a few other things they have in common, such as how much power they are drawing for example, etc, but there is a lot to be gained from creating objects and using subclass objects rather than just dealing with them all in terms of a device where you only know on/off but maybe you don't know how to increase temperature +2 degrees.
Is that helping you understand this a bit more?
------
If you are satisfied with the responses, add to the user's rep!
-
September 18th, 2009, 01:28 PM
#6
-
September 19th, 2009, 10:30 AM
#7
Re: Inheritance
Originally Posted by arpit2309
it may seem to be a little bit foolish,,,and i m quiet known about inheritance basics that a superclass can't access its sublass members,,,
but what if i declare any member of a subclass as public even then im not able to access them,,,,and according to public definition it can be called from any code even from outside...
so do the "inheritance" and "public" concepts contradict each other?????
A superclass can in fact access subclass members. This is because a superclass can force a subclass to implement a member by declaring it abstract, like
Code:
public abstract class Super {
public void m1() {
m2(); // calls method that must be implemented in subclass
}
public abstract void m2(); // abstract so it must be implemented by any subclass
}
//
public class Sub extends Super {
public void m2() { // must be implemented here because abstract in superclass
}
}
Last edited by nuzzle; September 19th, 2009 at 10:34 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|