0% found this document useful (0 votes)
25 views3 pages

Extends and Implements

Uploaded by

prabathkotti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Extends and Implements

Uploaded by

prabathkotti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXTENDS AND IMPLEMENTS

extends

Defintion:

"Extends" is used to create a class hierarchy where one class (subclass) inherits
properties and methods from another class (superclass). It establishes an "is-a"
relationship, meaning a subclass is a type of the superclass.

When to use "extends":

Use "extends" when you want to create a new class that builds upon or specializes an
existing class. This allows you to reuse code and inherit behavior from the superclass.

In Apex, you can use the extends keyword to create a subclass that inherits properties and
methods from a superclass. Here's an example:

Example Code:

Animal Class

public virtual class Animal {

public virtual void speak() {

System.debug('Animal speaks');

Dog Class

public class Dog extends Animal {

public override void speak() {

System.debug('Dog barks');

}
Cat Class

public class Cat extends Animal {

public override void speak() {

System.debug('Cat meows');

In this example, the Dog and Cat classes extend the Animal class, which means they
inherit the speak method from the Animal class. However, each subclass can override the
method to provide its own implementation, as demonstrated with the speak method in
both Dog and Cat classes.

implements

Definition:

"Implements" is used to declare that a class adheres to a specific interface. An interface is


a contract that defines a set of method signatures that must be implemented by the class.
It's a way to ensure that a class provides specific functionality.

When to use "implements":

Use "implements" when you want to enforce a contract for method implementations
across multiple classes. It's useful when you want multiple classes to have the same
method signatures but potentially different implementations.

In Apex, you can define an interface with a set of method signatures, and then a class can
implement that interface by providing concrete implementations for those methods.
Here's an example:

Example Code:

Shape Class

public interface Shape {

void draw();

}
Circle Class

public class Circle implements Shape {

public void draw() {

System.debug('Drawing a circle');

Rectange Class

public class Rectangle implements Shape {

public void draw() {

System.debug('Drawing a rectangle');

In this example, the Circle and Rectangle classes both implement the Shape interface by
providing their own implementation of the draw method.

Note: Interface methods do not have access modifiers, they are always global.

Summary:

"extends" is used for creating class hierarchies and inheriting properties and methods,
while "implements" is used for defining interfaces and ensuring that classes provide
specific methods. Use "extends" when you want to establish an inheritance relationship,
and "implements" when you want to define a common set of methods that multiple
classes must implement.

You might also like