• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

B extends A

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
let say i have:
abstract class A(){
...............
}
class B extends A(){
......
void method();
}
and now i'd like to know what is the difference between such statemnts:
B b = new B(); /*and*/ A bb = new B();
plz explain why when i am trying to use
bb.method(); i get such a compiler error
method() not found in class A.
thanx
[ July 27, 2002: Message edited by: Andrew Lit ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code:

a class declaration does not require (). You seem to be confusing a class declaration with a constructor. Furthermore, your class B does not have a implementation for method(), nor does your abstract class A declare a method called method().
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you mean the solution should be in declaration of method() in abstract class A?
but i still can't understand the difference between
B b = new B(); //and
A bb = new B();
the main question wouls be : are b and bb references of the same object?
thanx for the reply
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are b and bb references of the same object?
No. Each identifier references a different object.
B b = new B();
From left to right: This declares that an identifier called b of type B exists, and then assigns a new B object to the identifier b - b refers to a B object.
A bb = new B();
From left to right: This declares that an identifier called bb of type A exists, and then assigns a new B object to the identifier bb - bb refers to a B object.
If you are having trouble understanding why such an arrangement is allowed, I suggest that you take a look at The Campfire Stories - especially The "How My Dog Learned Polymorphism" Story.
Good Luck.
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
are b and bb references of the same object?
From left to right: This declares that an identifier called bb of type A exists, and then assigns a new B object to the identifier bb - bb refers to a B object.
Good Luck.


thanx for the reply, but i still have one question to clearify.
does that mean that object bb can't use not-overriden methods which are declares in class B ?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Lit:

thanx for the reply, but i still have one question to clearify.
does that mean that object bb can't use not-overriden methods which are declares in class B ?


That's right. Since you declared the object as of the class A, you cannot access fields or methods that are not defined in A without explicitly casting bb to class B.
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the confirmation
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic