Q:

Scala program to create an abstract class with the non-abstract method

belongs to collection: Scala Abstract Classes Programs

0

Here, we will demonstrate the use of abstract class with the non-abstract method. An abstract class can contain both abstract and non-abstract methods. To implement the abstract methods, we need to inherit the abstract class into a class.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create an abstract class with a non-abstract method is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to create an abstract class 
// with a non-abstract method

abstract class Demo1 {
  def sayHi() {
    printf("Hiii\n");
  }
  def sayHello();

}

class Demo2 extends Demo1 {
  def sayHello() {
    printf("Hello\n");
  }
}

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

    obj.sayHi();
    obj.sayHello();
  }
}

Output:

Hiii
Hello

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. It contain a non-abstract method sayHi() and an abstract method sayHello().

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala program to create an abstract class with the... >>
<< Scala program to create an abstract class with the...