Scala program to demonstrate the abstract class

Here, we are going to demonstrate the abstract class in Scala programming language.
Submitted by Nidhi, on July 07, 2021 [Last updated : March 11, 2023]

Scala – Abstract Class Example

Here, we will demonstrate the use of the abstract class. An abstract class contains the declaration of methods. To implement the abstract methods, we need to inherit the abstract class into a class.

Scala code to demonstrate the example of abstract class

The source code to demonstrate the abstract class is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to demonstrate the
// abstract class

abstract class Demo1 {
  def sayHello();
}

class Demo2 extends Demo1 {
  def sayHello() {
    println("Hello from Demo2");
  }
}

object Sample {
  def main(args: Array[String]) {
    var obj = new Demo2();
    obj.sayHello();
  }
}

Output

Hello from Demo2

Explanation

Here, we used an object-oriented approach to create the program. And, we created an object Sample.

Here, we created two classes Demo1 and Demo2. The Demo1 is an abstract class and it is inherited into the Demo2 class and implemented the sayHello() method.

In the main() function, we created the object of the Demo2 class and called sayHello() method.

Scala Abstract Classes Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.