Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:05:29+00:00 2026-05-28T03:05:29+00:00

My question is regarding Inheritance and Polymorphism. I have a class A which is

  • 0

My question is regarding Inheritance and Polymorphism.

I have a class A which is the super class. Classes B and C extends A and are subclasses.

Now i have a function in other class(not A or B or C) that takes argument of type A. So polymorphism allows us to pass the argument as A , B or C to that function. Now in that function, inorder to access B‘s properties, i have to typecast the received argument to B.

Is type casting a good programming technique ? If not what is the use of polymorphism here ?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:05:30+00:00Added an answer on May 28, 2026 at 3:05 am

    Typecasting is only useful if you know what to cast into.

    The use of polymorphism here is that the method in the other class can be written in a generic way, and a single piece of code can thus operate on different types, which, though, have some interface (class A) in common.

    So… typecasting is (depending on language culture) a very bad practice. Use of polymorphism is preferred (depending…), such as creating a different method with signature ->(B), taking an argument of type B.

    Update: added Java pseudocode

    class A { int m() {} }
    class B extends A { int b() {} }
    class C extends A {}
    class $ { 
      static void x(A $$) {} 
      static void x(B $$) {}
    }
    public class P {
      public static void main(String[] a) {
        $.x(new A()); //calls $::x(A)
        $.x(new B()); //calls $::x(B)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding inheritance in Java. If I have this base class
I have a question regarding Spring inheritance: Let's say I have a class (bean)
Question regarding static variables in static classes. If i have a static class and
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
this is my very first post! I have a quick question in regarding inheritance
I am having some issues regarding inheritance. I have a class of Person, and
Quick question regarding __callStatic() in PHP; class Test{ public static function __callStatic($method, $arguments){ echo
My question is regarding Inheritance modeling in Relational Database Systems. I have canonical data
I have a question regarding inheritance, so I will describe the scenario below: I
I have a question regarding GetTickCount function, I have two calls to this function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.