Skip to content
Computer ScienceISC Class 12 · Inheritance, Interfaces and Polymorphism

Interfaces and User-Defined Behaviour in Java (ISC Class 12 Computer Science)

Updated 17 October 2026 · Draft, not yet fact-checked

A Java interface lists method headers (public and abstract by default) and constants (public static final). A class implements it with the keyword implements and must define every method. One interface can have many implementations, and one class can implement several interfaces, so the end user can supply the behaviour, such as the comparison used by a sorting class.

Many classes implement one interface; a Shape reference can hold any of them, and the actual object's area() runs.

Illustration drawn for QuestionBuddy.

Understand Interfaces and User-Defined Behaviour in Java

Interface construct. interface Shape { double area(); } declares what a class must be able to do, not how. Methods have no body and are implicitly public abstract; variables are implicitly public static final constants. An interface cannot be instantiated and has no constructors.

Implementing. class Rect implements Shape { ... public double area() { ... } }. The class must define every interface method, with public access (otherwise it is a weaker access and the compile fails), or it must be declared abstract. A class may extend one class and implement many interfaces: class C extends B implements I, J. An interface may extend other interfaces.

Interface as a type. A variable of the interface type can refer to any object of an implementing class: Shape s = new Rect(3, 4);. A call s.area() runs the implementation of the actual object (polymorphism through dynamic binding). Only methods named in the interface can be called through the interface reference.

Why interfaces: user-defined behaviour. When a library class is written, some part can be known only to its user. A class that sorts records cannot know which field is compared or whether the order is ascending or descending. The class therefore asks for an object of an interface type, for example Order with the method before(a, b), and the user supplies the implementation. The sorting code stays unchanged while the behaviour changes.

Interface versus abstract class. An abstract class can have variables, constructors and ordinary methods, and a class can extend only one; an interface (in the ISC syllabus sense) specifies behaviour only, and a class can implement many. Use an abstract class for shared state and partial code; use an interface for a capability that unrelated classes can offer.

Two meanings of "interface". The language construct interface is different from the everyday word interface, meaning the set of public method headers through which a class is used. Every class has such an interface, even if no interface construct is written.

Inheritance and abstract classes are in the previous topic; stack and queue interfaces appear again in the data structures chapter.

Interfaces and User-Defined Behaviour in Java: key facts

Interface rules
  • Methods are public abstract by default
  • Variables are public static final
  • No objects of an interface type
  • No constructors
Implementing methods must be written public.
Using an interface
  • class C implements I
  • class C extends B implements I, J
  • I ref = new C();
  • Only methods of I are callable through ref
implements may list many interfaces.
Interface versus abstract class
  • Interface: behaviour only, many allowed
  • Abstract class: may hold variables, constructors and code, only one extended
  • Both cannot be instantiated
A class can do both: extend one and implement several.
Compile-time rules
  • Every interface method must be defined or the class is abstract
  • Reduced access (not public) is an error
  • Interface constants cannot be changed
Common error spots in questions.

How to answer Interfaces and User-Defined Behaviour in Java questions

Use this routine for interface programs.

  1. 1Write the interface with method headers only (semicolon, no body).
  2. 2Write each class with implements and define every method as public.
  3. 3In main, create objects and store them in interface-type variables or an array.
  4. 4Trace each call to the implementation of the actual object.
  5. 5For a user-defined behaviour (such as an ordering), pass the interface object to the method that uses it.
  6. 6Check that a call through the interface uses only methods named in the interface.

Interfaces and User-Defined Behaviour in Java in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Different payment methods

Suppose a shop program declares an interface Payment with a method pay(double amount), and the classes Cash and Card each implement it differently.

Why can the billing code hold a Payment reference without knowing which class is behind it?

Try it first, then show the solutionSolution
  1. Every implementing class has pay(), so the call is always valid.
  2. The actual object decides what happens.

Answer: The interface guarantees the method; the implementation is chosen at run time.

Memory hook: An interface is a promise of abilities.

Ascending or descending order

Suppose a sorting class needs to know whether 2 should come before 5, and different users want ascending or descending order.

How does an interface solve this?

Try it first, then show the solutionSolution
  1. The sort asks an Order object whether a comes before b.
  2. Asc and Desc are two implementations; the user passes one in.

Answer: The user supplies the comparison by an implementation of the interface; the sort code is unchanged.

Memory hook: The sorter asks, the user answers.

Tricks to remember Interfaces and User-Defined Behaviour in Java

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Interface = promise, class = keep

    The interface promises methods; each implementing class keeps the promise.

    Use it when: Understanding implements.

  • implements for interfaces, extends for classes

    Class to class: extends. Class to interface: implements.

    Use it when: Keyword choice.

  • Always public

    Interface methods are public, so the implementation must be public too.

    Use it when: Avoiding compile errors.

  • Two meanings

    The construct interface versus the word interface (the public methods).

    Use it when: Definition questions.

Interfaces and User-Defined Behaviour in Java: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Keyword for an interface · 1 mark · MCQBoard level

A class uses an interface with the keyword
(a) extends (b) implements (c) inherits (d) imports

Try it first, then show the solutionSolution
  1. A class implements an interface; extends is for a super class.

Answer: (b) implements

Example 2: Two implementations · 3 marks · Short answerHarder board level

Write the output:

interface Shape { double area(); }
class Rect implements Shape {
    double l, b;
    Rect(double l, double b) { this.l = l; this.b = b; }
    public double area() { return l * b; }
}
class Sq implements Shape {
    double s;
    Sq(double s) { this.s = s; }
    public double area() { return s * s; }
}
// in main
Shape[] a = { new Rect(3, 4), new Sq(5) };
double t = 0;
for (int i = 0; i < a.length; i++)
    t += a[i].area();
System.out.println(t);
Try it first, then show the solutionSolution
  1. Rect(3, 4).area() = 12.0. (1 mark)
  2. Sq(5).area() = 25.0. (1 mark)
  3. The total is 37.0. (1 mark)

Answer: 37.0

Example 3: User-defined sort order · 4 marks · Short answerHarder board level

Use the code to sort {4, 1, 3} in descending and then ascending order and show the array after the sort in each case.

interface Order { boolean before(int a, int b); }
class Asc  implements Order { public boolean before(int a, int b) { return a < b; } }
class Desc implements Order { public boolean before(int a, int b) { return a > b; } }

static void sort(int[] v, Order o) {
    for (int i = 0; i < v.length - 1; i++)
        for (int j = 0; j < v.length - 1 - i; j++)
            if (o.before(v[j + 1], v[j])) {
                int t = v[j]; v[j] = v[j + 1]; v[j + 1] = t;
            }
}
Try it first, then show the solutionSolution
  1. Desc on {4, 1, 3}: i = 0, j = 0: before(1, 4) is false; j = 1: before(3, 1) is true, swap, giving {4, 3, 1}. (1.5 marks)
  2. i = 1, j = 0: before(3, 4) is false. Result {4, 3, 1}. (0.5 mark)
  3. Asc on {4, 1, 3}: j = 0: before(1, 4) is true, swap, giving {1, 4, 3}; j = 1: before(3, 4) is true, swap, giving {1, 3, 4}. (1.5 marks)
  4. i = 1, j = 0: before(3, 1) is false. Result {1, 3, 4}. (0.5 mark)

Answer: Descending: 4 3 1. Ascending: 1 3 4.

Example 4: Interface constant · 3 marks · Short answerHarder board level

Write the output:

interface Tax {
    double RATE = 0.5;
    double calc(double x);
}
class Half implements Tax {
    public double calc(double x) { return x * RATE; }
}
// in main
System.out.println(new Half().calc(300) + " " + Tax.RATE);

State why Tax.RATE = 0.7; in main is invalid.

Try it first, then show the solutionSolution
  1. calc(300) = 300 x 0.5 = 150.0 and Tax.RATE is 0.5. (1.5 marks)
  2. Output: 150.0 0.5. (0.5 mark)
  3. An interface variable is implicitly public static final, so it is a constant and cannot be assigned. (1 mark)

Answer: 150.0 0.5; the constant cannot be reassigned

Example 5: Case: two capabilities in one class · 4 marks · Case-basedHOTS / competency

Suppose an office program has:

interface Printable { void print(); }
interface Savable   { void save(); }
class Doc implements Printable, Savable {
    public void print() { System.out.print("P "); }
    public void save()  { System.out.print("S "); }
}

main has: Doc d = new Doc(); Printable p = d; p.print(); ((Savable) d).save(); d.save();
(i) Write the output. (1 mark)
(ii) Why is p.save() a compile-time error? (1 mark)
(iii) A class can extend only one class, but can implement several interfaces. Why is this useful here? (2 marks)

Try it first, then show the solutionSolution
  1. (i) p.print() prints "P ", then save() through the cast prints "S ", then d.save() prints "S ". Output: P S S
  2. (ii) p has the type Printable, which names only print(); the compiler does not allow save() on that type without a cast.
  3. (iii) Doc can offer two unrelated abilities, printing and saving, and can be used wherever either a Printable or a Savable is needed.

Answer: (i) P S S (ii) Printable has no save() (iii) one class can provide several capabilities

Where students lose marks in Interfaces and User-Defined Behaviour in Java

The slips examiners see most often, and what to write instead.

  • Mistake: Weaker access in the implementing method

    Interface methods are public, so the implementation cannot be package-private.

    Wrong working

    double area() { ... } in a class implementing Shape

    Right working

    public double area() { ... }

    Do this: Write public on each implemented method.

    Usually costs: 1 mark

  • Mistake: Instantiating an interface

    An interface has no implementation.

    Wrong working

    Shape s = new Shape();

    Right working

    Shape s = new Rect(3, 4);

    Do this: Create an object of an implementing class.

    Usually costs: 1 mark

  • Mistake: Using extends for an interface

    A class implements an interface.

    Wrong working

    class Rect extends Shape

    Right working

    class Rect implements Shape

    Do this: implements for interfaces.

    Usually costs: 1 mark

Exam tips

  • Write the interface header with a semicolon and no body for each method.
  • Mark every implemented method public.
  • In an output question, find the actual class behind each interface reference.
  • Give both meanings of "interface" when the syllabus-style definition question appears.
  • Use an array of the interface type to show polymorphism in a program.

Practice questions on Interfaces and User-Defined Behaviour in Java

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    By default, the methods declared in an interface are
    (a) private and static (b) public and abstract (c) protected and final (d) public and static

    Hint

    They have no body.

    Answer

    (b) public and abstract

  2. Q2 · 2 marks · Short answerBoard level

    Distinguish between the Java construct interface and the word interface used for the set of method headers of a class.

    Hint

    One is a keyword, the other is a description.

    Answer

    The construct interface is a Java language element declared with the keyword interface that a class implements. The word interface in the general sense means the public methods through which any class is used; every class has one, whether or not a Java interface is declared.

  3. Q3 · 4 marks · Short answerHarder board level

    Write an interface Discount with a method double apply(double price), and two classes Flat (subtracts 50) and Quarter (subtracts one quarter of the price). Show apply for price 400 with each class.

    Hint

    Implementing methods are public.

    Answer
    interface Discount { double apply(double price); }
    class Flat implements Discount {
        public double apply(double price) { return price - 50; }
    }
    class Quarter implements Discount {
        public double apply(double price) { return price - price * 0.25; }
    }

    For 400: Flat gives 400 - 50 = 350.0; Quarter gives 400 - 100 = 300.0.

  4. Q4 · 3 marks · Short answerHarder board level

    State two differences between an interface and an abstract class, and write the header of a class C that extends a class B and implements interfaces I and J.

    Hint

    Think of variables, constructors and how many can be used.

    Answer

    An abstract class can have instance variables, constructors and ordinary methods; an interface has only method headers and constants. A class can extend only one abstract class but can implement many interfaces. Header: class C extends B implements I, J

  5. Q5 · 4 marks · Case-basedHOTS / competency

    Use the Order, Asc, Desc and sort code from the worked example. (i) Show the array after sort({2, 9, 5}, new Desc()). (ii) Explain why the sort method needs no change to sort in the other order. (iii) Name the idea shown.

    Hint

    Trace each pass with before(v[j + 1], v[j]).

    Answer

    (i) i = 0: j = 0: before(9, 2) is true, swap, giving {9, 2, 5}; j = 1: before(5, 2) is true, swap, giving {9, 5, 2}. i = 1: j = 0: before(5, 9) is false. Result {9, 5, 2}. (ii) The decision of which element comes first is made by the Order object, so passing new Asc() reverses the order. (iii) User-defined behaviour through an interface (polymorphism).

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Which keyword does a class use for an interface?Show answer

    implements.

  2. 2.Are interface variables changeable?Show answer

    No, they are public static final constants.

  3. 3.Can an interface be instantiated?Show answer

    No.

  4. 4.Can a class implement more than one interface?Show answer

    Yes, separated by commas.

  5. 5.What must an implementing class do for each interface method?Show answer

    Define it as a public method, or be declared abstract.

  6. 6.What problem does an interface solve in a sorting class?Show answer

    It lets the user supply the comparison and the order.

Interfaces and User-Defined Behaviour in Java: frequently asked questions

What is an interface in Java?

It is a type that declares method headers and constants without giving the code for the methods; classes that implement it supply the code.

What is the difference between an interface and an abstract class?

An abstract class can have variables, constructors and ordinary methods and is extended by one class; an interface has only method headers and constants and a class can implement many of them.

Why can a class implement many interfaces but extend only one class?

Java allows only single inheritance of classes, but an interface carries no state or code to conflict, so a class can combine several capabilities.

Do I need to write public in the implementing method?

Yes. Interface methods are public, and an implementation with weaker access fails to compile.