0% found this document useful (0 votes)
3 views

Lecture 4 Object Oriented Programming

This document introduces key concepts of object-oriented programming, including objects, classes, inheritance, interfaces, and packages. It explains the characteristics of objects, the importance of encapsulation, and the benefits of using software objects such as modularity and code reusability. Additionally, it covers class structures, variable types, access modifiers, method definitions, and the role of Java packages in organizing classes and interfaces.

Uploaded by

wilfrednalima285
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)
3 views

Lecture 4 Object Oriented Programming

This document introduces key concepts of object-oriented programming, including objects, classes, inheritance, interfaces, and packages. It explains the characteristics of objects, the importance of encapsulation, and the benefits of using software objects such as modularity and code reusability. Additionally, it covers class structures, variable types, access modifiers, method definitions, and the role of Java packages in organizing classes and interfaces.

Uploaded by

wilfrednalima285
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/ 28

Object-Oriented

Programming Concepts
Objectives
• This lesson will introduce you to:
• Objects
• Classes
• Inheritance
• Interfaces
• Packages
Objects
• Objects are key to understanding object-oriented technology.
• An object is a software bundle of related state and behavior
• Software objects are often used to model the real-world objects that
you find in everyday life.
• Real-world objects share two characteristics
• have state and behavior
• Examples of objects:
• Dogs:
• state (name, color, breed, hungry)
• behavior (barking, fetching, wagging tail).
Objects
• Examples of Objects:
• Bicycles:
• State (current gear, current pedal cadence, current speed)
• Behavior (changing gear, changing pedal cadence, applying brakes).
Software objects
• A software object:
• stores its state in fields (variables)
• exposes its behavior
through methods (functions in some
programming languages).
• Methods:
• operate on an object's internal state
• serve as the primary mechanism for object-
to-object communication
Software objects
• Hiding internal state and requiring all
interaction to be performed through an
object's methods is known as data
encapsulation
• Encapsulation is a fundamental principle of
object-oriented programming.
• Encapsulation allows an object to be in
control of how the outside world is
allowed to use it
A bicycle modeled as a software object
Benefits of software objects in programming
• 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.
• Information-hiding:
• By interacting only with an object's methods, the details of its internal implementation
remain hidden from the outside world.
• Code re-use:
• If an object already exists you can use that object in your program
• allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to
run in your own code.
• 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.
Classes
• A class is the blueprint from which individual objects are created
• An object is an instance of a particular class

class MyClass {
// field, constructor, and
// method declarations
}
Classes
Classes
In general, class declarations can include these components, in order:
• Modifiers such as public, private, and a number of others that you will
encounter later.
• The class name, with the initial letter capitalized by convention.
• The name of the class's parent (superclass), if any, preceded by the
keyword extends. A class can only extend (subclass) one parent.
• A comma-separated list of interfaces implemented by the class, if any,
preceded by the keyword implements.
• A class can implement more than one interface.
• The class body, surrounded by braces, {}.
Classes: Variable types
A class can contain any of the following variable types:
• Local variables:
• defined inside methods, constructors or blocks are called local variables
• destroyed when the method has completed.
• Instance variables:
• variables within a class but outside any method
• initialized when the class is instantiated
• can be accessed from inside any method, constructor or blocks of that particular
class.
• Class variables:
• declared within a class, outside any method, with the static keyword.
• Parameters:
• Variables in method declarations
All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.
All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.

Things to note
• All variables must have a type.
• You can use primitive types such as int, float, boolean, etc.
• Or you can use reference types, such as strings, arrays, or objects.
• the first letter of a class name should be capitalized
• the first (or only) word in a method name should be a verb.
Access Modifiers

• public modifier—the field is accessible from all classes.


• private modifier—the field is accessible only within its own class.
• In the spirit of encapsulation, it is common to make fields private.
Defining Methods
Defining Methods
• The only required elements of a method declaration are the method's return
type, name, a pair of parentheses, (), and a body between braces, {}.
• More generally, method declarations have six components, in order:
1. Modifiers—such as public, private, etc..
2. The return type—the data type of the value returned, or void if the
method does not return a value.
3. The method name
4. The parameter list in parenthesis—
• a comma-delimited list of input parameters, preceded by their data types, enclosed by
parentheses, ().
• If there are no parameters, you must use empty parentheses.
5. An exception list—to be discussed later.
6. The method body, enclosed between braces—the method's code, including
the declaration of local variables, goes here.
Method Signature
• Comprises the method's name and the parameter types.
Overloading Methods
• methods within a class can have
the same name if they have
different parameter lists

• Note: Overloaded methods


should be used sparingly, as they
can make code much less
readable.
Class Constructors
• This is a method you can
use to set initial values for
field variables
• When the object is created,
Java calls the constructor
first.
• Constructor methods take
the same name as the class
• Constructors have no return
type
Providing Constructors for Your Classes
• A class contains constructors that are invoked to create objects from
the class blueprint
• A class can have more than one constructor
• To create a new object called, a constructor is called by the new
operator:
Providing Constructors for Your Classes
• new Bicycle(30, 0, 8)
creates space in
memory for the
object and initializes
its fields.
Providing Constructors for Your Classes
Task
• Create a class Student, with the following instance fields:
• age
• Registration number
• Programme
• YearOfStudy

• specify a constructor for the class


• How would the main method look if your had to create an object John with
the following:
• Age=20
• Registration number BSC/27/70
• Programme – BSc Chemistry
• Year of study 4
Accessing Instance Variables and Methods
• Instance variables are accessed through created objects
Example
Source File Declaration Rules
• There can be only one public class per source file.
• A source file can have multiple non-public classes.
• The public class name should be the name of the source file as well which should
be appended by .java at the end.
• For example: the class name is public class Employee{} then the source file should be as
Employee.java.
• If the class is defined inside a package, then the package statement should be the
first statement in the source file.
• If import statements are present, then they must be written between the
package statement and the class declaration.
• If there are no package statements, then the import statement should be the first line in the
source file.
• Import and package statements will imply to all the classes present in the source
file.
Java Packages
• A java package is a group
of similar types of classes,
interfaces and sub-
packages.
• can be categorized in two
form, built-in package
and user-defined
package.
Advantage of Java Package
• 1)Java package is used to categorize the classes and interfaces so that
they can be easily maintained.
• 2) Java package provides access protection.
• 3) Java package removes naming collision.
Reference
• Classes & Objects:
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

You might also like