Question #442MediumDSAOOP ConceptsImportant

What will be the output of the following dart code? implements vs extends — difference between using it in class constructors and methods

#dart#oops#extends#implements#inheritance#interface#constructor

Answer

The Code

dart
main() {
  B b = B();
  D d = D();
}

class A {
  A() {
    print("Class A");
  }
  sampleMethodOfClassA() {
    print('Sample Method Of Class A');
  }
}

class B extends A {
  B() {
    print("Class B");
  }
}

class C {
  C() {
    print("Class C");
  }
  sampleMethodOfClassC() {
    print('Sample Method Of Class C');
  }
}

class D implements C {
  D() {
    print("Class D");
  }
}

Answer

This code will not compile. Class

text
D
uses
text
implements C
, which means
text
D
must provide its own implementation of every method defined in
text
C
, including
text
sampleMethodOfClassC()
. Since
text
D
does not override it, Dart throws a compile-time error.


Why Does
text
B
Work But
text
D
Doesn't?

text
extends
(Inheritance) — Class B

When

text
B extends A
:

  • B inherits all methods from A (including
    text
    sampleMethodOfClassA()
    )
  • The parent constructor is called automatically before the child constructor
  • B does not need to override inherited methods
dart
B b = B();
// Output:
// Class A    ← Parent constructor called first
// Class B    ← Then child constructor

text
implements
(Interface Contract) — Class D

When

text
D implements C
:

  • D treats C as an interface — it promises to provide the same API
  • D must override every single method from C (even concrete ones)
  • The parent constructor is NOT called
  • D gets no inherited implementation — only the contract
dart
// ❌ Compile Error: D has no implementation of sampleMethodOfClassC()
class D implements C {
  D() {
    print("Class D");
  }
}

How to Fix Class D

dart
class D implements C {
  D() {
    print("Class D");
  }

  
  sampleMethodOfClassC() {
    print('Sample Method Of Class C from D');
  }
}

Output After Fix

text
Class A
Class B
Class D

Note:

text
"Class C"
is never printed because
text
implements
does not invoke the parent constructor.


Key Differences:
text
extends
vs
text
implements

Feature
text
extends
text
implements
Parent constructor called?Yes (automatically)No
Inherits method implementations?YesNo
Must override all methods?Only abstract methodsAll methods (abstract + concrete)
Relationship"is-a" (inheritance)Interface contract
Multiple allowed?Single class onlyMultiple classes
Code reuse?YesNo (must rewrite everything)

Common Interview Follow-Up

dart
// extends → inherits body, only override what you want
class B extends A {
  // sampleMethodOfClassA() is inherited — no need to override
}

// implements → must provide ALL methods yourself
class D implements C {
  
  sampleMethodOfClassC() {
    // Must write your own implementation
  }
}

Key Takeaway: Use

text
extends
when you want to reuse a parent class's code. Use
text
implements
when you want to guarantee a class follows a specific API contract without inheriting any implementation.