Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Inheritance

Cindy_PaulMay 3 2016 — edited May 3 2016

class A

{

int x;

int y;

void test1()

{

...

}

}

class B extends A

{

void test2()

{

.......

}

}

class C extends B

{

C o = new C();

o.x;

void test3()

{

....

}

}

My question is ..

|)" new C()" will create a new object of type C  on the space in JVM called heap .. this object on the heap will have :

1)int x, int y

2)test2()

3)test3()

OR

only test3()

===========================================

||) o.test2() .. this has the reference o calling the test2() method .. so will it be calling from the object of class B sitting on the JVM heap or it will be calling from the class C object which is in heap.

Comments

Jiri.Machotka-Oracle

An object of the class C will have all attributes and methods of itself as well as of all its predecessors:

attr:

int x, int y from A

methods:

void test1() from A, void test2 from B, void test3() from C

If you want to disable inheritance, you may declare both the attributes and the methods as private.

Yet another important feature might be polymorphism - if you define a new method with the same signature - e.g. void test2() in C, this method (and not the method from the superclass) will be called, even though you may still use the keyword super to re-direct the call.

TPD-Opitz

Cindy_Paul wrote:

class C extends B

{

C o = new C();

// ...

}

My question is ..

|)" new C()" will create a new object of type C  on the space in JVM called heap ..

Since this code is somewhat recursive this will create many instances of type C in the heap untill you PC runs out of memory...

Cindy_Paul wrote:

this object on the heap will have :

1)int x, int y

2)test2()

3)test3()

Yes

Cindy_Paul wrote:

OR

only test3()

No

Cindy_Paul wrote:

============

||) o.test2() .. this has the reference o calling the test2() method .. so will it be calling from the object of class B sitting on the JVM heap or it will be calling from the class C object which is in heap.

There is only an object of class C in the heap. There is no object of class B that could be accessed explicitly. (Some nitpicker might say so but at your level stay with that...) Therefore the member methods of class B are also member methods of class C objects. This means the methods of the class C object will be called which it inherited from class B.

bye

TPD

unknown-7404
If you want to disable inheritance, you may declare both the attributes and the methods as private.

That is NOT sufficient. See The Java Tutorials

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

What You Can Do in a Subclass

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private

members of the parent.

See that last sentence?

unknown-7404

The Java Tutorials has plenty of trails that explain inheritance and they include WORKING example code.

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Please read those tutorials and TRY the examples. The code you posted has errors. That means you never even tried it yourself. Why should forum volunteers comment on code if you are not willing to post CORRECT code that you have tried yourself?


My question is ..

|)" new C()" will create a new object of type C  on the space in JVM called heap .. this object on the heap will have :

1)int x, int y

2)test2()

3)test3()

OR

only test3()

Neither of those is correct. What happened to 'test1()'? Why didn't you include it?

===========================================

||) o.test2() .. this has the reference o calling the test2() method .. so will it be calling from the object of class B sitting on the JVM heap or it will be calling from the class C object which is in heap.

What 'class B' are you talking about? The code you posted does NOT create an instance of class B.

There are NO SHORTCUTS. If you want to learn ANYTHING you have to be willing to:

1. RTFM - the documentation is your friend - spend time with your new friend

2. Try the WORKING example code in the documentation

You have to learn by DOING - you can not just read about things and ask questions.


1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 31 2016
Added on May 3 2016
4 comments
1,026 views