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 7805983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:23:11+00:00 2026-06-02T02:23:11+00:00

How can I force base methods to take in the same specific subclass instance

  • 0

How can I force base methods to take in the same specific subclass instance when overriden by a subclass?

i.e.:

abstract class Animal {
  def mateWith(that: Animal)
}

class Cow extends Animal {
  override def mateWith...?
}

Logically, a Cow should only be able to mateWith another Cow. However, if I do override def mateWith(that: Cow), this doesn’t actually override the base class method (which I want it to, since I want to enforce its existence in the subclass).

I could check to make sure the other instance is of type Cow, and throw an exception if it isn’t – is this my best option? What if I have more animals? I would have to repeat the exception-throwing code.

  • 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-06-02T02:23:12+00:00Added an answer on June 2, 2026 at 2:23 am
    abstract class Animal[T <: Animal[T]] {
      def mateWith(that: T)
    }
    
    class Cow extends Animal[Cow] {
      override def mateWith(that: Cow) { println("cow") }
    }
    
    class Dog extends Animal[Dog] {
      override def mateWith(that: Dog) { println("dog") }
    }
    

    And use it like this:

    scala> (new Cow).mateWith(new Cow)
    cow
    
    scala> (new Cow).mateWith(new Dog)
    <console>:17: error: type mismatch;
     found   : Dog
     required: Cow
                  (new Cow).mateWith(new Dog)
                                     ^
    

    No exception-throwing code needed; the type system handles it for you at compile-time!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I somehow force a derived class to always call the overridden methods base?
I have a method like public abstract class Base { public void MethodUnderTest(); }
I have an abstract base class class IThingy { virtual void method1() = 0;
I have an abstract immutable base class that defines enforces child classes to be
How can I make it so that any class that inherits from my base
Suppose I have an abstract base class BaseTimeCollection and at least one concrete class
On Visual studio I can force use of 32-bit time_t by declaring _USE_32BIT_TIME_T .
Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion
I've been trying to figure WIF out for a while now, and can force
Okay, I've got my normal app which is in portrait mode. I can force

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.