0% found this document useful (0 votes)
22 views45 pages

Cse 1325 - W7 - 10042021

Uploaded by

yaqi.huang
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)
22 views45 pages

Cse 1325 - W7 - 10042021

Uploaded by

yaqi.huang
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/ 45

CSE 1325

Week of 10/04/2021

Instructor : Donna French


Default and Explicit Initialization
for Instance Variables
So what happened with
Local variables are not initialized by default. String? Why was it
set to null?
Primitive-type instance variables are initialized by default
instance variables of types byte, char, short, int, long, float and
double are initialized to 0
instance variables of type boolean are initialized to false

You can specify your own initial value for a primitive-type instance variable by
assigning the variable a value in its declaration, as in
private int numberOfStudents = 10;
Primitive Types vs Reference Types
Java’s types are divided into primitive types and reference types.

Primitive types
boolean, byte, char, short, int,
long, float , double

All nonprimitive types are reference types.


Primitive Types vs Reference Types
A primitive-type variable can hold exactly one value of its declared type
at a time.

Programs use variables of reference types (normally called references)


to store the addresses of objects in the computer’s memory.

These variables are said to refer to an object in the program.


Primitive Types vs Reference Types
To call an object’s methods, you need a reference to the object.

If an object’s method requires additional data to perform its task, then


you’d pass arguments in the method call.

Primitive-type variables do not refer to objects, so such variables


cannot be used to invoke methods.
Primitive Types vs Reference Types
A variable of type String is a reference to the instance of the
String object you instantiated when you created the variable.

Reference-type instance variables (such as those of type String), if


not explicitly initialized, are initialized by default to the value null

null
represents a “reference to nothing.”
Constructors
Each class you declare can optionally provide a constructor with
parameters that can be used to initialize an object of a class when the
object is created.

Java requires a constructor call for every object that’s created.

If a class does not define constructors, the compiler provides a default


constructor with no parameters, and the class’s instance variables are
initialized to their default values.
Constructors
Class Account does not contain a
public class Account
{ constructor.
private String name;

public void setName(String name)


The default constructor is being used.
{
this.name = name; How do we know?
}

public String getName() Constructors are special methods


{
return name;
that share the name of class.
}
} Let's add a constructor.
public class Account Class Account now contains a
{ constructor.
private String name;

public Account(String name) How do we know?


{
this.name = name;
} There's a method in the class with the
same name as the class
public void setName(String name)
{
this.name = name;
Account
}
What does the constructor do?
public String getName()
{
return name; When the object is instantiated, the
} private instance variable name is set to
}
the String parameter passed in the
constructor.
Now let's instantiate multiple objects in our program.
Constructors
public class AccountDemo
{
public static void main(String[] args)
{
Account sAccount = new Account("Superman");
Account bAccount = new Account("Batman");

System.out.printf("sAccount is named %s\n", sAccount.getName());


System.out.printf("bAccount is named %s\n", bAccount.getName());
}
}
sAccount is named Superman
bAccount is named Batman
public class Account
{
Constructors Cannot Return
private String name; Values
public Account(String name)
{
this.name = name; Constructors can specify
} parameters but not return
public void setName(String name) types.
{
this.name = name;
}
This constructor is passed a
public String getName()
{
String containing the
}
return name; name, but it does not return
} anything.
There’s no default constructor in a class that declares a constructor

If you declare a constructor for a class, the compiler will not create a default constructor for
that class.
Constructors
Why use constructors?

If you do not provide a constructor, then your object will use the
default initializations for the instance variables.

0 for primitive and null for reference types

Use a constructor to start your object out with meaningful values.


Adding the Constructor to the class diagram
The constructor goes in the 3rd compartment of the class diagram

To distinguish a constructor from a class’s operations, place the word


“constructor” between guillemets (« and ») before the constructor’s
name.
public class Circle
{
private double radius = 0;

public double getRadius()


{
}
public void setRadius(double new_radius)
{
}
public double calculateCircumference()
{
}
};
public class Student
{
private String studentID;
private String netID;
private String emailAddress;

public String getStudentID()


{
}
public void setStudentID(String newStudentID)
{
}
public String getNetID()
{
}
}
public class PvsP Which lines will
{ compile?
public int x = 1;
public char y = 'A'; PvsP QuizMe = new PvsP();
public double z = 2.3;

private String a = "xyz"; QuizMe.x = 1;


private float b = 1.2F;
private long c = 123; QuizMe.a = "Quiz";
} QuizMe.y = 'A';
QuizMe.b = 1.23F;
QuizMe.z = 1.23;
QuizMe.c = 123;
Encapsulation
You will begin to notice that objects tend to have mostly private data
members and public member functions.

Why?

Take the example of the electronic devices that surround us every day.

They have simple interfaces that allows you to perform actions without
know the details behind those actions.
Encapsulation
The separation of interface and implementation is extremely useful because
it allows us to use objects without understanding how they work.

This vastly reduces the complexity of using these objects and increases the
number of objects we are capable of interacting with.

This same principle is applied to programming – separating implementation


from interface.

Generally, data members are made private (hiding implementation details)


and member functions are made public (giving the user an interface).
Encapsulation

Classes wrap attributes and member functions into objects created


from those classes – an object's attributes and methods are intimately
related.

Objects may communicate with one another, but they are not normally
allowed to know how other objects are implemented.

