• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

memory allocation for subclasses

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic