Lecture 02 - Thinking in Obejcts
Lecture 02 - Thinking in Obejcts
Thinking in Objects
SE116 - Introduction to Programming II
Previously on SE116
● Last week, we mainly covered an introduction to Object Oriented
Programming.
○ We talked about abstraction, encapsulation, modularity.
○ We revisited the declaration of a “class”. We mentioned that a “class” is declared to represent
a particular data type. We also mentioned that an “object” is an instance of a “class” type.
○ While declaring a “class”, we need to consider encapsulation (that is, information hiding). The
information (i.e., data members) within the class scope should be specified “private” and the
corresponding behaviours (methods) should be specified “public”.
● Have you studied the previous week’s lecture and lab materials?
● Do you have any question regarding any previous subject?
Previously on SE116
● Checkpoint#1: To implement a simple Java application, are you able to
declare a class “Student” and then instantiate a few number of new objects
from Student reference type? Are you also able to declare and define an array
of 5 Student references?
● Checkpoint#2: What are the primitive types in Java programming language?
● Checkpoint#3: Can you give two names as reference types available in Java?
● Checkpoint#4: Is a variable of type “double” a reference variable? Is a
variable of type “Double” a reference variable?
● Checkpoint#5: Is an array (allocated in memory) an object? Is an array
identifier in a Java program a reference variable?
How Do We Think in Objects?
● From now on, throughout the semester, we will be thinking in objects. We will be
using classes. We will implement solutions to our problems using an Object
Oriented Programming (OOP) paradigm.
● Regarding an Object Oriented (OO) approach, we need to think how we can use
the objects and classes to achieve effective solutions.
● A “class” type is our reference type. The objects to be allocated in the memory at
run-time using the “new” keyword would be referred by their reference variables
in our programs.
● To solve our puzzles (that is, our problems) via Object Oriented Programming,
we can think in objects and play with the objects in an appropriate way (that is,
we can implement applications using the objects to properly solve the problems).
Object Oriented Programming (OOP)
● What is so special about “object-oriented” programming?
○ Currently almost all popular programming languages are object oriented: Java, C#, Python, JavaScript…
● What are the other paradigms?
○ Procedural programming languages: C
○ Functional programming languages: Haskell, Lisp, Erlang
● What did OOP do differently?
○ Humans tend to think in objects.
○ As the name suggests, there are “objects” in OOP.
○ It is much easier to design programs (or software) as a human in OOP.
● What are the main features of an Object Oriented Programming Language?
○ An object oriented programming language should provide the following features (at least):
■ encapsulation,
■ inheritance,
■ polymorphism.
Object Oriented Programming
● When we want to find the average score in a class, we use data structures
such as an array, a for loop, and a few arithmetic operations to write our
program.
● What if we also want to keep student information?
● Consider a program where we want to keep the project, midterm, and final
exam scores, report their average for each specific student, find the most
overall successful student, or the most successful student in the final?
● Immediately we notice “student” - an object.
● We can say that each score belongs to a student.
Object Oriented Programming
● We can also think of a “score” as another type of object.
● The score can belong to a project, midterm, homework assignment, final
exam, etc.
● It can have its own weight with regards to the final grade of the student.
○ For example, the final exam has a weight of 40%, the midterm 30%, and the project 30%
which adds up to 100%.
● So, the “evaluation” could be an object that has a name (final exam), a weight
(40%), and the score of a student.
Object Oriented Programming
● The student can have information such as the student’s name, university ID,
department, etc.
● Student can also have evaluation objects, which we have just described.
● Now, the program is easier to build.
● I can focus on evaluation object - it is a completely stand-alone component.
● I can focus on student object - it is also a stand-alone component.
● I can write a program that uses both of these types (classes).
● In the future, I can use these classes in another program - maybe a program
that takes attendance?
Object Oriented Programming
● Object oriented programming gives us the ability to encapsulate information in
a single class.
○ I have to use the term “encapsulation”.
○ Encapsulation means: the action of enclosing something in or as if in a capsule.
● Think of class as the blueprint of an object.
● This kind of blueprint ->
Object Oriented Programming
● So we design “objects” that are made up of other objects (and primitives!)
● These designs are declared as “classes.”
● This is great because we can use basic building blocks to create more
complicated classes.
● This is also great because we can reuse the code - no need to redefine
everything.
Object Oriented Programming
● OOP is not just the object; as in information in a capsule.
● It also provides mechanisms to access and modify that information.
● Typically we “hide” data (member variables) and provide “access” to them
using accessor methods (class methods).
class
private data
Object Oriented Programming: Inheritance
● We have talked about how we can create objects from other objects.
● However, OOP also provides other mechanisms.
● The most important one is “inheritance” - as in parent and child.
● It is much better if we can think of it as in generalization and specialization.
Animal Cat
Here is your car class, in Car.java file. Now, I can create Car objects.
Car sahin = new Car(“Şahin”, 1991);