PriyoScriptPriyoScript
OOP

Classes and OOP

Complete OOP guide for PriyoScript with instance/static/inheritance and API usage.

Theory

Object-Oriented Programming (OOP) organizes code around objects that contain state (data) and behavior (methods). In PriyoScript, classes are declared using lisaaFamily, objects are created using priyoCreate, current instance is accessed with priyoSelf, and inheritance is handled through lisaaInherit + priyoParent. OOP is useful when you want reusable, real-world models such as students, courses, wallets, accounts, and services.

Current OOP Features (Stable)

  • Class declaration with lisaaFamily - Object creation with priyoCreate - Constructor pattern using init(...) - Instance members via priyoSelf - Inheritance with lisaaInherit - Parent constructor/method access via priyoParent - Static methods/fields via lisaaStable - Public-by-default member access - Access modifiers (lisaaOpen, lisaaPersonal, lisaaGuarded)
  • Interface declaration (lisaaAgreement) and class contract (lisaaFollow)

OOP API Table

CategorySyntaxDescription
ClasslisaaFamily Name { ... }Declares a class blueprint
ConstructorlisaaTask init(...) { ... }Initializes object during creation
Instance methodlisaaTask method(...) { ... }Method callable on object instance
Instance fieldpriyoSelf.field = valueStores object-specific data
Object creationpriyoCreate Name(...)Creates class instance
InheritancelisaaFamily Child lisaaInherit Parent { ... }Child class extends parent
Parent constructorpriyoParent(...)Calls parent init from child
Static methodlisaaStable lisaaTask method(...)Class-level method
Static field usageClassName.fieldClass-level state
Public accessobj.field, obj.method()Members are public by default
Private accesslisaaPersonal ...Accessible only inside owner class
Protected accesslisaaGuarded ...Accessible in owner + subclasses
InterfacelisaaAgreement Name { ... }Declares class behavior contract
ImplementslisaaFamily A lisaaFollow IEnforces interface methods

Example 1: Basic Class + Instance Method

monalisa {
  lisaaFamily Student {
    lisaaTask init(name) {
      priyoSelf.name = name
    }

    lisaaTask intro() {
      priyoTell("I am " + priyoSelf.name)
    }
  }

  priyoKeep s = priyoCreate Student("mona")
  s.intro()
}

Explanation:

  • Student class defines one field (name) and one instance method (intro).
  • init(name) runs automatically during priyoCreate Student("mona").
  • priyoSelf.name stores the value for this specific object.
  • s.intro() reads object state and prints it. Output:
I am mona

Example 2: Static vs Instance API

monalisa {
  lisaaFamily MathUtil {
    lisaaStable lisaaTask square(x) {
      priyoGiveBack x * x
    }

    lisaaTask cube(x) {
      priyoGiveBack x * x * x
    }
  }

  priyoTell(MathUtil.square(5))
  priyoKeep m = priyoCreate MathUtil()
  priyoTell(m.cube(3))
}

Explanation:

  • square is static, so it is called on class: MathUtil.square(5).
  • cube is instance method, so object creation is required.
  • This separation helps keep utility methods static and object-specific behavior instance-based. Output:
25
27

Example 3: Inheritance + Parent Constructor

monalisa {
  lisaaFamily Person {
    lisaaTask init(name) {
      priyoSelf.name = name
    }
  }

  lisaaFamily Teacher lisaaInherit Person {
    lisaaTask init(name, subject) {
      priyoParent(name)
      priyoSelf.subject = subject
    }

    lisaaTask profile() {
      priyoTell(priyoSelf.name + " teaches " + priyoSelf.subject)
    }
  }

  priyoKeep t = priyoCreate Teacher("piue", "Math")
  t.profile()
}

Explanation:

  • Teacher extends Person, so it inherits parent behavior.
  • priyoParent(name) initializes inherited part first.
  • Child then sets its own field subject.
  • Final method combines parent + child data. Output:
piue teaches Math

Example 4: Public Member Access

monalisa {
  lisaaFamily Course {
    lisaaTask init(title, credits) {
      priyoSelf.title = title
      priyoSelf.credits = credits
    }
  }

  priyoKeep c = priyoCreate Course("Compiler Design", 4)
  priyoTell(c.title)
  priyoTell(c.credits)
}

Explanation:

  • PriyoScript class members are public by default in current stable model.
  • c.title and c.credits can be directly read outside class methods. Output:
Compiler Design
4

Example 5: Static Field Pattern

monalisa {
  lisaaFamily Counter {
    lisaaStable count = 0

    lisaaTask init() {
      Counter.count = Counter.count + 1
    }

    lisaaStable lisaaTask total() {
      priyoGiveBack Counter.count
    }
  }

  priyoCreate Counter()
  priyoCreate Counter()
  priyoTell(Counter.total())
}

Explanation:

  • count is class-level shared state, not per-object state.
  • Each new object increments same static field.
  • Counter.total() returns global count. Output:
2

Example 6: Interface + Access Modifiers

monalisa {
  lisaaAgreement Greeter {
    lisaaTask greet(name)
  }

  lisaaFamily Human lisaaFollow Greeter {
    lisaaPersonal priyoKeep code = "H-1"
    lisaaOpen lisaaTask greet(name) {
      priyoGiveBack "Hi " + name
    }
    lisaaOpen lisaaTask revealInside() {
      priyoGiveBack priyoSelf.code
    }
  }

  priyoKeep h = priyoCreate Human()
  priyoTell(h.greet("mona"))
  priyoTell(h.revealInside())
}

Explanation:

  • lisaaAgreement defines required methods for implementers.
  • Human lisaaFollow Greeter must implement greet(name) with matching parameter count.
  • code is lisaaPersonal, so it can only be accessed inside Human methods.

Output:

Hi mona
H-1

Practice Questions

Concept Check

  1. When should a method be static instead of instance-level? 2. Why must priyoParent(...) be called first in child init? 3. What is the practical difference between priyoSelf.name and ClassName.count?

Last updated on

On this page