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

  • Home
  • SEARCH
  • 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 6912737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:07:46+00:00 2026-05-27T09:07:46+00:00

I have a subclass that has an extra method from the superclass so when

  • 0

I have a subclass that has an extra method from the superclass so when I need to use that method I need to cast the superclass.

I was wondering if this was a violation of programming to an interface?

If it is, what would be the tradeoff of implementing a blank method of the subclass in the superclass and overriding it just in the subclass that I need it? And if this would make it programming to an interface?

  • 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-27T09:07:47+00:00Added an answer on May 27, 2026 at 9:07 am

    If a method is meaningless in the superclass, adding it there simply to avoid casting is not a good idea. If a method is meaningful in the superclass, and it should reasonably take no action there, then you should put the method in the superclass and override it in the subclass. If the functionality of the added method does not belong in a superclass at all, add and implement a second interface in your subclass, and program to that interface instead when you need the additional functionality.

    Edit: You can add an interface WithText, and add it to the subclass that needs setText. If you add more subclasses that need setText, make them implement that interface too.

    interface Shape {
        void setPosition(int x, int y);
        void draw();
    }
    interface ShapeWithText : Shape {
        void setText(string text);
    }
    class Line implements Shape {
        // ...
    }
    class Circle implements Shape {
        // ...
    }
    class TextBox implements ShapeWithText {
        // ...
    }
    class CircularText implements ShapeWithText {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subclass that calls a method from a superclass. The method in
Similar to this question I have a custom subclass of UITableViewCell that has a
I have a UIView subclass ( CustomView for purposes of this question) that has
I have a subclass of NSObject that implements an -(id)initWithRootElement:(MyElement *)e method. NSXMLDocument has
If I have a subclass that has methods I've overridden from the parent class,
I have a File class that has an Open() method. I have a subclass
I want to have a UIView subclass that has a border image, but I
I have a subclass of UIImageView that I need to know when is touched.
I have a CoreData NSManagedObject subclass, TextNarration, that has an attribute of type NSString,
I have a UITextView subclass that has an NSIndexPath property that is inside a

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.