পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, January 30, 2017

D - Abstract Classes

Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.

If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Abstract Class

Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword. The following shows an example of how abstract class can be inherited and used.
import std.stdio;
import std.string;
import std.datetime;

abstract class Person
{
   int birthYear, birthDay, birthMonth;
   string name;
   int getAge()
   {
       SysTime sysTime = Clock.currTime();
       return sysTime.year - birthYear;
   }
}

class Employee : Person
{
   int empID;
}

void main()
{
   Employee emp = new Employee();
   emp.empID = 101;
   emp.birthYear = 1980;
   emp.birthDay = 10;
   emp.birthMonth = 10;
   emp.name = "Emp1";
   writeln(emp.name);
   writeln(emp.getAge);
}
When we compile and run the above program, we will get the following output.
Emp1
34

Abstract functions

Similar to functions, classes can also be made abstract. The implementation of such function is not given in its class but should be provided in the class that inherits the class with abstract function. A above example is updated with abstract function and is given below.
import std.stdio;
import std.string;
import std.datetime;

abstract class Person
{
   int birthYear, birthDay, birthMonth;
   string name;
   int getAge()
   {
       SysTime sysTime = Clock.currTime();
       return sysTime.year - birthYear;
   }
   abstract void print();
}

class Employee : Person
{
   int empID;

   override void print()
   {
      writeln("The employee details are as follows:");
      writeln("Emp ID: ", this.empID);
      writeln("Emp Name: ", this.name);
      writeln("Age: ",this.getAge);
   }
}

void main()
{
   Employee emp = new Employee();
   emp.empID = 101;
   emp.birthYear = 1980;
   emp.birthDay = 10;
   emp.birthMonth = 10;
   emp.name = "Emp1";
   emp.print();
}
When we compile and run the above program, we will get the following output.
The employee details are as follows:
Emp ID: 101
Emp Name: Emp1
Age: 34

No comments:

Post a Comment