I have an interface called Dictionary which has a method insert(). This interface is implemented by class BSTree, but I also have a class AVLTree which is a child class of ... |
I am making a drawing program, and have a few question with regards to that.
I'll need the user to have the choice of drawing a rectangle, an oval and a line. ... |
I have a class C. Class E extends it.
E e = new E();
C c = new C();
Why is
e = (E) c;
Upon further review: though numeric conversions have the same syntax as ... |
I'm working on a small java game which is a sort of clone of the 1995 game Theme Hospital.
I've recently been working on the GUI and the Level structure now I ... |
I've learned all these programming terms in Swedish so please bear with me..
I'm having problems calling a method in a subclass which should override a method in the superclass.
Here is the ... |
I have a Abstract Iterator class which has this function
void iterate(){
while(this.hasnext()){
..this.next()..
}
}
How do I ... |
For SCJP most of the time question such as below is asked to find valid example of polymorphic method calls. But what should one exactly look for to find is it ... |
|
I'm trying to set parameters for a abstract class:
public abstract class NewMath {
public abstract int op (int intOne, int intTwo);
}
Here is the extended subclass:
public class MultMath extends ...
|
Consider the following code:
public abstract class Base {
public void getAnswer();
}
public class Derived1 extends Base {
public void getAnswer() ...
|
In the below code , why b1.subtract() fails . Please explain me the reason ie., what happens in JVM while invoking that method .
class Base {
public void add() {
System.out.println("Base ADD");
}
}
class Child ...
|
I'm working on a homework assignment (a project), for which one criterion is that I must make use of polymorphism in a way which noticeably improves the overall quality or functionality ... |
is method overloading is a example of run time polymorphism?
is method overloading example of compile time polymorphism?
|
class another {
public void method(Object o) {
System.out.println("This is in method which takes object");
}
...
|
I'm creating some random classes to understand Polymorphism better.
Coding is as follows:
Poly1:
public abstract class Poly1 {
int comPoly;
}
SubPoly1:
public class SubPoly1 extends Poly1 {
String ...
|
What is parametric polymorphism in java
|
Is it better to put a default implementation of a method in a superclass, and override it when subclasses want to deviate from this, or should you just leave the superclass ... |
I got a list of objects like this
ArrayList <Page> pageList = aForeignObject.getAllPages();
And a child class
class MyPage extends Page
{
public void newFunction()
{
...
|
Particularly in java. The polymorphically refactored code is running slower. Is it possible that polymorphism could be the culprit or perhaps there is a confounding performance drain unrelated to polymorphism.
|
Is there an easy way to verify that an object belongs to a given class? For example, I could do
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
but this requires instantiating ... |
Hi I just wanted to undertand the following behavior..
I have defined the same method - gg() in the base and derived class with different access
Class Base
{
// ...
|
I've got a few questions:
1) given a class named class WavPanel extends JPanel
Can I say that :
a) it's an example of inheritance
b) it's an example of polymorphism since the class ... |
Is the following code an example of polymorphism ? Thank you
/**
* Create a waveform according to audio signal intensity
...
|
I've got some problems with the following example (to be more precise, with one specific line). Here's the code (question follows afterwards):
public class Up
{
public void cc(Up u) ...
|
The below code shows my problem. Effectively, I am trying to use Mockito's ArgumentCaptor to verify that a method was called once with a certain concrete class. I would like to ... |
I am reading the SCJP book by Kathy Sierra. Polymorphism is little confusing. Could you please help me out with a real world example for the following: I understood that Polymorphism ... |
I have a problem deserialize json string with gson.
i basically recieve an array of commands. the command can be start or stop or some other type of command. so naturally i ... |
According to book SCJP Sun Certified Programmer for Java 6 Study Guide, "all java objects are polimorphic".
class B {}
class A extends B {}
Class A doesn't have any overriden methods. Is ... |
I am playing with Java's reflection API, and I'm writing methods that inspect a given object and describes its fields, methods, and so on.
I am using the getFields() method to iterate ... |
which two are the benefits using polymorphism?
- Code that is protected from extension by other classes
- Code that is more dynamic at runtime
- Code is more flexible and reusable
- code is more efficient at ...
|
I'm about to take the final exam in my very first object-oriented programming class and I still don't understand something about the concept of polymorphism.
Let's say I have an abstract class ... |
Could someone please explain why this happening:
class Apple {
String type;
setType(){
System.out.println("inside apple class");
this.type = ...
|
I'm sure this is a basic OOP question -- I'm designing a message-passing system where there are several entirely distinct message formats, but I want them all to be able to ... |
public class XXX {
@Test
public void test() {
B b = new B();
...
|
The only examples of polymorphic method overriding I ever see involve methods that take no parameters, or at least have identical parameter lists. Consider the common Animal/Dog/Cat example:
public abstract class Animal
{
...
|
I have question regarding polymorphism about its assignment statement, for Example this is The Super class
public class Person {
...
|
I have been given a task that can be simplified to this scenario:
Customers need to order boxes and these boxes can come in different sizes, the different sizes can be ordered ... |
I am trying to get round a rather annoying issue in my homework.
Basically the task is to create a fake ordering UI where the user puts in some variables about the ... |
Basically I have been tasked with tackling the following scenario:
When you are designing your class/es, you have to decide what attributes and methods you will include in. For example, if you ... |
|
Hi Doni, Welcome to JavaRanch! The short answer is that there's no polymorphism here. The two different "test" methods have different argument lists, so they are not polymorphic; they're said to be overloaded. It's up to the compiler to decide which of a set of overloaded methods is called, and the compiler looks only at the type of the variable on ... |
I've got a question that I'm hoping one of you can answer. All of this goes into MyMain.java: public class MyMain { public static void main(String[] args) { MySuperclass mySuper = new MySubclass(); mySuper.doSomething("Hello"); mySuper.doSomethingElse("Hello"); } } class MySuperclass { public void doSomething(Object myThing) { System.out.println("MySuperclass: doing something"); } public void doSomethingElse(String myThing) { System.out.println("MySuperclass: doing something else"); } } class ... |
|
|
Have you read the JavaRanch article on "How my Dog learned polymorphism"? This is very good and by understanding how polymorphism works you may start to see how useful it is in Java and why it is used... Maybe you need to go right back to OO concepts and think about how OO is used to model real life systems... |
I'm having a problem understand exactly what's going on inside the JRE with the following circumstances. Let's say, for example, I have the following 2 classes that represent Actions: public class GenericAction { public void invoke(GenericObject obj) { System.out.println("Generic invoke method!"); } } public class SpecificAction { public void invoke(SpecificObject obj) { System.out.println("Specific invoke method!"); } } Additionally, SpecificObject extends GenericObject. ... |
|
This line Test t= new Abs(); tells the compiler that static operations are done on class Test. Period. It wires static methods and variables up to class Test at compile time. Try the static t.show() with "Test t = null;". The call is wired to Test, not the instance. Non-static operations are done on the actual class of the instance. The ... |
|
Sounds more like a beginner's question to me. I thought polymorphism was only seen at runtime. You will find lots of examples. Imagine you have a superclass, and you have subclasses; they can have methods which have the same signature (overridden) which use different operations. Example:-public abstract class Shape { protected double size; public Shape(double size) { this.size = size; } ... |
I answer this with the assumption that you know Inheritance.. Say you have a parent class Vehicle and child classes Car, Bike and Bus. Now you are writing the method to fill gas in your respective vehicles. Without Polymorphism, the code will be like this //for car void fillGas_Car(Car myCar){ //code to fill gas } //for bike void fillGas_Bike(Bike myBike){ //code ... |
|
Can any one tell me the real example for polymorphism. Real example in the sense, we have to tell the example to some non computer science persons. Encapsulation is nothing but wrapping of data and method into single unit. This definition for only us. But i wanna explain, what is meant by encapsulation to someone who is not in a computer ... |
many times I faced the question " what is polymorphism / how do you feel polymorphism ?" I answered but many times I felt that the person felt somthing missing so I got some lower rating . Can you tell me how should I answer ? generally what I feel like I will make the answer like this : 1. i ... |
|
Just read the Javaranch article "How my Dog learned Polymorphism". Brilliant. I wish more textbooks would use a simple approach to explaining their content. I am however still unclear on the use of instance variables when Polymorphism is in action. Consider the following, given the Dog-extends-Animal analogy in "How my Dog learned Polymorphism": 1) I add an instance variable (say x) ... |
In regard to your creative passage on polymorphism, just a couple of q's to better my understanding. Firstly, as explained by the story, there are advantages in using a different reference type to the object class (dog to animal) including an array as mentioned. In this case and others where advantages are less clear, is the use of this convention more ... |
From what I understand, neither is an example of Polymorphism. However, method overriding is a part of it. From "Beginning Java 2" by Ivor Horton: "Polymorphism...means the ability of a single variable of a given type to be used to reference objects of different types, and to automatically call the method that is specific to the type of object the variable ... |
I am currently learning polymorphism and wanted to make sure I've got the basic concept down. abstract class AbstractClass { abstract String whichClass() ; } class A extends AbstractClass { String whichClass() // overrides AbstractClass's whichClass() { return "A" ; } } class B extends AbstractClass { String whichClass() // overrides AbstractClass's whichClass() { return "B" ; } } public class ... |
Angela, Check out the Campfire stories here at JavaRanch - You can access it through the main page. I won't post the link - I would encourage you to explore this site. Any introductory book should have a decent explanation (with examples) on polymorphism. Use the search facility for this discussion forum to get more examples and explanations. Use Google for ... |
Originally posted by SenthilNathan CAlagan: Why java doesn't support multiple inheritance? I know indirectly it can be achieved through Interface. Thanks, senthil I'm hardly the one to answer this as I am truly a newbie but I just heard comments on this very subject while listening to Bruce Eckel's Thinking in Java CD. It's chapter 8 slide 4 in case you ... |
public abstract class Super //Parent class { private String abc; public Super(String x) { abc=new String(x); } public String toString() { return "this game name is " + abc; } public abstract void play(); } public class Game1 extends Super { private String name; public Game1(String a) { super("Theif"); name=a; } public void play() { System.out.println("theif cought"); } public String toString() ... |
I have a program that I am exercising polymorphism with but I cannot access the child members. Any ideas on how I go about getting to these members in my Main()? here is what I have beware quite lengthy) package Runner_Marathon; import java.text.*; import java.util.*; public class Runner_Marathon { private String firstName; private String lastName; private StreetAddress StrtAddsForm = new StreetAddress(); ... |
What is wrong about this program? Thank you for answers. Thomas class FredException extends Exception {} class SomeOtherException extends Exception {} class base { void fred () throws FredException { } } class derived extends base { void fred () throws SomeOtherException { } } class Fredtest1 { public static void main (String args[]) { base b = new derived (); ... |
It seems that in Java Polymorphism is only for member functions. Data members' value do not get resolved to overridden value in actual object type. So if a member function is using data member value, member function will be polymorphic but data member will NOT be polymorphic. Is that by design in Java or a limition or my misunderstanding? For example ... |
It seems that in Java Polymorphism is only for member functions. Data members' value do not get resolved to overridden value in actual object type. So if a member function is using data member value, member function will be polymorphic but data member will NOT be polymorphic. Is that by design in Java or a limition or my misunderstanding? For example ... |
Please consider the code as follows: The result is Child.test as first line because anSub is of Base reference type But of Child object type. When an instance method is overridden then the object type counts if the method is called. Therefore Child.test() method is called and printed. This behavour is called Polymorphism. But then ssubObj is fully a a Base ... |
Hi, This is a code from Bruce Eckel's Thinking in Java public class PrivateOverride { private void f() { System.out.println("private f()"); } public static void main(String args[]) { PrivateOverride po = new Derived(); po.f(); } } class Derived extends PrivateOverride { public void f() { System.out.println("public f()"); } } 2 question on this... 1.Can u create an object of child class ... |
I don't think there is a definite answer. There are people who refer to things like method overloading and templates as "static polymorphism". (Just do a google search.) Others will argue that this isn't really polymorphism, as it isn't resolved at runtime (though I don't fully follow that argument). Personally, I don't think it matters much. It isn't that atypical for ... |
Ok , I'm really struggling with this... this is the Shape class inheritance hierarchy I have to stick too. Am I on the right tracks or not I do not know? //-------------------------------Shape //---------------------------/----------\ //--------TwoDimensionalShape------------ThreeDimensionalShape //-------/-------|----------\-------------/--------|---------\ //Circle------Square-----Triangle------Sphere-----Cube-------Tetrahedron I was unsure how to include twodimensionshape for is there any point? The instructions here state I only need to concentrate on two dimensional shapes ... |
Paste the source code first: public class ThisTest { public static void main(String[] augs) { Son son1 = new Son("son1"); Son son2 = new Son("son2"); System.out.println("son1.name: "+son1.name); System.out.println("son1.getName(): "+son1.getName()); System.out.println(son1.equals(son2)); } } class Father { public String getName() { System.out.println("This in getName: "+this.getClass()); return "Pring name field in "+this.getClass()+": "+this.name; } public boolean equals(Object otherObject) { System.out.println("This in equals: "+this.getClass()); if ... |
i've read this part of code in a book, but i don't seems to understand the explantion given. Pls advise. public abstract class Employee { ..... public String toString() { } } public final class Boss extends Employee { ... public String toString() { } } public class Test { Employee ref; Boss b = new Boss ("John", "Smith", 800.00); ref ... |
I'll give this a shot. Polymorphism is about software reuse. It comes in the form or overloaded and overidden methods. Overriden method is a method that a subclass inherits from a superclass with the following qualities: - The subclass's method arg list must be the same - return type must be the same - access level (ie private public etc) must ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
Overloading is giving a method a new signature. Example: public int add( int i, int j) public int add( int i, int j, int k) Both of these methods have the same name "add" but since the parameters are different the add method is now overloaded. A method is overridden when you redefine a method in a subclass that was previously ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
The Java API is FULL of examples. You can even look at the source code yourself if you want, although it may be a bit more complex than what you currently need. However, the great thing about Object Oriented Programming is that you don't need to worry about the implementation details. You can look at the API docs (see the link ... |
Instead of just doing exercises from the textbook, I've decided to write some of my own classes from scratch to get a better feel for how everything is working. I wrote the following code, which compiles but gives a runtime exception, "no such method: main." I'm trying not to use any superfluous stuff, hoping it will lead to a more solid ... |
Hi, I am a little confused about polymorphism..... I have an abstract superclass animal. Dog,Cat,Wolf,Hippo Lion are subclasses of this superclass. each subclass has its own overloaded roam() method animals s=new Wolf(); animals t=new Hippo(); I then call a method on each of these: s.roam(); t.roam(); Since the compiler decides whether you can call a method based on the reference type,not ... |
Hi All, I'm studying Java using "Headfirst Java". A quick question for someone who may be able to help. In polymorphism we are told that a subclass inherits all the variables and methods of the superclass (unless they are private). so if class Dog extends Animal then we can say: Animal myDog = new Dog(); or Animal[] animals = new Animal[5]; ... |
Hi Its true see the following code inwhich sound method(instance method) is overidden which shows polymorphism import java.util.Random; class Animal { public Animal(String aType) { type = new String(aType); } public String toString() { return "This is a " + type; } public void sound() { System.out.println("Sound for an animal"); } private String type; } class Dog extends Animal { public ... |
Hi. I've read in a book: "In Java a subclass collects automatically all visible properties of the superclass, but the objects in the hierarchy EXIST INDIVIDUALLY. This means: If a subclass is istantiated, the constructor automatically calls the constructor of the superclass to instantiate the upper object." I knew about the constructor-chaining but never thought about the consequences. This means: When ... |
ok,what i can explain is this. There are various forms of usable/pragmatic polymorphism in Java like 1) Overloading - (Compile time) 2) Overriding using inheritance - (Runtime) 3) Overriding using interface (Runtime) Polymorphism one name many forms thus provides abstraction Overloading - same method name, different parameter list Overriding - same method name, same parameter list, same return type, ... |
Welcome to JavaRanch! An instance of B IS-AN A, and an instance of C IS-AN A. So if the array holds elements of type A, there should be no problem in adding instances of B or C. The trick is in using the array reference to access any methods defined in the subclass (for example, funB()). To do that, you will ... |
hello, i m unable to compile this program. Its giving me compilation error when i am calling am method getName() of subclass. when i wrote the same method in the super class its not giving me an error. why? Is it mandatory to have a method in super class, if i m calling the method of subclass using the polymorphism concept? ... |
How do you know I did not try to think about it before putting it here? A question to the nicer members of this forum: I think the third option (o2=bc) would not work, but it's hard for me to tell exactly why. Could you explain this? Or maybe some reference to a webpage that explains the principles behind this question? ... |
public class Test { public static void main(String [] args) { MySuper objSuper; MySub1 objSub1 = new MySub1(); objSuper = objSub1; // x() is overridden, y() is NOT overridden objSuper.x(); // OK objSuper.y(); // Compile ERROR - "cannot find symbol method y()" } } //--------------------------------------------------- class MySuper { public void x() { System.out.println("In MySuper's x()"); } } //--------------------------------------------------- class MySub1 extends ... |
Somebody said, "any object has two different types. The first one is the declared type . . . The second type is the actual type. " recently. it was actually Rob Prime, on this thread. You will have an Animal class, possibly abstract, and other subclasses, eg Dog, Cat, Horse, Goldfish. All those classes have the same methods as Animal. By ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
Hi Friends! I am writing a program to implement a console based calculator which will perform some basic arithmatic operation. It will provide two mode i.e. Decimal mode and hexadecimal mode. Since my project require polymorphism, i have to use same function name for both Decimal as well as hexadecimal. I wrote a code for decimal calculator. but cant find solution ... |
Hi All, My question is how to use polymorphism to solve the problem described. I have two Frameworkd Fr1 and Fr2. Fr1 has a class with method abc(A a) which takes an instance of some class A Fr2 has a class with method xyz(B b) which takes an instance of some class B I have a Class where i am instantiating ... |
Sandeep, I think you need to read HowToAskQuestionsOnJavaRanch. To get meaningful advice, you really need to post a lot more information than you've given us, for example, you're obviously running this code somehow, but I don't see a main method anywhere. I haven't tried, but I'm pretty sure that the code you've given us won't compile, so something is definitely missing ... |
There are different types of polymorphism in java 1)Method Overloading 2)Method overriding at inheritance 3)Method overriding by implementing interface. 1)Method Overloading: This is the concept where the method name will be same where as the parameters that's been passed to the method will be different. In this scenario there is no concern with the return type 2)Method Overriding at inheritance: This ... |
I have two classes, a Parent, and a Child. Parent has two methods, one of which is overloaded by Child. public class Parent { private JPanel panel; public JPanel initializeGUI() { JPanel panel = getPanel(); return panel; } public JPanel getPanel() { panel = new JPanel(); return panel; } } class Child extends Parent { JPanel panel; public JPanel getPanel() { ... |
I have a few general questions about the assignment I'm completing I'm trying real hard to understand this stuff, but have a poor textbook and a worse instructor. The assignment deals with Shape hierarchy I've created several class files for this package Shape is a super class TwoDimensionalShape and ThreeDimensionalShape classes extend Shape Square and Circle extend TwoDimensionalShape Cube and Sphere ... |
Hello, You are mixing up some terms. Dynamic binding is not a type of polymorphism; rather dynamic binding is how polymorphism is "implemented" or how it works. And dynamic binding and runtime binding are the same thing. "Dynamic Binding" is the more academic or computer science based name for it. And "run time binding" is a more every day term for ... |
From the below code, the class B is the subclass of class A. After the instance a is created, the method go is called. Java run this method of the class B. And the value of the attribute x is changed within the method go. Then I display the value of the attribute x of a. The initial value of x ... |
If we have this code: class A { void doSomething() { System.out.println("A"); } } class B extends A { void doSomething() { System.out.println("B"); } } public class Main { public static void main(String[] args) { B b = new B(); A a = new B(); B c = new A(); } } What do you get when you try to compile ... |
according to cathy sierra and bert bates - author of a famous java book. "the reference variables type determines the method that can be invoked on the object the variable is referencing." class Base{ void display(){ System.out.print("Base Class"); } } class Sub extends Base{ } class test{ public static void main(String a[]){ Base b = new Sub(); considering the above example ... |
As your code stands method1() is not inherited to Child class so it's not an override in the Child class. So Parent class methods called. But if you make the modifier default/public/protected of method1() in Parent then the method inherited so overriden by the Child class. Hence method1() of Child class called. |
I've recently began the construction of a class, the mechanics of which have lead me to the conclusion that I don't know my way around polymorphism 100%. Here's what I'm doing: The class contains a function which deals with a 'Targetable' argument - where 'Targetable' is an interface. The function then must deal with this 'Targetable' object differently depending upon whether ... |