ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to demonstrate the final class

Here, we are going to demonstrate the final class in Scala programming language.
Submitted by Nidhi, on July 07, 2021

Problem Solution:

Here, we will demonstrate the use of the final class. If we created a final class then it cannot be inherited. To create the final class, we need to use the final keyword.

Program/Source Code:

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

// Scala program to demonstrate the 
// final class

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

// Final class cannot be inherited.
// class Demo2 extends Demo1
class Demo2 {
  def sayHello() {
    println("Hello from Demo2");
  }
}

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

    obj1.sayHello();
    obj2.sayHello();
  }
}

Output:

Hiii from Demo1
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. Both classes contain sayHello() methods. The Demo1 is a final class and it cannot be inherited.

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

Scala Final Variables, Methods & Classes Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.