L a N e t

Loading

Nigeria's free digital learning hub. Videos, lecture notes, practice questions and leaderboards aligned with CCMAS, NBTE, NCCE, WAEC, NECO, IJMB and JAMB curricula.

  • 100% Free — No Subscription. No Paywall. Ever.

COS 202 — Computer Programming II: Lecture Note

Computer Science · 200 Level

About this lecture note

This is the complete lecture note for COS 202 Computer Programming II, prepared for Computer Science students at 200 Level. You can read it online below, open it in the full viewer, or download the PDF for offline study. It pairs with the course's video lectures, so you can read and watch together.

Preview: opening of the note

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS Proposed Outline for Series 1: Advanced Object-Oriented Programming Foundations Part 1: Object Oriented Programming Review and Conceptual Foundations 1.1 Overview of Object-Oriented Programming 1.2 Review of Classes, Objects, and Methods 1.3 Introduction to Object Relationships, including composition and aggregation Part 2: Polymorphism and Dynamic Method Behaviour 1.1 Concept of Polymorphism 1.2Method Overriding and Dynamic Binding 2.3 Practical Applications of Polymorphism in Problem Solving Part 3: Abstract Classes and Interfaces 3.1 Purpose and Characteristics of Abstract Classes 3.2 Defining and Using Interfaces in Program Design 3.3 Class Hierarchies and Interface-Driven Architectures Part 4: Building and Managing Class Hierarchies 4.1 Principles of Inheritance and Hierarchical Structuring 4.2 Designing Reusable and Extensible Class Families 4.3 Best Practices for Object Oriented Programme Organisation Introduction Object oriented programming has become one of the most widely used approaches for building real world software, because it allows developers to model problems more naturally and to create solutions that are easier to extend and maintain. As you proceed in this course, it is important to strengthen your understanding of the advanced features that give object oriented programming its power, so that you can develop applications that are both flexible and robust. This Series prepares you for that next step by guiding you through some of the most essential concepts that support modern programme design. The aim of this Series is to deepen your understanding of advanced object oriented programming principles and to equip you with the ability to design, organise, and extend class based structures using appropriate techniques. We shall commence this Series by revisiting the fundamental principles of object oriented programming in order to establish a clear conceptual foundation. Next, we will explore polymorphism in greater depth, focusing on dynamic method behaviour and its use in solving practical problems. Following that, we will study abstract classes and interfaces, and how they support the design of structured and consistent class hierarchies. Finally, we will wrap up by examining how class hierarchies are designed and managed in ways that support reuse, extension, and clarity in programme organisation.

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS Series Learning Outcomes Upon completing this Series, you should be able to: SLO 1.1 define key concepts related to advanced object oriented programming foundations, SLO 1.2 explain how polymorphism influences method behaviour and programme flexibility, SLO 1.3 describe the characteristics and purposes of abstract classes and interfaces, SLO 1.4 analyse how class hierarchies support organised and reusable programme structures, and SLO 1.5 design simple class hierarchies that apply principles of inheritance, abstraction, and polymorphism. 1.1 Object Oriented Programming Review and Conceptual Foundations 1.1.1 Overview of object oriented programming Object oriented programming, often referred to as OOP, is a programming approach that organises software around objects, which are entities that combine data and behaviour in a single structure. An object is a unit in a programme that contains attributes, which represent its data, and methods, which represent its behaviour. This approach enables programmers to model real world concepts more naturally, because objects can interact with one another in ways that resemble everyday relationships. OOP supports key ideas such as abstraction, encapsulation, inheritance, and polymorphism. These ideas help in designing systems that are easier to manage and extend over time. Figure 1.1: Simple visual representation of object structure

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS To understand OOP clearly, think of a simple example such as a student record in an academic system. A student can have attributes such as name, ID number, and level, and can perform behaviours such as registering for courses or checking results. By representing these as an object, the programmer ensures that the data and the operations on that data remain logically connected. Fill in the blank: A programming approach that organises software around objects containing both data and behaviour is called _________. 1.1.2 Review of classes, objects, and methods A class is a blueprint or template that defines the structure and behaviour of objects. It specifies the attributes and methods that every object created from it will possess. An object is therefore an instance of a class, meaning it is a concrete realisation of the class blueprint stored in memory. A method is a function defined inside a class that describes an action an object can perform. These three elements form the foundation of all object oriented systems, and understanding how they work together is essential for advanced topics in this course. Table 1.1: Comparison of Classes, Objects, and Methods Concept Explanation Role in Object-Oriented Programming Class A blueprint or template that defines the structure and behaviour of objects. It specifies attributes and methods. Provides the design from which objects are created. It organises related data and behaviours in a single unit. Object An individual instance created from a class. It contains actual data values and can perform actions defined by the class. Represents a real entity in a programme. Each object maintains its own state and interacts with other objects. Method A function defined inside a class that describes actions an object can perform. Allows objects to behave in meaningful ways, modify their state, or interact with other objects. When a programmer writes a class, they are defining a model. When an object is created from that class, the programme allocates memory for it and assigns values to its attributes. For example, if you have a class called Car, it may have attributes such as colour and speed, and methods such as accelerate or brake. Each specific car the programme creates, such as a red car or a blue car, is an object of that class. Fill in the blank:A method in a class describes an _________ that an object can perform.

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS 1.1.3 Introduction to object relationships, including composition and aggregation In object oriented design, it is often necessary for objects to relate to or work with other objects. An object relationship refers to a structured connection between two or more objects, indicating how they depend on or interact with one another. Two common forms of object relationships are composition and aggregation. Composition is a relationship where one object is made up of one or more other objects, and the lifetime of the contained objects depends on the lifetime of the whole. In other words, if the main object is destroyed, its composed parts are also destroyed. For example, a House object may be composed of Room objects, because the rooms cannot normally exist independently of the house. Aggregation is a relationship where one object uses or contains another object, but the contained object can exist independently. For instance, an Employee object may be associated with a Department object, but if the employee leaves the organisation, the department still exists. Aggregation is therefore a weaker form of relationship compared to composition. Box 1.1: Examples of composition and aggregation in typical applications These relationships help programmers design systems where responsibilities are distributed clearly across objects. When designing your own applications, it is important to identify which objects depend on others and which ones operate independently. This clarity improves the structure of your class hierarchies and helps prevent errors or unnecessary duplication of code.

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS Fill in the blank: A relationship where an object is made up of other objects that cannot exist independently is called _________. Pilot questions 1.1 1. Which programming paradigm organises software around objects containing both data and behaviour? 2. What is the main difference between a class and an object? 3. Which term describes a function defined inside a class? 4. Which relationship results in the destruction of contained objects when the main object is destroyed? 5. Which relationship allows the contained object to exist independently from the container? 1.2 Polymorphism and Dynamic Method Behaviour 1.2.1 Concept of polymorphism The term polymorphism refers to the ability of different objects to respond to the same method call in ways specific to their own classes. In simple terms, polymorphism allows one interface to represent many underlying forms. This becomes extremely useful in object oriented programming because it enables programmers to write code that is flexible, extendable, and easier to maintain. With polymorphism, a single method name can be applied to objects of different classes, and each object can provide its own implementation of that method.

LA-NET | ONLINE LEARNING VIDEO PLATFORM FOR POST-SECONDARY & HIGHER INSTITUTION STUDENTS Figure 1.2: Visual representation of polymorphism in object oriented programming By using polymorphism, programmers reduce the need for repeated code and allow for more generic processing of objects. For example, imagine a common method such as draw being applied to different shapes. A Circle object and a Rectangle object may share the same method name, but each produces its own unique behaviour based on its

This is the opening of the note. Read the full note or download the PDF to continue.