38
CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes

CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

CS111: PROGRAMMING

LANGUAGE II

Lecture 1: Introduction to classes

Page 2: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Lecture Contents

Dr. Amal Khalifa, 2014

What is a class?

Encapsulation

Class basics:

Data

Methods

Objects

Defining and using a class

2

Page 3: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

In Java

Java is an object-oriented programming language

As the term implies, an object is a fundamental entity in a Java program

Everything must be in a class (Even Main method).

3

Dr. Amal Khalifa, 2014

Page 4: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

What is a class?

Class:

Defines a data type (blueprint)

Combines data and operations in

one place:

data / fields/ properties / state

variables

Operations/ behavior / methods that

modify state

Dr. Amal Khalifa, 2014

4

Page 5: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

What is a data type?

Any data type includes

Data (range of values)

Operations (that can be performed on data)

Example: int data type has: Data: +-32,767 Operations: +, - , * , / , %

Same with classes

Collection of data values together with set of basic operations to be allowed for the values

6-5 Dr. Amal Khalifa, 2014

Page 6: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Class = Blueprint

One blueprint to create several similar, but different,

houses:

Copyright © 2012 Pearson Education,

Inc.

Page 7: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

• The bundling of data and procedures(methods) into a single unit(called class).

• Class : various data elements and member functions are wrapped up together.

• main feature of object oriented programming

Encapsulation 7

Dr. Amal Khalifa, 2014

Page 8: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Objects First with Java - A

Practical Introduction using BlueJ,

© David J. Barnes, Michael Kölling

Objects & Classes

objects

represent ‘things’ from the real world, or from some

problem domain (example: “the red car down there in

the car park”)

classes

represent all objects of a kind (example: “car”)

Page 9: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Objects & Classes

variables of class type are objects

Object:

an instance / realization of a class

same as relationship between a dataType and its

variables

9

Dr. Amal Khalifa, 2014

Page 10: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

How to define a Class??

Dr. Amal Khalifa, 2014

10

Type definition

Object declaration

Member access

Page 11: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Objects First with Java - A

Practical Introduction using BlueJ,

© David J. Barnes, Michael Kölling

Basic class structure

public class ClassName

{

Fields

Constructors

Methods

}

The contents of a class

Page 12: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Defining Classes

Dr. Amal Khalifa, 2014

12

Identifying Class Attributes Classes have attributes (data) and operations (behaviors).

Class attributes are implemented in Java programs as fields, and class

operations are implemented as methods.

Page 13: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

the Unified Modeling Language (UML)

Dr. Amal Khalifa, 2014

13

can be defined as a modeling language to capture

the architectural, behavioral and structural aspects

of a system.

The UML has an important role in object oriented

analysis and design.

Objects are the key to this object oriented world

The basic requirement of is to identify the object

efficiently.

Page 14: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

UML Class diagrams

Dr. Amal Khalifa, 2014

14

Class diagrams describes the objects in a system

and their relationships, so it is widely used by the

developer community.

The UML representation of a class is a rectangle

containing three compartments stacked vertically.

The top compartment shows the class's name.

The middle compartment lists the class's attributes.

The bottom compartment lists the class's operations.

Page 15: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

What about a student??

Examples 15

Dr. Amal Khalifa, 2014

Page 16: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

The student class - Exercise

Dr. Amal Khalifa, 2014

16

Draw UML class diagram for class

named Student where data is:

name, UnivID, date of birth,

department, level, GPA

Methods are:

PrintData()

Page 17: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

A class in Java

Dr. Amal Khalifa, 2014

17

.java file-

name extension

The class

keyword

Public

keyword

access modifier

class is

“available to

the public

No main

can’t execute

// Student.java

public class Student {

// data..

//methods...

//not static!!

}

Page 18: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Student Test

Dr. Amal Khalifa, 2014

18

Another class.

Why not

Import

student.java??

Using new

object

instantiation

Dot separator

// StudentTest.java

