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

Lecture 02 - Thinking in Obejcts

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 02 - Thinking in Obejcts

Uploaded by

anonimolankaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 02:

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

public accessor methods

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

More general More specific


Object Oriented Programming: Inheritance
● As humans, we love to categorize.
● You can see it in everything; we have categorized all living things.
● OOP makes use of this.
● The common elements are in general classes: consider the “animal” class.
● More specific elements are in specialized classes: consider the “cat” class.
● Later on, if we want to create a “dog” class, we can also base it on “animal”
and provide this class with basic animal properties and actions.
Object Oriented Programming: Inheritance
● To be honest, as humans we can get caught up in the process and create
deep layers of inheritance.
● Try to keep it shallow - an inheritance tree that has more than three layers is
hard to maintain.
Object Oriented Programming: Inheritance
● Inheritance provides a mechanism to pass properties and actions to “sub-
classes.”
● As we design the program (or software) we come across the decision to either
to use inheritance or composition.
● We can create a subclass so that this subclass gains the properties and
actions of its parent class OR we can use an object of the parent class as a
member variable so get access to its properties and actions.
● This decision has to be made carefully.
○ Does the new class really have to be a subclass of the parent class?
Object Oriented Programming: Polymorphism
● Another concept in OOP is related to inheritance.
● If we have classes that are related, such as parent and child classes, how can
we write programs that take advantage of them?
● Let’s assume that you have a method called void greet(Animal a) that has a
parameter of class Animal.
● The Cat class is a subclass of Animal, or Cat “is-a” Animal.
● Since we accept an animal object, and since a cat object is also an animal
object, we can call this greet method with a cat object.

greet(myCat); // where myCat is a cat object.


Object Oriented Programming: Polymorphism
● When we are implementing the greet method, we expect an Animal object,
therefore we can only use the properties and actions of this class.
● Let’s assume that Animal class has a “makeASound()” method.
● Since all subclasses inherit the properties and actions of Animal class, Cat
(and any other subclass) will have “makeASound()” method.
● This method could be overridden, or the subclass can rely on the
implementation in the Animal class.
● For our case, let’s say that in Cat class we override it.
● Polymorphism shines at this moment.
Object Oriented Programming: Polymorphism
void greet(Animal a) { ● This is where polymorphism shines.
a.makeASound(); ● Even though we “pass” a Cat object to
“greet” method, OOP finds out that this is
}
a specialized class and calls the
makeASound() method that is overridden
greet(myCat); by the Cat class.
// where myCat is a Cat object ● Polymorphism means “many” “forms”.
● This example shows why.
● If we have passed an object that belongs
to another subclass of the Animal class
(even a sub-sub-sub...class) it would be
able to find out the “type” of the object and
use the correct method.
Object Oriented Programming: Polymorphism
● Without polymorphism, we would have needed a way to find out the type of
the class, then cast the object to that type.
if (type == “Cat”) {
Cat c = (Cat)a;
} else if (type == “Dog”) {
Dog d = (Dog)a;
} …

● This would have caused a major problem:


○ When we have created a new subclass, we would have to update the method with a new if
block…
Java as an OOP
● We will be using Java as our OOP language.
● Except the primitives, everything in Java is an object.
● Primitives have object counterparts (i.e., the wrapper classes).
○ byte –> Byte
○ short –> Short
○ int –> Integer
○ long –> Long
○ float –> Float
○ double –> Double
○ boolean –> Boolean
○ char –> Character
Revisiting the Classes
● A class is a “blueprint” of an object.
● There are cats - which is a large group of animals.
● The properties of cats, such as their physical traits and behaviors belong to all
cats. This is what the class is about.
● There are also cats you know about - for example, your cat, or your friend’s
cat. They are specific cats - not just any cat. This is what the object is about.
● Another example would be a “car” - which has a blueprint.
● You have the specifications to build a car (class), and you use it to “build” or
“construct” a car (object).
● Let’s go with that!
The Car Class
public class Car {
private int year;
private String model;
public Car(String m, int y) { // the constructor
model = m;
year = y;
}
}

Here is your car class, in Car.java file. Now, I can create Car objects.
Car sahin = new Car(“Şahin”, 1991);

The object sahin is an instance of the Car class.


Revisiting the Classes
● Remember that when we use the new keyword, an object is created in
memory and its reference is returned as a value.
● So, when a reference variable refers to an object, the variable stores the
memory address of the object, not the object itself.
● It makes sense, because a class can have references to other classes, which
could have references to the original class, which in turn would create a
recursive loop.
○ Let’s say a Student object has a reference to a Course object, and the Course object has a
reference to a Lecturer object, and the Lecturer object has a reference to the same Student
object…
Point.java PointDemo.java
public class Point { public class PointDemo {
public int x; public static void main(String[] args) {
public int y; Point a = new Point (0, 0);
public Point(int a, int b) { Point b = new Point (3, 4);
x = a; System.out.println(a.distanceTo(b));
y = b; }
} }
public double distanceTo(Point b) {
double dx = Math.pow(x - b.x, 2); - How many variables are there in main?
double dy = Math.pow(y - b.y, 2); - Is variable a referring to an object?
return Math.sqrt(dx + dy); - Is variable b a primitive?
} - How many objects are instantiated?
} - Is there a call to a constructor?
- What is passed to distanceTo method?
- What are the data members in Point class scope? - Who called the distanceTo method?
- What are the methods? - Why is the keyword new used?
- Is there any non-parameterized constructor?
Details on Classes
● Let’s switch to Chapters 3 and 8 of the textbook to dissect the code samples
shown.
● Let’s focus on the following:
○ UML class diagrams,
○ class declarations, instance variables, methods, object instantiation
○ constructors,
○ overloaded methods,
○ "this" reference,
○ "has-a" relationship,
○ "static" class members,
○ "final" instance variables.

You might also like