Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Introduction to Programming Languages/Object Oriented Language Features

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Classes A class is a blueprint used to create objects. It defines: fields (state) methods (behavior) Key roles of classes: Group data + behavior Allow object creation (instantiation) Act as a type Serve as a unit for inheritance Provide a namespace (organizing code) Example - Java class Stack { private Node top; public boolean hasMore() { return top != null; } public void add(String data) { top = new Node(data, top); } }Here: top = state add, hasMore = behavior Python Example class Stack: def __init__(self): self.top = None def has_more(self): return self.top is not None def add(self, data): self.top = (data, self.top) SML (no native classes, but similar idea) fun stack top HasMore = case top of null => false | _ => trueBehavior is defined via functions instead of classes. Prolog (data + rules) has_more(stack([_|_])). add(stack(L), X, stack([X|L])).Objects are represented as structures. Prototypes A prototype is an object used as a template to create new objects by copying it. Key idea: Clone → modify → use Python Example import copy stack1 = {"top": None} stack2 = copy.copy(stack1) stack2["top"] = "new value" JavaScript Style (conceptual) let stack1 = { top: null }; let stack2 = Object.create(stack1); stack2.top = "value"; SML (conceptual cloning) val stack1 = [1,2,3] val stack2 = 0 :: stack1Not true prototypes, but shows reuse via copying. Prolog clone(stack(L), stack(L)). Inheritance Inheritance allows a child class to reuse and extend a parent class. What is inherited? Methods Fields Behavior Java Example class Stack { String top() { return "top"; } } class PeekableStack extends Stack { String peek() { return top(); } } Python Example class Stack: def top(self): return "top" class PeekableStack(Stack): def peek(self): return self.top() SML (message forwarding) fun peekableStack top Peek = top GetData | peekableStack top msg = stack top msg;Unknown messages are forwarded (like inheritance) Prolog top(stack([H|_]), H). peek(S, X) :- top(S, X).Reuse via rules Encapsulation Encapsulation is the practice of hiding internal details and exposing only a controlled interface. Why it matters: Prevents misuse Protects invariants Improves modularity Java (strong encapsulation) class Node { private String data; public String getData() { return data; } } Python (convention-based) class Node: def __init__(self): self._data = "secret" SML (lexical scoping) fun makeNode data = let val hidden = data in fn () => hidden endhidden is inaccessible outside Prolog Encapsulation via module boundaries (if supported) Polymorphism Polymorphism allows different objects to respond to the same method name. Types (simplified): Same interface Different implementations Java Example interface Shape { void draw(); } class Circle implements Shape { public void draw() { System.out.println("Circle"); } } class Square implements Shape { public void draw() { System.out.println("Square"); } }void render(Shape s) { s.draw(); } Python Example class Circle: def draw(self): print("Circle") class Square: def draw(self): print("Square") def render(shape): shape.draw() SML fun draw shape = case shape of "circle" => "Circle" | "square" => "Square" Prolog draw(circle). draw(square). Dynamic Dispatch Dynamic dispatch is the process where the system determines at runtime which method to execute. Key Idea: You don’t manually check types — the system does it Java Example Shape s = new Circle(); s.draw(); // calls Circle's version Python Example shape = Circle() shape.draw() Exam Insights (Important) Classes = structure + behavior Prototypes = cloning instead of classes Inheritance = reuse + extension Encapsulation = information hiding Polymorphism = same interface, different behavior Dynamic dispatch = runtime method selection