# Class contract on abstract method

**URL:** <https://racket.discourse.group/t/class-contract-on-abstract-method/2052>\
**Category:** Questions & Answers\
**Tags:** contract\
**Created:** [June 28, 2023, 11:06pm UTC](https://racket.discourse.group/t/class-contract-on-abstract-method/2052 "2023-06-28T23:06:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![madkins23](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/madkins23/32/1208_2.png) [@madkins23](https://racket.discourse.group/u/madkins23)\
**Post date:** [June 28, 2023, 11:06pm UTC](https://racket.discourse.group/t/class-contract-on-abstract-method/2052/1 "2023-06-28T23:06:50Z")

</div>

In the following example code:

```scheme
#lang racket/base

(require
  racket/class
  racket/contract)

(define/contract parent%
  (class/c (override [meth (->m string? void?)]))
  (class object%
    (super-new)
    (abstract meth)))

(define/contract child-1%
  (class/c [meth (->m string? void?)])
  (class parent%
    (super-new)
    (define/override (meth str)
      (displayln str))))

(define child-2%
  (class parent%
    (super-new)
    (define/override (meth str)
      (displayln str))))

(module+ test
  (require rackunit)
  (let ([child-1 (new child-1%)]
        [child-2 (new child-2%)])
    (check-exn exn:fail? (lambda() (send child-1 meth 1)))
    (check-exn exn:fail? (lambda() (send child-2 meth 1)))
    ))

```

the first contract exception check succeeds (a contract exception is raised) but the second fails:

```scheme
$ raco test contract-abstract-override.rkt 
raco test: (submod "contract-abstract-override.rkt" test)
1
--------------------
FAILURE
name: check-exn
location: contract-abstract-override.rkt:31:4
params: '(#<procedure:exn:fail?> #<procedure:...stract-override.rkt:31:25>)
message: "child-2"
--------------------
1/2 test failures

```

I was kind of expecting the contract on `meth` to be inherited in `child-2%`. It is, after all, attached to the original, overridden method. Am I doing this wrong or is it always necessary to redefine contracts for overridden methods?

I also tried this without the `abstract` part, using `define/public` in `parent%`. I got the same result.

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [June 28, 2023, 11:59pm UTC](https://racket.discourse.group/t/class-contract-on-abstract-method/2052/2 "2023-06-28T23:59:47Z")

</div>

From the docs:

“”  
A method contract tagged with override describes the behavior expected by the contracted class for method-id when called directly (i.e. by the application (method-id...)).  
“”

The code in question _defines_ meth, it doesn’t call (any existing) `meth` directly.
