Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

memory allocation for subclasses  RSS feed

 
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.
 
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.
 
Java Cowboy
Sheriff
Posts: 16083
88
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?
 
Bartender
Posts: 10575
66
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
 
Marshal
Posts: 58384
178
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!