Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

memory allocation for subclasses

 
padmavathi gangasani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
padmavathi gangasani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic