Interfaces: introduction and declaration; inheriting and hiding concepts; inheriting, overloading and overriding methods and constructors; interface implementations

Interface એ pure contract છે: no bodies, no constructors, implicitly public abstract methods, અને class એક સાથે many implement કરી શકે છે, જે Java નો multiple inheritance નો answer છે.

12 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ત્રણ things જે print થવા જોઈએ

BookBridge હવે issued books, fine payments અને new membership cards માટે receipts print કરે છે. ત્રણ classes, family દ્વારા unrelated: FineReceipt એ MembershipCard ની kind નથી.

પણ dashboard એને identically treat કરવા માંગે છે: "everything printable, print yourself at closing time."

Inheritance આને express નથી કરી શકતું: કોઈ sensible common parent નથી, અને Java ફક્ત એક extends allow કરે છે anyway. આપણે જે જોઈએ છે એ shared ancestor નહીં પણ shared promise છે: અહીં sign કરો, અને તમે print કરી શકો.

Theory

contract, bloodline નહીં

Driving licence ને care નથી કે તમારા parents કોણ છે: test pass કરો, અને તમે drive કરી શકો. Doctors, students અને shopkeepers બધા એક hold કરે છે.

Interface એ classes માટે એ licence છે: abilities ની list જેની સાથે કોઈ implementation attached નથી. કોઈપણ family ની કોઈપણ class એને sign કરી શકે છે, અને signing એટલે એક thing: તમે actually આ methods ને provide કરવા જોઈએ. Family inherit થાય છે; contracts sign થાય છે. Java class ને એક family અને many contracts હોવા દે છે.

Theory

Declaration અને implicit rules

interface Printable {

void print();

}

શું compiler silently add કરે છે, અને exams ask કરે છે:

  • દરેક method implicitly public abstract છે: signature, no body
  • દરેક field implicitly public static final છે: constant, initialise થવું જોઈએ
  • કોઈ constructors નથી: new Printable() meaningless અને illegal છે

class implements સાથે sign કરે છે અને all methods ને define કરવા જોઈએ, અથવા પોતાને abstract declare કરવું જોઈએ:

class FineReceipt implements Printable { ... }

Practical

બે contracts, એક class

interface Printable {
    void print();                      // implicitly public abstract
}

interface Payable {
    void collect(int amount);
}

class FineReceipt implements Printable, Payable {   // many contracts at once
    public void print() {              // public અહીં COMPULSORY છે
        System.out.println("Fine receipt printed");
    }
    public void collect(int amount) {
        System.out.println("Collected Rs " + amount);
    }
}

public class BookBridge {
    public static void main(String[] args) {
        Printable p = new FineReceipt();   // interface reference
        p.print();
    }
}

Theory

multiple-inheritance debt, paid

પાછા Java-vs-C++ lesson માં, promise કરવામાં આવ્યું હતું: Java 2 parent classes ને forbid કરે છે પણ idea નો another route છે. આ એ છે.

class FineReceipt implements Printable, Payable બંને contracts ને take on કરે છે, અને interfaces પોતે inherit કરી શકે છે: interface Printable extends Displayable contract પર contract stack કરે છે.

શા માટે આ safe છે જ્યારે 2 parent classes નહોતા? Interfaces no state અને no method bodies carry કરે છે, એટલે C++ diamond problem (કયા parent નું data? કોની implementation?) ની પાસે collide કરવા માટે કંઈ નથી.

Quiz

class Card implements Printable print() ને declare કરે છે પણ public keyword ને forget કરે છે: void print() { ... } શું થાય છે?

  1. Compiles fine; default access package માં enough છે
  2. Compile error: implementing method public કરતાં weaker access નથી હોઈ શકતો
  3. Runtime error જ્યારે print() ને interface દ્વારા first call કરવામાં આવે
  4. Compiler silently તમારા માટે public add કરે છે
Show the answer

Compile error: implementing method public કરતાં weaker access નથી હોઈ શકતો

Interface methods implicitly public છે, અને override ક્યારેય visibility ને REDUCE નથી કરી શકતો, એટલે default access એ step down છે અને javac એને reject કરે છે: આ single most common compile error છે જ્યારે students પહેલી વાર interfaces implement કરે છે. Option A package rule ને એ context પર apply કરે છે જેને એ govern નથી કરતો. Option C too late છે: Java access ને compile time પર settle કરે છે. Option D describe કરે છે કે interfaces તેમની own declarations માટે શું કરે છે, તમારા implementing class માટે નહીં.

Think first

Interface vs abstract class

બંને abstract methods hold કરી શકે છે, કોઈ instantiate નથી થઈ શકતું. tap કરતા પહેલા: 2 differences નામ આપો જે decide કરે કે તમે ક્યારે interface ને pick કરશો.

Show the answer

1: how many. class at most 1 abstract class extend કરે છે પણ many interfaces implement કરે છે; જો Card ને Printable અને Payable બંને હોવું જોઈએ, તો interfaces એ only route છે. 2: શું contain કરી શકે. abstract class instance fields, constructors અને ordinary method bodies રાખી શકે છે જે children inherit કરે; interface ની પાસે એમાંથી કંઈ નથી, માત્ર contract (તેના fields public static final constants છે). Rule of thumb: shared implementation, abstract class; shared capability, interface.

Watch out

3 interface traps

new Printable() compile નથી થતું: no constructors, build કરવા માટે કંઈ નથી. Interface variable ફક્ત implementing object ને HOLD કરી શકે છે: Printable p = new FineReceipt();

Partial implementation: 3 માંથી 2 methods implement કરો અને class ને abstract declare કરવું જોઈએ, અથવા એ compile નહીં થાય.

extends vs implements: class to class એ extends, class to interface એ implements, interface to interface એ extends. એને swap કરવા એ instant compile error છે.

Theory

આ pattern Java world ને run કરે છે

Interface references families આરપાર polymorphism આપે છે: dashboard Printable references hold કરે છે અને print() ને call કરે છે without caring કે દરેક object actually શું છે, exactly Person p = new Member() જેમ પણ કોઈ bloodline required નથી. shape ને mind માં રાખો: Unit 4 માં, BookBridge ના due-date reminder ને background માં run કરવું literally class Reminder implements Runnable છે, એક more signed contract. Android (BCA305-02) એ દરેક button click ને same રીતે wire કર્યું હતું.

Summary

Key takeaways

  • Interface એ pure contract છે: method signatures, no bodies, no state, no constructors.
  • Methods implicitly public abstract છે; fields implicitly public static final છે.
  • class many interfaces implement કરે છે અને દરેક method ને public તરીકે define કરવું જોઈએ, અથવા abstract હોવું જોઈએ.
  • Interfaces interfaces ને extend કરે છે; classes interfaces ને implement કરે છે; classes એક class ને extend કરે છે.
  • Interface references (Printable p = new FineReceipt()) cross-family polymorphism આપે છે.
  • No state અને no bodies એટલે જ multiple contracts safe છે જ્યાં multiple parents નહોતા.
  • Memory hook: એક family, many licences.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Classes and Objects

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Interfaces: introduction and declaration; inheriting and hiding concepts; inheriting, overloading and overriding methods and constructors; interface implementations · Java Programming Language · Gri-Learn