ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to create an abstract class with data members

Here, we are going to learn how to create an abstract class with data members in Swift programming language?
Submitted by Nidhi, on July 07, 2021

Problem Solution:

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

Program/Source Code:

The source code to create an abstract class with data members 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 data members

abstract class Demo1 {
  var num: Int = 0;
  def sayHello();
}

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

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

    obj.num = 10;
    
    printf("Num: %d\n", obj.num);
    obj.sayHello();
  }
}

Output:

Num: 10
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, it contains an abstract method sayHello() and a data member num. Then we inherited the Demo1 class into the Demo2 class and implemented the sayHello() method.

In the main() function, we created the object of the Demo2 class. Then we called the sayHello() method and set and print the value num.

Scala Abstract 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.