Why can't I cast a base class instance to a derived class?
For example, if I have a class B which extends a class C, why can't I do this?
B b=(B)(new C());
or ... |
For my semester project, my team and I are supposed to make a .jar file (library, not runnable) that contains a game development framework and demonstrate the concepts of OOP. Its ... |
I have some doubts while comparing C++ and Java multiple inheritance.
- Even Java uses multiple, multi-level inheritance through interfaces - but why doesnt it use anything like a virtual base class as ...
|
When I create the base and derived class in the same directory without specifying any package they compile fine, but adding them to a package leads to an error in derived ... |
I have a lot of classes that extends a single base class. While doing dozer mapping what I want to do is have one mapping for the base class and refer ... |
Ok, so this question will probably get closed, but hell, its 4:30 in the morning and I can't sleep because I'm still frustrated with my Java midterm last night. The ... |
1. There are some methods common to all objects, like toString: String example(Object x) { return "you passed me " + x.toString(); } How could you have written such code without Object? 2. The Collection Framework (java.util.List, Set, Map, etc...) rely heavily on Object to allow the defintion of general collections: void anotherExample(Object x) { List list = new ArrayList(); list.add(x); ... |
|
Hi I am having a Java Bean C (which extends B which in turn extends A) ...and each of these Classes have the BeanInfo classes .. ABeanInfo , BBeanInfo , CBeanInfo.... And i had written my beaninfos such a way that they are optimised by extending the basebeaninfo and calling super.getPropertyDescriptors and adding them to the Descriptoirs of extra methods like ... |
hi stuart, since i know that it's you who normaly posts the answers, not the questions, i was warned ;-) i discussed it with some colleagues. we'd say that it's not possible. my naive approach was to check - if "this" holds a reference to B - what is "super" holding then? well, "this" is an object reference (can for example ... |
in java i can't do something like: BaseClass a = new BaseClass(); DerivedClass b = (DerivedClass) a; is there some utility to _cast_ a base class to a derived class? I don't want to have to implement a clone method just to invoke the getters on the base, and the setters on the derived. i have an ejb which returns some ... |
Hi, Welcome to JavaRanch! You'll have to be a bit clearer as to what you're asking here; what does "is associated with" mean, exactly? But perhaps it would help you to know that any method that accepts an A as an argument will automatically accept a B or a C, and any variable of type A can, again, automatically, refer to ... |
I have a base class. I want to create an intial context and also want to configure the log file. Should I place them in the constructor? Is this a good practice? properties = new Hashtable(); properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); context = new InitialContext(properties); String logConfigFile = getProperties("LOG_CONFIG_FILE"); System.out.println("Log Configuration File " + logConfigFile); PropertyConfigurator.configure(logConfigFile); } |
Hi all, I have a problem here regd the base and sub class, Assumen the subclass extends the base class. SubClass objSC = new SubClass(); BaseClass objBC = objSC; Now here the object of the BaseClass refers to the SubClass? So all methods of the SubClass can be referred to by objBC? |
lets say we have these classes. class Tree {} class Pine extends Tree{} class test{ Tree tree = new Pine(); } now, we all know that tree is now an instance of Pine and Tree but actually derives Pine. My question is Pine anyway extends Tree so an object of Pine also gets the characteristics of Tree. so, we can say ... |
|
It's not clear from your question whether you want a single singleton, which is allowed to be of the base class or any subclass, or whether you want separate singletons of the base class and every subclass. I'm guessing it's the former. If you make the base class constructor store "this" as the singleton instance, in a static field of the ... |
|
Hello, I'd love to search for existing solutions to my problem, but I really can't come up with the terminology for a search. I think I know the answer, but I'm second-guessing myself. In my mind, I usually create only abstract base classes to tie groups of subclasses together, never using the base class itself. My concern is when the instantiation ... |
|
|
21. Base class forums.oracle.comWhat will happen if I have a derived class default constructor which does not call the base class constructor as below? Another hint: who says the derived class constructor does not call a base class constructor? One very good way to learn about what is happening is to output messages in your constructors. Add the following line to your base class ... |
Hi everybody, I don't remember where I saw this, but I think I remember seeing a software package once where all its classes were in a hierarchy, descended from a single base class that extended Object as the root of the tree, like this: Object ->SoftwareObject extends Object ->->Base Window class extends SoftwareObject and JFrame ->->Listener extends SoftwareObject and implements a ... |
I'm going to assume you missed the two comments about there being no copy constructors in Java. To answer your other question. accessing derived members via a reference to a base class doesn't make sense. So it doesn't work. With a reference to X you can only access the members of X and its base classes. |
|
|
I have a bunch of DefaultMutableTreeNodes already created and I want to convert them all to myTreeNode objects which extends DefaultMutableTreeNode. Is there an easy way for assigning the super class of myClass which is DefaultMutableTreeNode. I know this doesn't work but what I want to do is something like setting the super class= passed in DMTN. I know I could ... |
You mean, can you write a class which extends another class, which is unknown until runtime? No, you can't. Apart from not being syntactically possible, classloaders wouldn't be able to load the subclass because they'd first have to load the superclass, which presumably you're proposing to load from within the subclass which isn't yet loaded, because the superclass isn't loaded yet ... |
Because if you just call super(), the message will not bedome part of the object's state. Well actually my qs was why we calling super (Base class construtor) also even though if java specialises for us to call it, then why?? And if at all if we call it, then why are we passing string parameter to it?? (For this you ... |