Vala Programming/Concepts/Objects

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Object Oriented Programming[edit]

The Vala OO System is very similar to, and is somewhat based on languages like C# and Java. Please note that unlike these languages, Vala does not force you to adopt and Object Oriented Paradigm, but it is highly recommended for maximum productivity; and because most third-party libraries and bindings use Object-Orientation, it will ultimately have to be adopted by you.


Generally Classes in Vala are declared like this:

/*Simple Class Derived From GLib.Object*/
public class Sample : GLib.Object {
    /*Class Fields*/    
    bool my_field; 
    int my_int_field = 2;

    public Sample () {
        /* Constructor */
    public ~Sample () {
        /* Destructor */
    public void my_method () {
        /*Method Code*/
    public int return_method () {
        /*Return Some Integer*/
        return my_int_field;
    public void param_method (int param) {
        /*Use Arguments*/
/*Intermediate Hacking...*/

Note that if you declare a base class, it is recommended to derive it from the GLib.Object, otherwise you will not be able to access some of its features.



Inheritance in Vala is very similar to C#. Vala supports single inheritance only, i.e you cannot inherit an object from more than one base class. The general format is:

/*Inheriting From Other Classes*/
public class Base : GLib.Object {
    public int member;
    public Base (int carg) {
        /*Code Here*/
public class Derived : Base {
    public Derived () {
        base (14); // Call base constructor
        /*Code Here*/



Abstract Classes[edit]