import ballerina/io;
type Person abstract object {
public int age;
public string firstName;
public string lastName;
function getFullName() returns string; function checkAndModifyAge(int condition, int a);
};
type Employee object {
public int age;
public string firstName;
public string lastName;
function __init(int age, string firstName, string lastName) {
self.age = age;
self.firstName = firstName;
self.lastName = lastName;
}
function getFullName() returns string {
return self.firstName + " " + self.lastName;
} function checkAndModifyAge(int condition, int a) {
if (self.age < condition) {
self.age = a;
}
}
};public function main() {
Person p = new Employee(5, "John", "Doe");
io:println(p.getFullName());
p.checkAndModifyAge(10, 50); io:println(p.age);
}
Abstract ObjectsIn Ballerina, objects can be abstract. Abstract objects only describe the fields and the signatures of the methods. An abstract object cannot be initialized and does not have a default value. |
|
|
|
Defines an abstract object called |
|
Method declarations can be within the object. However, the method cannot have a body. |
|
|
|
Defines a non-abstract object called |
|
Non-abstract objects can have initializers. |
|
Methods should have a body. |
|
|
|
|
An abstract object type cannot be initialized. It does not have an implicit initial value. |
|
|
Initializes a value using the non-abstract object |
|
|