public class Students {

public static void main (String[] args)

{

//define object(s)

//call a method

}

Page 19: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Access modifiers

Controlling Access to Members

Computer Science Department

19

Access modifiers

Public Methods

Client’s view of the services provided by the class

Private data

not accessible outside the class

protected.

Page 20: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Public and Private Members

Data in class almost always designated private in

definition!

Upholds principles of OOP (Data Hiding)

Hide data from user

Allow manipulation only via operations

Which are member methods

6-20 Dr. Amal Khalifa, 2014

Page 21: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Marks for UML-supported visibility types 21

Dr. Amal Khalifa, 2014

Mark Visibility type

+ Public

# Protected

- Private

~ Package

Page 22: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Example

Dr. Amal Khalifa, 2014

22

. Modify class

efinition

// Student.java

public class Student {

// data private

//methods public

}

Page 23: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Student Test

Dr. Amal Khalifa, 2014

23

Try to access

private

members!!

What about

the public

ones ?

// StudentTest.java

public class StudentTest {

public static void main (String[] args)

{

//define object(s)

//access different members

}

Page 24: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Set/get methods

Computer Science Department

24

• Object needs to “have access" to its data

• accessor methods

• Allow object to read data

• Also called "get methods"

• Simple retrieval of member data

• Mutator methods

• Allow object to change data

• Also called “set methods"

• Manipulated based on application

Page 25: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Example: set/get methods

Dr. Amal Khalifa, 2014

25

. Class

Methods have

direct access

to class

members

(data or

methods)

// Student.java

public class Student {

// data

//methods

//set methods

//get methods

}

Page 26: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Student Test

Dr. Amal Khalifa, 2014

26

The set

method can

be used to

modify a data

field

the get

method can

be used to

display the

updated

value

// StudentTest.java

public class StudentTest {

public static void main (String[] args)

{

//define object(s)

//modify data!!

}

Page 27: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Constructors

Dr. Amal Khalifa, 2014

27

A constructor can be used to initialize an object of a

class when the object is created.

Page 28: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Example: constructors

Dr. Amal Khalifa, 2014

28

. Constructor

must be public

Same name

as class

no return type

Parameters??

// Student.java

public class Student {

// data

//an empty constructor

public Student()

{

}

//set/get methods

//other methods

}

Page 29: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Student Test

Dr. Amal Khalifa, 2014

29

Keyword new

calls the

class’s

constructor to

perform the

initialization.

// StudentTest.java

public class StudentTest {

public static void main (String[] args)

{

//define object(s)using empty

//constructor

//print data

}

Page 30: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

The default Constructor

By default, the compiler provides a default

constructor with no parameters in any class that

does not explicitly include a constructor.

If you declare any constructors for a class, the Java

compiler will not create a default constructor for that class

With the default constructor, its instance variables

are initialized to their default values.

Dr. Amal Khalifa, 2014

30

Page 31: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Overloaded Constructors

Dr. Amal Khalifa, 2014

31

Overloaded constructors

enable objects of a class to be initialized in different

ways.

multiple constructor declarations with different

signatures.

the compiler differentiates signatures by :

the number of parameters,

the types of the parameters and

the order of the parameter types in each signature.

Page 32: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Example: constructors

Dr. Amal Khalifa, 2014

32

. Define more

Constructors

Parameters??

// Student.java

public class Student {

// data

//constructors

public Student()

{

}

//set/get methods

//other methods

}

Page 33: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Student Test

Dr. Amal Khalifa, 2014

33

More objects

Different

initialization

Same

methods,

different data

// StudentTest.java

public class StudentTest {

public static void main (String[] args)

{

//define object(s)using different

//constructors

//print data

}

Page 34: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Using this Reference

Dr. Amal Khalifa, 2014

34

Every object can access a reference to itself with keyword this.

Implicitly:

refer to the object’s instance variables and other methods inside member methods.

Enables the class’s code to know which object should be manipulated.

Explicitly:

Can also use keyword this in a non-static method’s body.

Page 35: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Example: constructors

Dr. Amal Khalifa, 2014

35

. Use this

reference to

access class

members

// Student.java

public class Student {

// data

//constructors

public Student()

{

}

//set/get methods

//other methods

}

Page 36: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Important hint 36

Dr. Amal Khalifa, 2013

Page 37: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Be careful!! 37

Dr. Amal Khalifa, 2013

Page 38: CS111: Programming Language II · Every object can access a reference to itself with keyword this. Implicitly: refer to the object’s instance variables and other methods inside

Text Book:

Chapter 3

That’s all for today…..

Dr. Amal Khalifa, 2014

38