logo
down
shadow

cast on object of type superclass returns object of type subclass in java


cast on object of type superclass returns object of type subclass in java

By : Bullfrog
Date : November 22 2020, 10:31 AM
wish helps you This is two questions for the price of one. (1) Why do we get the implementation from class B, and (2) why do we get the version of the method with the parameter of type A.
For question (1), the thing to remember is that the class of an object doesn't change when you cast it, or assign it to a variable whose type is something different. So in your example, the class of this is always C, because that's what you created. Class C inherits its version of met(A a) from class B because it doesn't have its own version, and because class B has overridden the version in class A. This is what polymorphism is all about - the version of the method depends on the class of the object that you call it on, not on the type of the expression that you use to call it.
code :


Share : facebook icon twitter icon
Returning a subclass type object instead of superclass type object as demanded by interface

Returning a subclass type object instead of superclass type object as demanded by interface


By : user3227465
Date : March 29 2020, 07:55 AM
Does that help Yes; it's called covariant return. Note, though, that you cannot do the same thing with parameters; they must match exactly.
Java: using setter of a subclass when creating an object of type Superclass

Java: using setter of a subclass when creating an object of type Superclass


By : jbcora
Date : March 29 2020, 07:55 AM
it fixes the issue I have a superclass Person and two subclasses Man and Woman , Cast p1 to Man:
code :
((Man) p1).setAge(13);
Creating a subclass object with the subclass's type or its superclass's type

Creating a subclass object with the subclass's type or its superclass's type


By : gfranz
Date : March 29 2020, 07:55 AM
this will help In small programs like this it is difficult to see the benefits of inheritance and specifically the ability to create students and persons the same way.
However, think for a second that you have a sorting algorithm that sorts the type 'Person' and only that type.
code :
Person p = new Person(name, age);
ArrayList<Person> list = new ArrayList<Person>();

Sort(list);
Person s = new Student(name, age, school);
list.add(s);
Sort(list);
Creating subclass object in an array which type of superclass in java

Creating subclass object in an array which type of superclass in java


By : Roger Sanchez
Date : March 29 2020, 07:55 AM
I wish this help you I'm trying to cover OOP stuffs but I stuck here. I get error in Company class, employees[0].setBonus(50) part. Isn't there any other way to solve it except defining Bonus methods in Employee class? Or is there anyway to keep all objects in one array? I defined bonus methods but there was another thing, what I have to return in Empoyee class' getBonus method? ,
Or is there anyway to keep all objects in one array?
code :
if ( employees[0] instanceof Manager){
   ((Manager) employees[0]).setBonus(50);
}
 employees[0].setBonus(50);
private static Employee[] employees;

private static Manager[] managers;
public Company()
{
   employees= new Employee[]{new Employee("James",5000),new Employee("Jessie",5001)};
   managers= new Employee[]{new Manager("Sapo",10000)};
}

 public static void main(String[] args){
        Company company= new Company();
        managers[0].setBonus(50);
        System.out.println(managers[0].getBonus());    
    }
Why can't I call a public method in the subclass from the superclass when the object is of the subclass type?

Why can't I call a public method in the subclass from the superclass when the object is of the subclass type?


By : Melisa Miller Summy
Date : March 29 2020, 07:55 AM
hop of those help? You are absolutely correct that OnlyB is the same as this.OnlyB. And that's the issue; this is of type A. It has no knowledge that B even exists (nor should it).
Superclasses do not, and are not supposed to, have any knowledge about the fact that they have derived classes at all let alone what methods are in them. Its not an issue of "searching"; what happens when you have C that doesn't define OnlyB? What method would be called?
Related Posts Related Posts :
  • Pros and Cons of Clojure http client libraries
  • use final inside a for each loop
  • Convert java data object to service object
  • Unsupported major.minor version 51.0 while executing JSP
  • How do I display a word diagonally in Java?
  • making sure one task completes before another starts in java
  • My program gives an index out of bounds error
  • Removing duplicate characters in a String (user inputted keyword)
  • Jersey 2.0 Content-Length not set
  • AWS.SimpleQueueService.NonExistentQueue Exception thrown when Accessing Existing SQS queue
  • Where can I find the source code for the com.sun.jdi package?
  • How can I get path of resource under WEB-INF/class folder (java ee web dynamic project)
  • convert a hexadecimal into a decimal via string convert in java
  • error populating a table using jstl
  • If Singletons are bad then how do you store global state for a framework
  • input system where the user inputs the array position of the object followed by a # to indiacte quantity but it gives me
  • Java: when would I ever want to use static methods when I can use a singleton instead?
  • Extracting RSA public key modulus and exponent stored in unsigned char arrays
  • What does "cannot be resolved to a type" mean, and how can I fix it? Java Android 4.0
  • How can I have a callback on stdout/stderr readiness instead of busy polling?
  • How to report AWT/Swing event queue length programmatically?
  • What is the difference between different for loops in Java?
  • Java arrays effective use/alternative
  • EasyMock chained method call producing null pointer exception
  • Installing Java on Windows 8
  • Java: Is it bad practice to write methods that start with "get" when they aren't getters?
  • Can Iterable.filter() skip processing with "constant" (including short circuited) Predicates?
  • How to save edited JTable data to database?
  • Java Packet Sniffer
  • I have a getText error
  • Reading bytestream and writing to file
  • dat file is not get created by buffered writer
  • Converting a pdf to word document using java
  • Measuring memory use of a piece of code at runtime in Java as an on-going functionality of a service
  • What is Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); in OpenGL ES 20?
  • Opening 2 audio-files and playing them simultaneously
  • Accept a word then print every letter of that word on new line in java?
  • How can I send http request to another servlet
  • Reattaching a detached instance: hibernate lock()
  • How to add similar objects into an array?
  • Changing views within an activity
  • Conditions in while loop
  • Why do we need Set and Map for Java Enum
  • Converting a list of objects to a list of optional objects in Java
  • how threads work in java
  • Importing/Exporting Google Contacts in Java App
  • Finding non duplicate element in an array
  • How to prevent JDialog from popping up in front of the JFrame when I close the JFrame?
  • Program hangs indefinitely after two threads print even off numbers alternatively
  • Does JPA support getting data from Database Views?
  • docx4j traversalutil finds no TextElements in the Paragraph node
  • How to create a ListView with AlertDialog.Builder onItemClickListener?
  • Run code after Spring context has been initialized in web app deployed on Tomcat
  • Gradle project does not build when I add RoboBlender
  • java.lang.VerifyError with Mockito 1.10.17
  • Ivy can not resolve the scope of a dependency which is a dependency of a transitive dependency
  • Cannot assign value to static integer
  • Use case of method overriding in PHP
  • Printing and returning the correct list
  • Inferring parameter types in a generic class which are nested generic types of provided arguments
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org