Polymorphism Possible Only When Superclass Is Abstract?
Jan 23, 2014I've a question regarding polymorphism.
Is polymorphism possible only when the superclass is abstract?
I've a question regarding polymorphism.
Is polymorphism possible only when the superclass is abstract?
My understanding was I could override a method from the superclass, including with different parameters, but when I try to use super. it gives me an error the arguments have to match the superclass. But, if I do that it won't make any sense.
The first code below is the superclass. The issue I'm having is on the second code at lines 7 and 10. The ultimate goal is to make a new class where I'm able to display various packages with or without insurance.
public class Package {
double shippingWeight;
public char shippingMethod;
final char air = 'A';
final char truck = 'T';
final char mail = 'M';
double shippingCost;
[code]....
I am trying to display the getCommands() method from my subclasses but I do not know how to cast them both. At the moment I can only display one animals getCommands() method.
public class Test {
public static void main(String[] args) {
Pet [] pet = new Pet[5];
pet[0] = new Dog("Scamp", 1, "run");
pet[1] = new Dog("Molly", 2, "fetch");
pet[2] = new Dog("Rover", 3, "dig");
[Code] ....
So far I thought that setting superclass member variables as protected would allow the subclasses to access them using this. and that this was a good approach. However now after further reading am finding that actually these variables are better set as private and then accessed by the subclasses using public method (getters and setters) or constructor.
So my question is do you recommend setting them as private instead of protected and what would be the best way to access these variables from the subclasses ?
I would like to pass a variable I have in my main to my JFrame. In my main I calculate which pictures I should show in my JFrame. Is it possible to make objects of these pictures in my main class and use them in my JFrame? something like my code below.
public class JFrameTesting {
public static void main(String[] args) {
Image ImageVariable = Toolkit.getDefaultToolkit().getImage("C:1.jpg");
MyJFrame f = new MyJFrame();
f.setTitle("Drawing Graphics in Frames");
[code]....
I'm using binding into fxml:
<Button fx:id="btnSalva" defaultButton="true" mnemonicParsing="false" disable="${controller.busy}"
onAction="#salva" prefHeight="57.0" prefWidth="141.0" styleClass="text-bold" text="Salva" />
Controller is my controller that extends another class from which it inherits busy property.
I see that fxmlloader only looks into the bottom class and not in superclasses, don't know if it's voluntarily or a bug.
What are the benefits of using an Interface plus an abstract class, over just an abstract class?
View Replies View RelatedWhile studying polymorphism , i have some doubts that i am unable to clarify ..... Here they are :
Suppose our inheritance ladder be :
C1 <- C2 <- C3 <-....... <- C100
where C1 is most general (superclass) and C100 is most specific class .
Now , if i write java code in my main() :
C21 Obj = new C100();
Obj.someMethod();
So, what will happen in scenarios as given below :
Scenario - 1) If someMethod() is only defined in C1 ? How will compiler search for this someMethod() ?Will it be executed ?
Scenario - 2) If that someMethod() is static and only defined in C1 , then how will it be searched and executed ?
Scenario - 3) If someMethod() is only present in C100 , then what will happen ?
I have difficulty understanding the following behaviour.
class A {
public String string = "a";
public String toString() {
return string;
}
}
class B extends A {
[code]...
The output of the program is 'c'. This is expexted behaviour. But if class B is changed as follows,
class B extends A {
public String string = "b";
}
Now the program prints 'a' instead of 'c'. Why the statement: b.string = "c"; is not taken into account?
I have a program I want to make (text based, no gui). There is the main class, an Employee class (sort of a template), a CrewMember class, and a Manager class.
I'll put the code for each class an explain the problem I have.
package polymorphism;
import java.util.Random;
public class Start {
public static void main(String[] args) {
Random rand = new Random();
Employee staff[] = new Employee[5];
for(int i = 0; i < staff.length; i++){
[Code] ....
Some of the code is a bit incomplete simply because I ran into the problem. As you can see I made an array in the Start class and it holds objects of Employee type, but create a new instance of either a crew member or a manager, sets their wages, hours, and bonus if applicable. I know if I create an array of a certain type, I can't call upon the subclass' method (Manager in this case) because it has a new method that I added. What I'm trying to do is pretty much call upon the getSalary() method in the Manager class/object, but of course I can't. What way would i be able to do that? I tried looking for some answers. I read about making the superclass abstract and implementing it into the subclasses. Would that be an option?
I would eliminate the ifs and elses using polymorphisms that solves this lot of ifs? How could I solve this?
View Replies View RelatedRecently I have been thinking of using additional interfaces in one of my libraries to hide certain "unsafe" methods of my classes from the user. I would like to get the opinion of other, more advanced java programmers on this issue.
What I do is something like the following (heavily simplified):
public interface ReadOnly {
public int getValue();
}
public interface ReadWrite extends ReadOnly {
public void setValue(int value);
[Code] ....
The user would have access to the ExternalInterface. The ExternalInterface is controlling the values of the InternalComponent. The user can get the InternalComponent, but typecasted to a ReadOnly which does not expose the setters.
Now the thing is the user can still access the setValue method by typecasting the ReadOnly to an InternalComponent or to a ReadWrite, and that is not bad. If the user knows exactly what he/she is doing this will give him/her more flexibility and more power. But this way it *should* become obvious that this is not the intended use and perhaps the extra work of typecasting will discourage the user from doing this.
can we achieve runtime polymorphism by data members?
View Replies View RelatedI have a superclass used for all the other entity
@MappedSuperclass
public abstract class BssStandardEntityLaravel extends BssStandardEntity
implements InterfacciaBssStandardEntity, Cloneable{
private static final long serialVersionUID = 1L;
@Column(name = "created_at", nullable=true)
@Temporal(TemporalType.TIMESTAMP)
protected Date created_at = new Date();
[Code] ....
When i try to read some data with a JPA controller, this error is raised:
Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.
org.datanucleus.store.rdbms.exceptions.NoTableManagedException: Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class.
at org.datanucleus.store.rdbms.RDBMSStoreManager.getDatastoreClass(RDBMSStoreManager.java:702)
[Code] ....
It requires BssStandardEntityLaravel table like a normal entity. I used the same entity package in other applications and it works perfectly. Why this error is raised?
I use jpa2.1.0 and datanucleus to enhance classes
Why is the equals-method in the super-class invoked? Shouldn't the equals-method in the sub-class be invoked(at least in the first if-statement since b2 is a B(i know B is also an A))?Is the equals-method overridden? Or does B have its own equals-method?
class SomeClass{
public static void main(String[] args) {
B b1 = new B(123,1);
B b2 = new B(123,2);
[code]...
I have a quick polymorphism question. I have a parent class and a sub class that extends the parent class. I then declare an array of parent class but instantiate an index to the sub class using polymorphism. Do I have to have all the same methods in the child class that I do in the parent class? Here is an example of what I mean.
public class ParentClass
{
public ParentClass(....){ }
public String doSomething(){ }
}
public class ChildClass extends ParentClass
{
public ChildClass(....)
[Code] ....
Is polymorphism similar to interfaces where the child class needs all the same methods?
I am just started to learn java and i am facing trouble learning abstract class.
View Replies View RelatedWhat programs use abstract classes?
View Replies View RelatedDo we have constructor in abstract class? If we have then what is the use of it?
View Replies View RelatedI know whats the interfaces and abstract class and also know that difference between interface and abstract class,but here my doubt is eventhough abstract class more advantage than the interface,then why should we use interfaces and when?
View Replies View RelatedI am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
View Replies View RelatedI passed my abstract class private final reference to another concrete class and I used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occurring then i will check my code...i think my code is right but little bit doubt abstract class concept.
{
}
class concept1 extends concept
{
private final concept parent;
public concept1(concept aparent)
{
parent=aparent;
System.out.println(parent);
}
public static void main(String args[])
{
//concept p=new concept1(null);
concept c=new concept1(null);
}}
An abstract method is a method with no implementation. So would like to know what is the purpose of calling it if there is no implementation?
View Replies View Related I did research again....
interface:
methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?)
fields - public, static, final ONLY
abstract class: a normal class, but has at least one abstract method
methods - all
i.e., static, non-static, abstract (can it have a default method?)
fields - all
i.e., public, protected, private / final, non-final / static, non-static
why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.
View Replies View RelatedI just wanted to know that why are final and abstract called as modifiers ,what is the essence of calling them as modifiers since there are two types of modifiers access modifiers and non-access modifiers so final and abstract come under the second category ,so why are these called as modifiers?
View Replies View Related