An abstract class is a class that has to be inherited. You can not instantiate an abstract class. By the same token and abstract method has to be implemented by child classes. When you define an abstract method you don’t put any logic in it, you just define the parameters that the method is expected to receive, and possibly the expected response type. For instance
abstract class Foo
abstract def bar(text : String) : Array(String)
end
def Baz < Foo
def bar(text : String)
string.split(" ")
end
end
def Boof < Foo
# This will not compile because it doesn't implement `#bar`
end
def fun(foo : Foo)
pp foo.bar # This would work because the class `Foo` has a `#bar` method
end
Database adapters are a good example. You have a Base class that contains all the methods that the adapters need to include, then each adapter is free to do things their own way as long as they all have the necessary methods.
Because that’s what it is, it’s abstract. The literal definition of abstract is existing in thought or as an idea but not having a physical or concrete existence. I don’t think you can be much more clear than that.
It’s also a well known term in programming. Several languages have abstract classes and methods.
That’s weird to me. That definition of abstract does not even remotely define what it’s actually doing in code, though.
It’s setting requirements needed for an inheritance to function correctly? That’s neither existing in thought or an idea, it’s the totally opposite. It’s concretely and explicitly written out…
Sure it does. You are defining a class that exists only as a concept. You can’t actually instantiate it. Same with abstract methods. They can’t be used until they are implemented. They are abstract.
Anyway, the other point is that it’s a commonly used and accepted pattern in programming. Java and several other languages do the same thing.
It’s not defining a class or def that “exists only as a concept”. It’s defining a class and def, that is a requirement for something that wants to inherent from it.
(assuming I understand your Database adapters analogy correctly)?
It does exist only as a concept though. Abstract classes can’t be instantiated. They only exist as a framework for other classes. I can’t think of a word that would be better, but that’s just me.
abstract class Animal
property name = "Base Animal"
def initialize()
pp "THIS IS ONLY CALLED! AND I MEAN ONLY CALLED: IF Dog.new is executed"
end
end
class Dog < Animal
def say_hello
pp self.name
end
end
pet = Dog.new
pet.say_hello
Correct. Any abstract method must be defined by the child class, any non-abstract method will be automatically included in the child class and will be usable by it. A simpler example of an abstract class in action is Cadmium’s Tokenizer class.
module Cadmium
abstract class Tokenizer
abstract def tokenize(string : String) : Array(String)
def trim(arr)
if arr[0] == ""
arr.shift
end
if arr[-1] == ""
arr.pop
end
arr
end
end
end
As you can see it defines one abstract method tokenize which all tokenizers have to include. It also defines the method trim which all tokenizers will have by default.