Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of allowing the declaration of an abstract method in a non-abstract class?

Tags:

oop

dart

According to this article, it's possible, in Dart, to define a non-abstract class to have an abstract (or not-implemented) method. The abstract method causes a warning, but does not prevent instantiation.

What's the purpose of allowing the declaration of an abstract method in a non-abstract (or concrete) class in Dart? Why was Dart designed to work in this way?

like image 638
nbro Avatar asked Aug 17 '17 14:08

nbro


People also ask

What is the purpose of declaring abstract methods?

In object oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and to focus on essential details (functionality). It increases the efficiency and thus reduces complexity.

Can you declare an abstract method in a non abstract class?

Abstract method declarations are only permitted in abstract classes. public abstract void MyMethod(); The implementation is provided by a method override, which is a member of a non-abstract class. It is an error to use the static or virtual modifiers in an abstract method declaration.

Why would you have a non abstract method in an abstract class?

An abstract class can extend only one class or one abstract class at a time. Declaring a class as abstract with no abstract methods means that we don't allow it to be instantiated on its own. An abstract class used in Java signifies that we can't create an object of the class directly.

Can't have an abstract method in a non abstract class the class?

A normal class(non-abstract class) cannot have abstract methods. In this guide we will learn what is a abstract class, why we use it and what are the rules that we must remember while working with it in Java. An abstract class can not be instantiated, which means you are not allowed to create an object of it.


1 Answers

The specification is actually very explicit about declaring abstract methods in a concrete class:

It is a static warning if an abstract member m is declared or inherited in a concrete class


We wish to warn if one declares a concrete class with abstract members.


It is a static warning if a concrete class has an abstract member (declared or inherited).

They don't have any intended purpose for it, which is why they issue warnings. If you're familiar with Java: it's similar to accessing a static member via an object, which is also pointless and triggers a warning.

As for why it passes compilation, Dart uses an optional type system, which means typing concepts should not affect the semantics of the language, and that's simply what Dart is enforcing:

The purpose of an abstract method is to provide a declaration for purposes such as type checking and reflection.


The static checker will report some violations of the type rules, but such violations do not abort compilation or preclude execution.

like image 97
Vince Avatar answered Sep 22 '22 13:09

Vince