Encapsulation is the technique of information hiding - implementation


details are hidden within the objects themselves.
Encapsulation
Benefits of Encapsulation

Encapsulated classes are easier to use and reduce the complexity of


programs.

Encapsulated classes help protect your data and prevent misuse

Encapsulated classes are easier to debug


Encapsulation
Access Functions

Getter
A method that returns the value of a private variable

Setter
A method that changes the value of a private variable
Abstraction

Abstraction is the concept of describing something in simpler terms, i.e


abstracting away the details, in order to focus on what is important.

Abstraction is used to reduce complexity and allow efficient design and


implementation of complex software systems.

Abstraction is the act of representing essential features without


including the background details or explanations.
abstraction

Vehicle is an abstraction of truck and car. Animal is an abstraction of dog.


Vending Machine

Snack Machine
Coke Machine
What is this?

A tree?

How do you know?

We are aware of the


abstract concept of a tree
and are able to recognize
this picture as an
instantiation of our
abstract version of "tree".
So do we make assumptions
about this tree even though
we've never met this tree?

Yes

This tree is made up of


leaves, branches, a trunk
and roots.

It needs water and sunlight


to live.
Object Relationships
We are going to explore the relationship between objects
based on what we know about them.

There are many different kinds of relationships two objects


may have in real-life and we use specific “relation type” words
to describe these relationships.
Object Relationships

A square "is a" shape


A square "is a" shape

A car "has a" steering wheel

A computer programmer "uses a" keyboard

A flower "depends on" a bee for pollination.


A car "has a" steering wheel
A student is a "member of" a class.

Your brain exists as "part of" you


UML Relationships
There are many methods of showing relationships between classes.
We are going to focus on four specific relationships.

Association

Composition

Aggregation

Inheritance
UML Relationships
Association

• Represents the "has a" relationship


• a linkage between two classes
• shows that classes are aware of each other and their relationship
• uni-directional or bi-directional
Object Relationships - Association
A Student can be associated with an Instructor
Object Relationships - Association
A single Student can be associated with multiple Instructors
Object Relationships - Association
Multiple Students can be associated with multiple Instructors
UML Relationships
Aggregation Composition

• An association that represents a


• Special type of association very strong aggregation
• Represents the "has a" /"whole- • Represents the "has a" /"whole-
part" relationship. part" relationship.
• Describes when a class (the • Describes when a class (the whole)
whole) is composed of/has is composed of/has other classes
(the parts) BUT the parts cannot
other classes (the parts) exist without the whole.
• Diamond on "whole" side • Diamond on "whole" side
UML Relationships
Inheritance

• Represents the "is a" relationship

• Shows the relationship between a super class/base class and a


derived/subclass.

• Arrow is on the side of the base class


"is a" or "has a"?

Tortoise is an Animal
Otter is an Animal
Slow Loris is an Animal

"is a" relationship

Inheritance
"is a" or "has a"?

Otter is a Sea Urchin?


Otter has a Sea Urchin?

"has a" relationship

association/composition/aggregation?

"whole/part" relationship?

Otter is part of Sea Urchin?


Sea Urchin is part of Otter?

not "whole/part"

Association
"is a" or "has a"? Creep is part of Tortoise?
Tortoise is part of Creep?
Creep is a Tortoise?
Creep has a Tortoise? Yes – Creep is "whole" and Tortoise is "part"

"has a" relationship Can the Creep exist without the Tortoise?

Yes
association/composition/aggregation?

"whole/part" relationship? Aggregration


"is a" or "has a"?

Lobby is a Visitor Center? Bathroom is a Visitor Center?


Visitor Center is a lobby or bathroom?

Lobby/Bathroom has a Visitor Center?


Visitor Center has a Lobby and a Bathroom?

"has a" relationship

association/composition/aggregation?

"whole/part" relationship?
Can the "parts" – Bathroom and Lobby –
Visitor Center is part of Bathroom/Lobby?
exist without the "whole" - Visitor Center?
Bathroom/Lobby is part of Visitor Center?

Yes – Visitor Center is "whole" and Bathroom/Lobby is "part" Composition


Separating Interface from Implementation
The client code actually should only know 3 things about a class

• what instance methods to call


• what arguments to provide to each method
• what return type to expect from each method

The client code does not need to know how those methods are
implemented.
Separating Interface from Implementation
When the client code does know how a class is implemented, then the
programmer might write client code based on the class's
implementation details.

Ideally, if the class's implementation changes, the class's clients should


not have to change.

Hiding the class's implementation details makes it easier to change the


class's implementation while minimizing, and hopefully, eliminating
changes to the client code.
Interface of a Class
We are all familiar with a radio, and, in general,
the controls that allow us to perform a limited
set of operations on the radio – changing the
station, adjusting the volume and choosing
between AM and FM.

Some radios use dials, some have push buttons


and some support voice commands.

These controls serve as the interface between


the radio's users and the internal components.

The interface specifies what operations a radio


permits users to perform but does not specify
how the operations are implemented inside
the radio.
Interface of a Class
The interface of a class describes what services a class's clients can use
and how to request those services, but not how the class carries out
the services.

A class's public interface consists of the class's public member


methods which can also be known as the class's public services.

We can specify a class's interface by writing a class diagram that lists


only the class's methods prototypes and the class's instance variables.

You might also like