Web Technology II · Unit 6
Object Oriented Programming
Exam-focused notes for Object Oriented Programming (Web Technology II, BIT301): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.
What this unit covers
- Class and object definition
- Object instantiation
- Class properties and attributes
- Class methods and access
- Constructors and initialization
- Inheritance and class hierarchy
- Interfaces and class differences
- Anonymous classes
- Encapsulation principles
Anonymous classes
What is anonymous class? Write a program to demonstrate the use of anonymous classes. [5]
An anonymous class is a class that is defined and instantiated in a single expression and is never given a name. PHP has supported them since version 7, written as new class { ... }. The value of the expression is an object, so the definition and the object...
Full solved answer →Describe an anonymous class using suitable example. [5]
An anonymous class is a class written directly at the place where its single object is needed, with no name attached to it. PHP creates the class and the object in one expression, new class { ... }, a form available since PHP 7. Before that the only way to ...
Full solved answer →Class and object definition
Write a program to create a class Employee with some attributes and methods. Create an object of the class and display some output using any method in the class. [5]
A class is the blueprint that describes what an employee record holds and what can be done with it, and an object is one concrete employee built from that blueprint. In PHP the properties are declared with a visibility keyword, the constructor is the magic ...
Full solved answer →How can you define class and object in PHP. Illustrate with example. [5]
A class is a blueprint or template that defines the properties (attributes) and behaviors (methods) of objects. It is a user-defined data type that encapsulates data and functions together. Syntax: An object is an instance of a class. When a class is define...
Full solved answer →Interfaces and class differences
How is class different from interface? Illustrate with an example. [5]
A class is a complete blueprint: it declares what an object holds and supplies the bodies of the methods that act on it. An interface is only a contract: it names the methods an implementing class must provide, without saying how any of them work. A class a...
Full solved answer →Constructors and initialization
Write a PHP program to demonstrate use of constructor. [5]
A constructor is a special method in a class that is automatically called when an object is created. In PHP, the constructor method is defined using construct(). --- --- --- Feature Description ------ construct() Special method called automatically on objec...
Full solved answer →Class properties and attributes
Write a PHP program to access properties and methods of a class. [5]
Concept Syntax Description --------- Define a class class ClassName { } Blueprint for objects Create an object $obj = new ClassName(); Instance of a class Access property $obj-propertyName Using arrow operator - Access method $obj-methodName() Using arrow o...
Full solved answer →Inheritance and class hierarchy
Write a PHP program to demonstrate the concept of inheritance. [5]
Inheritance is an OOP concept where a child class (derived class) acquires the properties and methods of a parent class (base class) using the extends keyword in PHP. --- --- --- Concept Description ------ extends Used to inherit parent class into child cla...
Full solved answer →Make Unit 6 stick
Practice BIT301 with flashcards & quizzes