0% found this document useful (0 votes)
20 views15 pages

CSC 302 (Introduction to OOP)

The document provides an introduction to Object-Oriented Programming (OOP), outlining its principles: encapsulation, abstraction, inheritance, and polymorphism. It explains the concepts of objects and classes, emphasizing the importance of state and behavior in real-world objects and how they relate to programming. Additionally, it discusses the benefits of using OOP, such as modularity, information-hiding, code re-use, and ease of debugging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

CSC 302 (Introduction to OOP)

The document provides an introduction to Object-Oriented Programming (OOP), outlining its principles: encapsulation, abstraction, inheritance, and polymorphism. It explains the concepts of objects and classes, emphasizing the importance of state and behavior in real-world objects and how they relate to programming. Additionally, it discusses the benefits of using OOP, such as modularity, information-hiding, code re-use, and ease of debugging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CSC 302

(Introduction to OOP)

Course Instructor: Mr. Umar Ibrahim


Adopted from oracle java documentation: https://docs.oracle.com/javase/tutorial/java/concepts/index.html
What is OOP?
Object-oriented programming(OOP) is a paradigm of programming that uses the idea
of objects. Objects have State(fields) and behavior(methods). The fields hold data
(variables) while the methods perform a function on that data(change the state of
objects)

The four principles of object-oriented programming are:


1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
Source: https://www.imaginarycloud.com/blog/the-dos-and-donts-of-oop/
Objects
Real-world objects share two characteristics:

State and behavior.

Some books refer to data as attributes or fields

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).

Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear,
changing pedal cadence, applying brakes).

Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of
object-oriented programming.

Can we think of real world object by identifying their state and behavior?
An object stores its state in fields (variables in some programming languages) and exposes its behavior
through methods (functions in some programming languages). Methods operate on an object's internal state
and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring
all interaction to be performed through an object's methods is known as data encapsulation — a
fundamental principle of object-oriented programming.
Benefits of using objects
1. Modularity: The source code for an object can be written and maintained independently of the source
code for other objects. Once created, an object can be easily passed around inside the system.
2. Information-hiding: By interacting only with an object's methods, the details of its internal
implementation remain hidden from the outside world.
3. Code re-use: If an object already exists (perhaps written by another software developer), you can use
that object in your program. This allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.
4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply
remove it from your application and plug in a different object as its replacement. This is analogous to
fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
Class
A class is the blueprint from which individual objects are created.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other
bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and
therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Bicycle class is one possible implementation of a bicycle:


The fields cadence, speed, and gear represent the
object's state.

The methods (changeCadence, changeGear,


speedUp etc.) define its interaction with the outside
world.

You may have noticed that the Bicycle class does not contain
a main method. That's because it's not a complete application;
it's just the blueprint for bicycles that might be used in an
application. The responsibility of creating and using new
Bicycle objects belongs to some other class in your
application.
Here's a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:

The output of this test prints the ending pedal


cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2


cadence:40 speed:20 gear:3
Objects and Classes
A class is a template, a blueprint or a guide from which objects are built or made. Objects are created from
a class. A class can have so many objects so we can say many objects can belong to a class.

Analogy

A BMW is an object of class car, A Honda, Peugeot and Toyota are all objects of class car because they
are all types of cars.

A class rep is an object of class Student.

Blue is an object of class color.

Malaria is an object of class Disease

FUD is an object of class University.


class University {
Example of a class
int idnum; //University ID

String Uname; //name of the university

String uniLocation; //Location of the university

int no_Of_students; //No of students in the university


}

Class car {

String cartype; // type of car

String carColor; // color of the car

Int numOfDoors; //number of doors on the car

}
class UniversityMain {
class carMain {
public static void main(String args[]) {
public static void main(String args[]) {

University uni1= new University ();


University uni1= new University ();
int range; // assign values to fields in minivan
int range; // assign values to fields in minivan
uni1.idnum = 582;
uni1.idnum = 582;
uni1.Uname = “Federal Uni Dutse”;
uni1.Uname = “Federal Uni Dutse”;
uni1.no_Of_students = 15000;
uni1.no_Of_students = 15000;

}
}
constructor
Is used for creating objects of a class. A constructor is the channel by which
classes can communicate.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy