
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - Method Overriding
This chapter takes you through method overriding in Scala programming. A subclass can provide a specific implementation of a method that is already defined in its superclass.
Method Overriding
A subclass can provide a specific implementation of a method that is already defined in its superclass. So, the subclass can modify and extend the behavior of the method. You can achieve runtime polymorphism, where a single interface can be used for different underlying forms (data types).
Method overriding is the process by which a method in a subclass has the same name, return type, and parameters as a method in its superclass. But it provides a different implementation.
Syntax
The syntax of the method overriding is -
class SuperClass { def methodName(param1: Type1): ReturnType = { // Superclass implementation } } class SubClass extends SuperClass { override def methodName(param1: Type1): ReturnType = { // Subclass implementation } }
Example
The following example demonstrates method overriding in Scala programming -
class Animal { def sound(): Unit = { println("Animal makes a sound") } } class Dog extends Animal { override def sound(): Unit = { println("Dog barks") } } object Demo { def main(args: Array[String]): Unit = { val animal: Animal = new Dog animal.sound() } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Dog barks
In the example, Dog class overrides the sound method of the Animal class using the override keyword. Demo object shows polymorphism by creating an instance of Dog. But referencing it with an Animal type, calling the overridden sound method.
Rules for Method Overriding
1. override Keyword
You must use the override keyword when overriding a method. So, you can avoid accidental overriding and have readable code.
2. Method Signature
The method signature in the subclass must exactly match the method signature in the superclass. This includes the method name, return type, and parameter list.
3. Access Modifiers
The access level of the overriding method must be the same and less restrictive than the method in the superclass.
Method Overriding with Different Parameter Lists
While overriding a method, the subclass can have methods with different parameter lists. But to override, the method signature must exactly match the superclass method.
Example
The following example shows method overriding with different parameter lists Scala programming -
class Vehicle { def start(): Unit = { println("Vehicle is starting") } def start(key: String): Unit = { println(s"Vehicle is starting with key: $key") } } class Car extends Vehicle { override def start(): Unit = { println("Car is starting") } override def start(key: String): Unit = { println(s"Car is starting with key: $key") } } object Demo { def main(args: Array[String]): Unit = { val vehicle = new Vehicle() val car = new Car() vehicle.start() // Vehicle is starting vehicle.start("VehicleKey") // Vehicle is starting with key: VehicleKey car.start() // Car is starting car.start("CarKey") // Car is starting with key: CarKey } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Vehicle is starting Vehicle is starting with key: VehicleKey Car is starting Car is starting with key: CarKey
In the example, Car class overrides the start methods of the Vehicle class. Demo object shows how the overridden methods are called on instances of Vehicle and Car.
Overriding with Covariant Return Types
There can be method overriding with covariant return types, where the return type of the overriding method is a subtype of the return type of the overridden method.
Example
The following example shows method overriding with covariant return types in Scala programming -
class Animal { def create(): Animal = { println("Creating an Animal") new Animal } } class Dog extends Animal { override def create(): Dog = { println("Creating a Dog") new Dog } } object Demo { def main(args: Array[String]): Unit = { val animal = new Animal() val dog = new Dog() val newAnimal: Animal = animal.create() // Creates an Animal val newDog: Dog = dog.create() // Creates a Dog } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Creating an Animal Creating a Dog
In the example, Dog class overrides the create method of the Animal class with a covariant return type. Demo object shows how the overridden method is called on instances of Animal and Dog.
Using Superclass Methods
You can call the superclass version of an overridden method using the super keyword. So you can access superclass method using super keyword.
Example
The following example shows how to access the superclass method using super keyword -
class Animal { def sound(): Unit = { println("Animal makes a sound") } } class Dog extends Animal { override def sound(): Unit = { super.sound() println("Dog barks") } } object Demo { def main(args: Array[String]): Unit = { val dog = new Dog dog.sound() } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Animal makes a sound Dog barks
In the example, Dog class overrides the sound method of the Animal class. It uses the super keyword to call the superclass version of the sound method before executing its own implementation.
Method Overriding Summary
- A subclass to provide a specific implementation for a method defined in its superclass.
- Overridden methods must have the same signature as the methods they override.
- Method overriding supports polymorphism, code reusability, and dynamic binding.
- There can be method overriding with covariant return types, where the return type of the overriding method is a subtype of the return type of the overridden method.
- The method signature in the subclass must match the method signature in the superclass.
- The super keyword can be used to call the superclass version of an overridden method.
- You should use the override keyword for clarity and to avoid errors.