Está en la página 1de 29

Java Programming Language

Wjdan Ramadan
Software Engineer
Fayez Ghazzawi & Graduate of Faculty of Information
Software Engineer Technology Engineering

Graduate of Faculty of Information


Technology Engineering
The Course’s Headings
➢Introduction and Classes
➢Methods
➢Inheritance and Interfaces
➢Composition, Aggregation and Polymorphism
➢Inner Classes
➢Exceptions
➢Threads
➢Arrays and Collections
➢Java Streams, Files and Applets
Fayez Ghazzawi
Wjdan Ramadan
Methods
• Methods are always defined inside a class (because Java is pure
OOP as we said before).
• To create a method we must specify:
1. Access Modifier.
2. Return Type.
3. Method’s Name.
4. Method’s Parameters.
• Without specifying the access modifier, the method will take the
default access modifier package-private.
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Addition
{
public int sum (int a, int b) { return a+b; }

public static void main(String[] args)


{
System.out.println(sum(5,5));
{
}

10

Fayez Ghazzawi
Wjdan Ramadan
Methods Main Tips

• Java distinguishes each method by its Signature.


• Signature has:
• Method’s Name.
• Number of Parameters and the Data Type of each one.
• We must initialize each local variable in the method before using
them.

Fayez Ghazzawi
Wjdan Ramadan
Instance vs. Class Methods
• Class Methods are shared by all instances, but Instance
Methods are called from each object separately.
• Class Methods are declared as static, Instance Methods are
non-static.
• Class Methods are called by using the name of the class or the
name of object, but Instance Methods can only be called by the
name of object.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class myClass
{
public void Hi() { System.out.println(“Hi”); }

public static void main(String[] args)


{
myClass c = new myClass();
c.Hi();
myClass.Hi();
{
}

Hi
Compile Error !!!

Fayez Ghazzawi
Wjdan Ramadan
Example
public class myClass
{
public static void Hi() { System.out.println(“Hi”); }

public static void main(String[] args)


{
myClass c = new myClass();
c.Hi();
myClass.Hi();
{
}

Hi
Hi

Fayez Ghazzawi
Wjdan Ramadan
Method Overloading
• Method overloading is essential developed to allow the same
method name to be used with different argument types.
• We can overload a method by creating another one in the same
class with same name but different number or types of
parameters.
• Overloaded Methods can’t have the same Signature.
• Remember !!! Return Type of a method is not considered as a
part of the method’s signature so we can’t distinguish the
overloaded methods by its return type.

Fayez Ghazzawi
Wjdan Ramadan
Example

public int sum (int a, int b)


{ return a+b; }

public float sum (float a, float b)


{ return a+b; }

public double sum (double a, double b)


{ return a+b; }

Fayez Ghazzawi
Wjdan Ramadan
this Reference
• this is a reference which refers to the current instance of the
class.
• It is usually used with Setters, Getters and Constructors.
• It is use to reach instance variables or methods.
• But any class variables or methods (variables and methods
which are declared as static) can be reached by this
reference.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num;
private String name;

public Student (int num, String name)


{ this.num = num;
this.name = name; } }

Fayez Ghazzawi
Wjdan Ramadan
final Keyword
• final Variable is a constant that can’t be modified.
• If final is used within a primitive the value is constant but if it’s
used within an object the reference is constant.
• Therefore final variable must be initialized.
• final Method is a method that can’t be overridden by a child
of its class.
• final Class is a class that can’t be inherited from.

Fayez Ghazzawi
Wjdan Ramadan
final Variables
• final variables has two types:
1. Initialized final:
this final variable is initialized when declared.
2. Blank final:
this final variable is initialized in the class’s constructors.
• final variable must be initialized. if not, a Compile
Error is occurred.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class One
{
public final int i= 0;

public int increase()


{ return this.i++; }
}

Compile Error !!!

Fayez Ghazzawi
Wjdan Ramadan
final Methods

• We use final with methods to prevent any child class


from changing its mean.
• So final methods can’t be overridden.
• Any private method is final implicitly, because we
implicitly can’t override it.

Fayez Ghazzawi
Wjdan Ramadan
final Classes
• Final classes can’t be inherited from.
• Any variable in a final class can be final or not
according to the programmer’s decision.
• All methods in a final class are implicitly final because
the final class can’t be inherited from so all the methods
can’t be overridden.

Fayez Ghazzawi
Wjdan Ramadan
Setters and Getters

• These methods provide an indirect access to private


attributes.
• They’re used to modify and display the private
variables inside the class.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;

public int getNum() { return num; }


public String getName() { return name; }

public void setNum(int num) { this.num = num; }


public void setName(String name) { this.name = name; }
}

Fayez Ghazzawi
Wjdan Ramadan
Constructors
• Constructor are used for initializations.
• It is a method that is called automatically when an object is
created.
• The Constructor consists:
1. Having the same name of the class.
2. Having no return type.
3. Being declared as public (optional).
• The Compiler creates a no-argument constructor if there are no
constructors explicitly provided.

Fayez Ghazzawi
Wjdan Ramadan
Default Constructor
• It is a constructor without arguments.
• The compiler creates a default constructor if there is no
constructor specified explicitly.
• If there is a specified constructor, the compiler won’t create one
for you :P .

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;

public static void main (String[] args)


{
Student st = new Student(); // Correct :D
}
}

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;

public Student (int num, String name)


{ this.num = num; this.name = name; }

public static void main (String[] args)


{
Student st1 = new Student(); // !! Error
Student st2 = new Student(50, “Fayez”); // Correct :D
}
}

Fayez Ghazzawi
Wjdan Ramadan
Overloading Constructors
• Because constructors must have the same name as the class
itself, we can specify several constructors with different
arguments.
public class Student
{
private int num; private String name;

public Student()
{ this.num = 0; this.name = “ “; }

public Student(int num, String name)


{ this.num = num; this.name = name; }
}
Fayez Ghazzawi
Wjdan Ramadan
Overloading Constructors

• A constructor can call another constructor by the function


this(…) but:
1. It must be called in the first statement of the current constructor.
2. It arguments must match a certain constructor's parameters.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;

public Student()
{ this(0, “ “); }

public Student(int num, String name)


{ this.num = num; this.name = name; }
}

Fayez Ghazzawi
Wjdan Ramadan
Static Initializer Block
• Static Initializer Block is a block that:
1.Not named.
2.Has no return type.
3.Begins with static keyword.
• It is similar to the constructor, but it is used to initialize all class
(static) variables previously declared.
• It is executed first, only once and for all objects.

Fayez Ghazzawi
Wjdan Ramadan
Example
public class Car
{
private static int count;

static{
count = 50;
}

public static void main(String[] args)


{ System.out.println(++Car.count); }
}

51

Fayez Ghazzawi
Wjdan Ramadan
The End.
See you next class

https://www.facebook.com/fayez.ghazzawi.3.4

fayezghazzawi@hotmail.com

Fayez Ghazzawi
Wjdan Ramadan

También podría gustarte