programming 1 - yola classes and...modifiers • variables, functions, and classes declared by...

49
Programming 1 Classes and Objects

Upload: others

Post on 27-Apr-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Programming 1

Classes and Objects

Page 2: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Objective

• Define and Create class

• Encapsulate

• Access modifiers

• Types of variables in the class

• Define and create object

• Access object data and functions

• Constructor

• Overload constructors

• Use this word

• Pass an object to a function

Page 3: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Attributes

The name

• Age

• Dynasty

Behavior patterns

• Caterwaul

• Play

• eater

• Industrials

Page 4: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Attributes and behavior patterns

• Called class

• Used to create instances of the object

Cat class

• Attributes

• Behavior patterns

objects

Page 5: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class

• The class is a large container that can contain all the code of variables, functions, objects,.... etc.

• How to define

Class class_name

{

}

Page 6: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class

• Class parts:

1. Class Name

2. Attributes (Variables are defined inside and outside of a functions called Attributes).

3. Behavior ( define functions and operations)

Page 7: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class structure

• The class can consist of a name and properties only or a name and operations only.

Page 8: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class structure

Reserved

word

Subject to label

conditions

Page 9: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class structure

• If the class contains (main method) it should be public

• The class name must be unique, that is, non-recurring in the same project

Page 10: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class structure

Define class

properties

Behaviors

Page 11: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class example

Behaviors

Page 12: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class exampleClass name

Attributes

Behaviors

Page 13: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Modifiers

• Modifiers restrict access to properties and behaviors of class.

Page 14: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Modifiers

• variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are in the same package or in another package.

• But when use private that means more restrictive , so it can not access from other class even if they are in the same package.

• Java provides a default modifier that is used when there is no one declared access to any variable, function, or class is only accessible Through the class in the same package.

Page 15: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Modifiers

• Public:

✓ Visible to all classes

✓ Least secure

✓ Functions are usually general

• Private:

• More secure

✓ Fields are usually private

✓ Visible in current classes.

• Default:

✓ Visible in current package

✓ There is no word for this access level.

Page 16: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Modifiers

• If private variables or functions exist within any class, they cannot be used or called from the main method.

• So the solution using

get and set methods.

• get to access any variable

• Set to change the value of the variable

in a safe mannerError not accessible

Error not accessible

Page 17: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Modifiers

The following methods

adding in person class

To change age

in correct way

Reach to private

variables

Page 18: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Encapsulation

• A method that can be used to hide basic data in a class, ie to hide properties In it.

• Other classs can only deal with these hidden proparties, through functions created by the basic programmer.

• Encapsulation provides techniques to restrict what each class sees from other

• So to achieve the encapsulation principle, you must define the properties as private and define methods that are used to reach them as public.

• Using the set and get methods to reach hidden properties.

Page 19: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class variables types

• Member variables (Instance variables )

• Class variables (Static variables )

• Automatic variables (Local variables )

• Constant (Final variables)

Page 20: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class variables types (1)

• Member variables (Instance variables )

• Are variables defined in the class outside the brackets of any function immediately after the class statement as following.

• These variables are called instance variables because they arise with every Instance that arises from this class.

• To use these variables we must mention the name of the instance and then a point followed by the name of the variable as in the following example

Page 21: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class variables types (2)

• Class variables (Static variables )

• They are variables very similar to the first type and are also known in the same place, but differ with them in that they are preceded by the word static.

• It also varies in that its value is constant for all Instance of that class and is shown in the example:

• Also, the word static can be added to functions to be a function specific to the class and not to the object

Page 22: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class variables types (3)

• Automatic variables (Local variables )

• These variables differ from the above because they originate within the brackets of the Methods and not the class and must be initialized with a value before use

• It should be noted that the use within brackets does not make it possible or visible to what is outside those brackets, so it is preferable to define them immediately after the definition of the function as follows

Page 23: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Class variables types (4)

• Constant (Final variables)

• Variables whose values never change and are defined by the word final as follows

//Can not change value of pi inside

method

Page 24: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Example

A, b, c, d are instance variables because define it inside class and out the methods.

e is class variable because it is static type

X, y and z are local variables because define it inside methods.

Page 25: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Define and create object

• Object is an instance of a specific class

• Cannot create object if there is no class.

• To define an object from a class you must put the class name and then name of the object

Page 26: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Define and create object

• Objects such as primitive values are represented by variables

• need new word to create object, but string object not need new word

Create variables age

Create objects

type name value

Page 27: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Define and create object

• To create object from Person class write :

Person A1 = new Person();

• Object name is A1

