aspose file tools*
The moose likes Beginning Java and the fly likes  memory allocation for subclasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " memory allocation for subclasses" Watch " memory allocation for subclasses" New topic
Author

memory allocation for subclasses

padmavathi gangasani
Greenhorn

Joined: Oct 10, 2012
Posts: 4
I have a very basic question?

What is the difference between extending a class and using super class methods and creating objects for the class,using object to invoke a method.

How memory will be allocated in both terms adn why proteced modifier access for subclass and non subclass(using object to invoke protected method)in different packaes.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

padmavathi gangasani wrote:
What is the difference between extending a class and using super class methods and creating objects for the class,using object to invoke a method.


It's a design issue. You don't extend a class to be able to use its methods. You extend a class when you're creating a specialized type of whatever that class represents. Inheritance is much overused, especially by beginners but also by experienced developers. You should rarely extend concrete classes.

How memory will be allocated in both terms


The memory for the member variables is the same. The basic object overhead is 1 vs. 2. However, you will NOT be using memory concerns to decide whether to extend or not.

adn why proteced modifier access for subclass and non subclass(using object to invoke protected method)in different packaes.


What?
padmavathi gangasani
Greenhorn

Joined: Oct 10, 2012
Posts: 4
Thanks for reply.

What I am tryin to ask is there are two classes A and B.

If B extends A,will B hold the memory of A?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

padmavathi gangasani wrote:Thanks for reply.

What I am tryin to ask is there are two classes A and B.

If B extends A,will B hold the memory of A?


The details of how the memory is laid out is not defined by the spec. It's also not something that a Java programmer needs to think about.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

If B extends A, then an object of type B will also contain all the member variables that are in class A. An object of type B is an object of type A, with extra member variables and methods added (those that are defined in class B).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
padmavathi gangasani
Greenhorn

Joined: Oct 10, 2012
Posts: 4
Thanks Josper,
It clears my doubt ,but I have another confusion.

If class B is in different package and we didnt extend A.But,created reference for A.
But, we cant access protected methods in A through that reference.

Why?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

padmavathi gangasani wrote:But, we cant access protected methods in A through that reference. Why?

Because that's how it's defined. I suggest you take a look at this.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And welcome to the Ranch
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
padmavathi gangasani wrote:But, we cant access protected methods in A through that reference. Why?

Because that's how it's defined. I suggest you take a look at this.

Winston


And if we could access them, then protected would be no different than public, and there would be no point to having it in the first place; it would be redundant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: memory allocation for subclasses