1

Just a simple question I was thinking about, is it possible to include method or function in an interface as following:

Waiting for your comments and ideas about possibilities or issues doing this:

export interface INewsletter {
  id: number;
  title: string;
  release_date: any;
  filename: string;
  original_filename: string;
  notification: boolean;
  file: File;
  newsletterTranslations: any;
  translations: any;
  newsletterFiles: any;
  newsletter_files: any;

  myMethod() { something to do } // My method here
}
1
  • 2
    @JuanMendes FYI, Java 8 allows you to provide default implementations for interface methods, like what they're asking here. Commented Nov 27, 2017 at 23:00

1 Answer 1

4

An interface is a contract. You can specify the interface has a method but you can't include an implementation. So adding myMethod() : void; is valid but not myMethod() { something to do } as this includes a implementation/body.

export interface INewsletter {
  id: number;
  title: string;
  release_date: any;
  filename: string;
  original_filename: string;
  notification: boolean;
  file: File;
  newsletterTranslations: any;
  translations: any;
  newsletterFiles: any;
  newsletter_files: any;


  myMethod():void; // replace void with any other return type or any
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.