programming 1 - yola classes and...modifiers • variables, functions, and classes declared by...
TRANSCRIPT
Programming 1
Classes and Objects
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
Attributes
The name
• Age
• Dynasty
Behavior patterns
• Caterwaul
• Play
• eater
• Industrials
Attributes and behavior patterns
• Called class
• Used to create instances of the object
Cat class
• Attributes
• Behavior patterns
objects
Class
• The class is a large container that can contain all the code of variables, functions, objects,.... etc.
• How to define
Class class_name
{
}
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)
Class structure
• The class can consist of a name and properties only or a name and operations only.
Class structure
Reserved
word
Subject to label
conditions
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
Class structure
Define class
properties
Behaviors
Class example
Behaviors
Class exampleClass name
Attributes
Behaviors
Modifiers
• Modifiers restrict access to properties and behaviors of class.
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.
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.
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
Modifiers
The following methods
adding in person class
To change age
in correct way
Reach to private
variables
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.
Class variables types
• Member variables (Instance variables )
• Class variables (Static variables )
• Automatic variables (Local variables )
• Constant (Final variables)
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
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
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
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
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.
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
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
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();
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
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.
To access the properties and methods of the object
• Then Create object from SavingsAccount class
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.
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.
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
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
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
Relation of Object to Class
Run result:
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
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
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.
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
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
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
Constructor
Default Constructor to
create object without
initial values
Constructor to create
object with initial values
Call Constructors
output
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.
Use this word
Get the same previous
result
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
Pass objects to functions• The object is passed as any variable as in the following example
output
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