Introduction to Java II
P.1
ghadeer-al-hasan ghadeerof@gamil.com
Object & Classes:
Object:
An entity that has state and behavior,
• state: represent data of an object.
• behavior: represent functionality of an object such as deposit.
• identity: object identity is typically implemented via unique ID.
The value of the ID is not visible to the external user, But, it is used
internally by the JVM to identify each object uniquely.
Class:
• Is a group of objects that has common properties, it is a template or
blueprint from which objects are created.
• In java can contain: data member, method, constructor, block,
Class and interface.
P.2
Variable and DataType :
Variable :
Variable is name of reserved area allocated in memory(RAM).
Type of Variable:
There are three type of variable in java:
• Local variable
that is declared inside the method
• Instance variable
that declared inside the class but outside the method, isn’t declared as static
• Static variable
that is declared as static , it cannot be local, are stored in static memory,
created when the program starts and destroyed when the program stops
P.3
Variable and DataType :
Data Types:
• Primitive data types
• Non- primitive data types
P.4
Variable and DataType :
Data Type Default Value Default size
boolean false 1 bit
char ‘u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
P.5
P.S :
In Unicode, character holds 2 byte, so java also uses 2 byte for character.
Lowest value: u0000
Highest value: uFFFF
P.6
Constructor:
Is a special type of method that is used to initialize the object.
It is invoked at the time of object creation, constructor the values
There are basically two rules defined for the constructor:
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type.
There are two types of constructor:
1. Default constructor (no- arg constructor)
2. Parameterized constructor
P.S:
If there is no constructor in a class, compiler automatically creates a default
Constructor, it is provides the default values the object
P.7
Example of default constructor:
P.8
Example of parameterized constructor :
It is used to provider different values to the distinct objects.
Student s3 = new Student();
it is false
Why???
P.9
Overloading Constructor:
The compiler differentiates
these constructors by taking into account
the number of parameters
in the list and their type
P.10
Difference between constructor and method:
Constructor Method
Is used to initialize the state of an object Is used to expose behavior of an object
Must not have return type Must have return type
Is invoked implicitly Is invoked explicitly
Compiler provides a default constructor if you
don’t have any constructor
Is not provided by compiler in any case
Name must be same as the class name Name may or may not be same as class name
P.11
Copier Constructor:
P.12
Access Modifier:
The access modifiers specifies accessibility of a data member, method, constructor or class.
There are 4 types of access modifiers :
1. private
is accessible only within class
2. default
when you don’t use any modifier, it is treated as default by default, is accessible only with package
3. protected
is accessible within package and outside the package but through inheritance only
it can’t be applied on the class
4. public
accessible everywhere , it has the widest scope among all other modifiers
P.13
Example of Private Access Modifier:
Student.java
Main.java
If you make any class constructor private,
you cannot create the instance of that
class from outside the class
A class cannot be private or
protected except nested class
P.14
Example of Default Access Modifier:
A.java
A class is not public,
so it cannot be accessed from
outside the package.
B.java
P.15
Example of Protected Access Modifier:
Student.java
P.16
Main.java
Example of Public Access Modifier :
Student.java
Main.java
P.17
Finally :
Access
Modifier
Within
class
Within
package
Outside
package by
subclass only
Outside
package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes
P.18
The End
ghadeer-al-hasan ghadeerof@gamil.com

2- Introduction to java II

  • 1.
    Introduction to JavaII P.1 ghadeer-al-hasan ghadeerof@gamil.com
  • 2.
    Object & Classes: Object: Anentity that has state and behavior, • state: represent data of an object. • behavior: represent functionality of an object such as deposit. • identity: object identity is typically implemented via unique ID. The value of the ID is not visible to the external user, But, it is used internally by the JVM to identify each object uniquely. Class: • Is a group of objects that has common properties, it is a template or blueprint from which objects are created. • In java can contain: data member, method, constructor, block, Class and interface. P.2
  • 3.
    Variable and DataType: Variable : Variable is name of reserved area allocated in memory(RAM). Type of Variable: There are three type of variable in java: • Local variable that is declared inside the method • Instance variable that declared inside the class but outside the method, isn’t declared as static • Static variable that is declared as static , it cannot be local, are stored in static memory, created when the program starts and destroyed when the program stops P.3
  • 4.
    Variable and DataType: Data Types: • Primitive data types • Non- primitive data types P.4
  • 5.
    Variable and DataType: Data Type Default Value Default size boolean false 1 bit char ‘u0000’ 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte P.5
  • 6.
    P.S : In Unicode,character holds 2 byte, so java also uses 2 byte for character. Lowest value: u0000 Highest value: uFFFF P.6
  • 7.
    Constructor: Is a specialtype of method that is used to initialize the object. It is invoked at the time of object creation, constructor the values There are basically two rules defined for the constructor: 1. Constructor name must be same as its class name 2. Constructor must have no explicit return type. There are two types of constructor: 1. Default constructor (no- arg constructor) 2. Parameterized constructor P.S: If there is no constructor in a class, compiler automatically creates a default Constructor, it is provides the default values the object P.7
  • 8.
    Example of defaultconstructor: P.8
  • 9.
    Example of parameterizedconstructor : It is used to provider different values to the distinct objects. Student s3 = new Student(); it is false Why??? P.9
  • 10.
    Overloading Constructor: The compilerdifferentiates these constructors by taking into account the number of parameters in the list and their type P.10
  • 11.
    Difference between constructorand method: Constructor Method Is used to initialize the state of an object Is used to expose behavior of an object Must not have return type Must have return type Is invoked implicitly Is invoked explicitly Compiler provides a default constructor if you don’t have any constructor Is not provided by compiler in any case Name must be same as the class name Name may or may not be same as class name P.11
  • 12.
  • 13.
    Access Modifier: The accessmodifiers specifies accessibility of a data member, method, constructor or class. There are 4 types of access modifiers : 1. private is accessible only within class 2. default when you don’t use any modifier, it is treated as default by default, is accessible only with package 3. protected is accessible within package and outside the package but through inheritance only it can’t be applied on the class 4. public accessible everywhere , it has the widest scope among all other modifiers P.13
  • 14.
    Example of PrivateAccess Modifier: Student.java Main.java If you make any class constructor private, you cannot create the instance of that class from outside the class A class cannot be private or protected except nested class P.14
  • 15.
    Example of DefaultAccess Modifier: A.java A class is not public, so it cannot be accessed from outside the package. B.java P.15
  • 16.
    Example of ProtectedAccess Modifier: Student.java P.16 Main.java
  • 17.
    Example of PublicAccess Modifier : Student.java Main.java P.17
  • 18.
    Finally : Access Modifier Within class Within package Outside package by subclassonly Outside package Private Yes No No No Default Yes Yes No No Protected Yes Yes Yes No Public Yes Yes Yes Yes P.18
  • 19.