JS++ | Abstract Classes and Methods
Last Updated :
04 Sep, 2018
We have explored virtual methods and 'overwrite' (early binding) and 'override' (late binding) which allow us to define base implementations for a method and more specific implementations of the method in subclasses. However, what do we do if there is no relevant base implementation that makes sense? Consider a 'talk' method. While a 'Dog' will "woof" and a 'Cat' will "meow, " what will the 'Animal' base class do? Abstract classes and methods allow us to solve this problem.
When a class is declared with the 'abstract' modifier, it cannot be instantiated. Furthermore, it allows the class to have "abstract methods." Abstract methods are methods that don't define an implementation, and the implementation is left to the derived classes. The derived classes must implement all abstract methods when inheriting from an abstract class; otherwise, we'll get a compile error.
Let's start by making our 'Animal' class in Animal.jspp abstract and declaring an abstract 'talk' method:
external $;
module Animals
{
abstract class Animal
{
protected var $element;
private static unsigned int count = 0;
protected Animal(string iconClassName) {
string elementHTML = makeElementHTML(iconClassName);
$element = $(elementHTML);
Animal.count++;
}
public static unsigned int getCount() {
return Animal.count;
}
public virtual void render() {
$("#content").append($element);
}
public abstract void talk();
private string makeElementHTML(string iconClassName) {
string result = '<div class="animal">';
result += '<i class="icofont ' + iconClassName + '"></i>';
result += "</div>";
return result;
}
}
}
If we try to compile right now, we'll get compiler errors demanding that the subclasses of 'Animal' implement the 'talk' abstract method.
Let's start with Cat.jspp:
import Externals.DOM;
external $;
module Animals
{
class Cat : Animal
{
string _name;
Cat(string name) {
super("icofont-animal-cat");
_name = name;
}
final void render() {
$element.attr("title", _name);
super.render();
}
final void talk() {
alert("Meow!");
}
}
}
We import 'Externals.DOM' because this module provides all the 'external' declarations for the DOM (Document Object Model) API for browsers. This allows us to use the 'alert' function which will pop up a message box with what we want the cat to say ("Meow!"). We could have also just declared 'alert' as 'external'. Notice that we used the 'final' modifier to override 'talk', but we could have just as well used 'override'.
We're going to implement Dog.jspp similarly, but dogs make a different sound:
import Externals.DOM;
external $;
module Animals
{
class Dog : Animal
{
string _name;
Dog(string name) {
super("icofont-animal-dog");
_name = name;
}
final void render() {
$element.attr("title", _name);
super.render();
}
final void talk() {
alert("Woof!");
}
}
}
Now let's implement Panda.jspp. Pandas can bark, so let's make it bark:
import Externals.DOM;
external $;
module Animals
{
class Panda : Animal
{
Panda() {
super("icofont-animal-panda");
}
final void talk() {
alert("Bark!");
}
}
}
One last class to implement: 'Rhino'. Let's assume rhinos don't make a sound. It's perfectly fine to just override the abstract method and have it do nothing:
external $;
module Animals
{
class Rhino : Animal
{
Rhino() {
super("icofont-animal-rhino");
}
final void talk() {
}
}
}
Notice that we didn't import 'Externals.DOM' either. We didn't need it. Our rhino doesn't talk so we don't need the 'alert' function for showing a message box.
At this point, your project should be able to compile without errors. However, our animals don't actually talk yet. We need some kind of event to trigger the animal talking. For example, we might want an animal to talk when we click on it. To achieve this, we need event handlers, and this is the subject of the next section.
Similar Reads
Introduction of Object Oriented Programming As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functi
6 min read
R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti
4 min read
Features of C Programming Language C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system.The main features of C language include low-level access to memory, a simple set of keywords, and a clean style
3 min read
Introduction to Programming Languages Introduction: A programming language is a set of instructions and syntax used to create software programs. Some of the key features of programming languages include: Syntax: The specific rules and structure used to write code in a programming language.Data Types: The type of values that can be store
13 min read
type() function in Python The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read
Structures in C++ C++ Structures are used to create user defined data types which are used to store group of items of different data types.SyntaxBefore using structure, we have to first define the structure using the struct keyword as shown:C++struct name{ type1 mem1; type2 mem2; ... };where structure name is name an
8 min read
Difference between Shallow and Deep copy of a class Shallow Copy: Shallow repetition is quicker. However, it's "lazy" it handles pointers and references. Rather than creating a contemporary copy of the particular knowledge the pointer points to, it simply copies over the pointer price. So, each of the first and therefore the copy can have pointers th
5 min read
Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google
2 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read
Shallow Copy and Deep Copy in C++ In general, creating a copy of an object means to create an exact replica of the object having the same literal value, data type, and resources. There are two ways that are used by C++ compiler to create a copy of objects.Copy ConstructorAssignment Operator// Copy ConstructorGeeks Obj1(Obj);orGeeks
6 min read