• A1 object have special copy from person class properties.

• Note: The new person () code actually generates an object from the class, and gives initial values for the properties in it.

• An object can be obtained from the Person class by typing the same code In two levels

Person A1;

A 1 = new Person(); //== Person A 1 = new Person();

Page 28: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

How to deal with objects

• We create an object from the class using the word new.

• Then we enter values for its properties, call its methods etc ... as follows: -

object-name. property-name

object-name. method-name

Page 29: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

How to deal with objects

• Example:

Person A 1 = new Person();

A 1 .job= “Student”;

A1 . printInfo( ) To print the object information

using the class method.

Page 30: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

To access the properties and methods of the object

• Then Create object from SavingsAccount class

Page 31: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

• Objects help the programmer.

• For example, if you plan to create a simple program To save people information, will you create a class for everyone ?!

• Of course not, but create only one class representing a person, and put things in it the basic you want to be present for each person. Then you create it objects as much as you want, and then every object of this class becomes a phrase For someone who has his own information.

Page 32: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

• As you can see, we have created a class that contains the basic information that we want to fill for each person.

• Then we created 4 objects (4 people), and then entered special information for each object in them.

• Now if you add any new variable or function in the Person class, any object of this class will have a copy of the new object you added.

• If you modify a specific code in the Person class, this code will also be modified for all objects of this class.

Page 33: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

Person Class

Name:

Sex:

Job:

Age:

Objects from Person

class

Name: Rabih

Sex : male

Job : Engineer

Age :27

Name: Ahmad

Sex : male

Job : doctor

Age :34

Name: Ros

Sex : female

Job : secretary

Age :22

Name: Mohamad

Sex : male

Job : student

Age :21

Page 34: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

• Note: The Person class and Main class must be created in the same package for the code to work properly

// Define method to print .

// Define 4 properties

Page 35: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

// create objects from person class

// Define p1 properties

// Define p2 properties

// Define p3 properties

// Define p4 properties

// display properties for each class

Page 36: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class

Run result:

Page 37: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Relation of Object to Class• If you find that you repeat similar lines of code, you may become

Programming is boring, so find a way to do the same work on fewer lines.

• As an alternative, try writing this code as part of a function.

1st time

2 nd time

???? time

Page 38: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

• The build process is to initialize the object to assign initial values to its properties

• The construction function name must be the same as the class name

• We may work an unspecified number of construction cases, provided that the cases Constructions should not be equal in number of transactions and be transactions of the same Type and order overloading

Page 39: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

• One of the most important things to think about after creating a new class is make a Way of creating objects easy from this class.

• Hence the idea of a constructor, a function of its kind Specifically, it is called during object creation to generate initial values for properties In it.

• Since a class object can only be created through a constructor, it will A Java compiler generates a default virtual constructor for you if it finds that The class you defined does not contain any Constructor function, but no Explicitly written in the class.

Page 40: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

• If we define a class named Person and we do not define a Constructor Him as in the next class.

• The compiler will automatically create an empty constructor about us as follows

• They are called only once when any object is created

Page 41: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

• The basic rule when defining a constructor is that it must have the same class name and be public.

• If you define a constructor, the compiler will not generate a default one.

• You can define more than a constructor. And you can always create an empty constructor, even Use if you do not want to give specific initial values to properties when creating an object.

• The construction does not include any return type or even a void

Page 42: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

• Now we will return to the Class Person, and we will add 2 Constructor, one empty) which is like Default, and another where we can enter values directly into the properties in the object

Page 43: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Constructor

Default Constructor to

create object without

initial values

Constructor to create

object with initial values

Page 44: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Call Constructors

output

Page 45: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Use this word

• The word this is a reserved word in Java, and is used to refer to the current object.

• We will use it to distinguish between variables declared inside functions (Local Variables) and between Variables declared inside the class and outside function (Global Variables).

• It can be deal with this like any object using the dot (.)

• We will go back to the Person class and use this word several times to see its effect on code.

Page 46: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Use this word

Get the same previous

result

Page 47: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Use this word

When you match the names

of the parameters with Class

variables should use the word

this So an error does not

appear during program

execution

Page 48: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Pass objects to functions• The object is passed as any variable as in the following example

output

Page 49: Programming 1 - Yola Classes and...Modifiers • variables, functions, and classes declared by public, are accessible by any class in a Java program, whether these are classes are

Summary

• Define and Create class

• Encapsulate

• Access modifiers

• Types of variables in the class

• Define and create object

• Access object data and functions

• Constructor

• Overload constructors

• Use this word

• Pass an object to a function