KEMBAR78
Java- language Lecture 6 | PDF
JAVA
FUNDAMENTALS
Eng : Hatem Abd El-Salam
Lecture 6
Agenda
 Overloading & Overriding
 This
 Super
 Getters and setters
© 2016 by Eng. Hatem Abd El-Salam
Overloading
 If a class have multiple methods by same name but different parameters, it is known
as Method Overloading.
 Advantage of method overloading?
Method overloading increases the readability of the program.
 Different ways to overload the method
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
© 2016 by Eng. Hatem Abd El-Salam
Overloading (Cont.)
1) Example of Method Overloading by changing the no. of arguments
© 2016 by Eng. Hatem Abd El-Salam
Overloading (Cont.)
2) Example of Method Overloading by changing data type of argument
© 2016 by Eng. Hatem Abd El-Salam
Overloading (Cont.)
Why Method Overloading is not possible by changing the return type of method?
 In java, method overloading is not possible by changing the return type of the
method because there may occur ambiguity. Let's see how ambiguity may occur:
 because there was problem:
© 2016 by Eng. Hatem Abd El-Salam
Overloading (Cont.)
 Method Overloading and Type Promotion
 As displayed in the above diagram, byte can be promoted to( short, int, long, float or
double). The short data type can be promoted to (int,long,float or double). The char
data type can be promoted to (int, long, float or double )and so on.
© 2016 by Eng. Hatem Abd El-Salam
Overloading (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Overriding
 If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in java.
 In other words, If subclass provides the specific implementation of the method that
has been provided by one of its parent class, it is known as method overriding.
 Usage of Java Method Overriding
1. Method overriding is used to provide specific implementation of a method that is already provided
by its super class.
2. Method overriding is used for runtime polymorphism
 Rules for Java Method Overriding:
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
 Understanding the problem without method overriding
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
EX:
 Consider a scenario, Bank is a class that provides functionality to get rate of
interest. But, rate of interest varies according to banks. For example, SBI, ICICI and
AXIS banks could provide 8%, 7% and 9% rate of interest.
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
 Can we override static method?
No, static method cannot be overridden. It can be proved by runtime polymorphism,
so we will learn it later.
 Why we cannot override static method?
because static method is bound with class whereas instance method is bound with
object. Static belongs to class area and instance belongs to heap area.
 Can we override java main method?
No, because main is a static method.
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
 Difference between method Overloading and Method Overriding in java
© 2016 by Eng. Hatem Abd El-Salam
Overriding (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
This
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
1. This keyword can be used to refer current class instance variable.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
2. this() can be used to invoked current class constructor
The this() constructor call can be used to invoke the current class constructor
(constructor chaining). This approach is better if you have many constructors in the
class and want to reuse that constructor.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
 Where to use this() constructor call?
The this() constructor call should be used to reuse the constructor in the constructor.
It maintains the chain between the constructors
it is used for constructor chaining.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
3) The this keyword can be used to invoke current class method (implicitly).
You may invoke the method of the current class by using the this keyword. If you
don't use the this keyword, compiler automatically adds this keyword while invoking
the method.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
4) this keyword can be passed as an argument in the method.
The this keyword can also be passed as an argument in the method. It is mainly
used in the event handling.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
5) The this keyword can be passed as argument in the constructor call.
We can pass the this keyword in the constructor also. It is useful if we have to use
one object in multiple classes.
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
6) The this keyword can be used to return current class instance.
We can return the this keyword as an statement from the method. In such case,
return type of the method must be the class type (non-primitive).
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
This (Cont.)
 Proving this keyword
Let's prove that this keyword refers to the current class instance variable. In this
program, we are printing the reference variable and this, output of both variables
are same.
© 2016 by Eng. Hatem Abd El-Salam
Super
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
 super is used to refer immediate parent class instance variable.
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
 2) super is used to invoke parent class constructor.
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
 Note: super() is added in each class constructor automatically by compiler.
As we know well that default constructor is provided by compiler automatically but it
also adds super() for the first statement . If you are creating your own constructor
and you don't have either this() or super() as the first statement, compiler will
provide super() as the first statement of the constructor.
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
 3) super can be used to invoke parent class method
© 2016 by Eng. Hatem Abd El-Salam
Super (Cont.)
© 2016 by Eng. Hatem Abd El-Salam
Getters and setters
 When implementing a method of a class, use the class’s set and get methods to
access the class’s private data. This simplifies code maintenance and reduces the
likelihood of errors.
 When necessary, provide public methods to change and retrieve the values of
private instance variables. This architecture helps hide the implementation of a class
from its clients, which improves program modifiability.
© 2016 by Eng. Hatem Abd El-Salam
Getters and setters (Cont.)
 Set methods
 Also known as mutator methods
 Assign values to instance variables
 Should validate new values for instance variables
• Can return a value to indicate invalid data
 Get methods
 Also known as accessor methods or query methods
 Obtain the values of instance variables
 Can control the format of the data it returns
© 2016 by Eng. Hatem Abd El-Salam
© 2016 by Eng. Hatem Abd El-Salam
Encapsulation
 Encapsulation in Java is a mechanism of wrapping the data (variables) and code
acting on the data (methods) together as a single unit. In encapsulation, the
variables of a class will be hidden from other classes, and can be accessed only
through the methods of their current class. Therefore, it is also known as data
hiding.
To achieve encapsulation in Java :
 Declare the variables of a class as private.
 Provide public setter and getter methods to modify and view the variables values.
© 2016 by Eng. Hatem Abd El-Salam
Encapsulation(Cont.)
Benefits of Encapsulation
 The fields of a class can be made read-only or write-only.
 A class can have total control over what is stored in its fields.
 The users of a class do not know how the class stores its data. A class can change
the data type of a field and users of the class do not need to change any of their
code.
Example:
 I'll let you tell me what funny video we will watch on my computer. But I don't really
trust you enough to hand over my laptop and let you bring it up yourself.
© 2016 by Eng. Hatem Abd El-Salam
Without setter and getter
© 2016 by Eng. Hatem Abd El-Salam
With setter and getter
© 2016 by Eng. Hatem Abd El-Salam
© 2016 by Eng. Hatem Abd El-Salam
Name : Eng. Hatem Abd El-Salam
Email : Hatemabdelsalam@Hotmail.com
LinkedIn : linkedin.com/in/hatemabdelsalam
SlideShare : slideshare.net/HatemAbdElSalam
Contact Details
© 2016 by Eng. Hatem Abd El-Salam

Java- language Lecture 6

  • 1.
    JAVA FUNDAMENTALS Eng : HatemAbd El-Salam Lecture 6
  • 2.
    Agenda  Overloading &Overriding  This  Super  Getters and setters © 2016 by Eng. Hatem Abd El-Salam
  • 3.
    Overloading  If aclass have multiple methods by same name but different parameters, it is known as Method Overloading.  Advantage of method overloading? Method overloading increases the readability of the program.  Different ways to overload the method There are two ways to overload the method in java 1. By changing number of arguments 2. By changing the data type © 2016 by Eng. Hatem Abd El-Salam
  • 4.
    Overloading (Cont.) 1) Exampleof Method Overloading by changing the no. of arguments © 2016 by Eng. Hatem Abd El-Salam
  • 5.
    Overloading (Cont.) 2) Exampleof Method Overloading by changing data type of argument © 2016 by Eng. Hatem Abd El-Salam
  • 6.
    Overloading (Cont.) Why MethodOverloading is not possible by changing the return type of method?  In java, method overloading is not possible by changing the return type of the method because there may occur ambiguity. Let's see how ambiguity may occur:  because there was problem: © 2016 by Eng. Hatem Abd El-Salam
  • 7.
    Overloading (Cont.)  MethodOverloading and Type Promotion  As displayed in the above diagram, byte can be promoted to( short, int, long, float or double). The short data type can be promoted to (int,long,float or double). The char data type can be promoted to (int, long, float or double )and so on. © 2016 by Eng. Hatem Abd El-Salam
  • 8.
    Overloading (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 9.
    Overriding  If subclass(child class) has the same method as declared in the parent class, it is known as method overriding in java.  In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.  Usage of Java Method Overriding 1. Method overriding is used to provide specific implementation of a method that is already provided by its super class. 2. Method overriding is used for runtime polymorphism  Rules for Java Method Overriding: 1. method must have same name as in the parent class 2. method must have same parameter as in the parent class. 3. must be IS-A relationship (inheritance). © 2016 by Eng. Hatem Abd El-Salam
  • 10.
    Overriding (Cont.)  Understandingthe problem without method overriding © 2016 by Eng. Hatem Abd El-Salam
  • 11.
    Overriding (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 12.
    Overriding (Cont.) EX:  Considera scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate of interest. © 2016 by Eng. Hatem Abd El-Salam
  • 13.
    Overriding (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 14.
    Overriding (Cont.)  Canwe override static method? No, static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.  Why we cannot override static method? because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.  Can we override java main method? No, because main is a static method. © 2016 by Eng. Hatem Abd El-Salam
  • 15.
    Overriding (Cont.)  Differencebetween method Overloading and Method Overriding in java © 2016 by Eng. Hatem Abd El-Salam
  • 16.
    Overriding (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 17.
    This © 2016 byEng. Hatem Abd El-Salam
  • 18.
    This (Cont.) 1. Thiskeyword can be used to refer current class instance variable. © 2016 by Eng. Hatem Abd El-Salam
  • 19.
    This (Cont.) 2. this()can be used to invoked current class constructor The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor. © 2016 by Eng. Hatem Abd El-Salam
  • 20.
    This (Cont.)  Whereto use this() constructor call? The this() constructor call should be used to reuse the constructor in the constructor. It maintains the chain between the constructors it is used for constructor chaining. © 2016 by Eng. Hatem Abd El-Salam
  • 21.
    This (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 22.
    This (Cont.) 3) Thethis keyword can be used to invoke current class method (implicitly). You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. © 2016 by Eng. Hatem Abd El-Salam
  • 23.
    This (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 24.
    This (Cont.) 4) thiskeyword can be passed as an argument in the method. The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. © 2016 by Eng. Hatem Abd El-Salam
  • 25.
    This (Cont.) 5) Thethis keyword can be passed as argument in the constructor call. We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. © 2016 by Eng. Hatem Abd El-Salam
  • 26.
    This (Cont.) 6) Thethis keyword can be used to return current class instance. We can return the this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). © 2016 by Eng. Hatem Abd El-Salam
  • 27.
    This (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 28.
    This (Cont.)  Provingthis keyword Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same. © 2016 by Eng. Hatem Abd El-Salam
  • 29.
    Super © 2016 byEng. Hatem Abd El-Salam
  • 30.
    Super (Cont.)  superis used to refer immediate parent class instance variable. © 2016 by Eng. Hatem Abd El-Salam
  • 31.
    Super (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 32.
    Super (Cont.)  2)super is used to invoke parent class constructor. © 2016 by Eng. Hatem Abd El-Salam
  • 33.
    Super (Cont.)  Note:super() is added in each class constructor automatically by compiler. As we know well that default constructor is provided by compiler automatically but it also adds super() for the first statement . If you are creating your own constructor and you don't have either this() or super() as the first statement, compiler will provide super() as the first statement of the constructor. © 2016 by Eng. Hatem Abd El-Salam
  • 34.
    Super (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 35.
    Super (Cont.)  3)super can be used to invoke parent class method © 2016 by Eng. Hatem Abd El-Salam
  • 36.
    Super (Cont.) © 2016by Eng. Hatem Abd El-Salam
  • 37.
    Getters and setters When implementing a method of a class, use the class’s set and get methods to access the class’s private data. This simplifies code maintenance and reduces the likelihood of errors.  When necessary, provide public methods to change and retrieve the values of private instance variables. This architecture helps hide the implementation of a class from its clients, which improves program modifiability. © 2016 by Eng. Hatem Abd El-Salam
  • 38.
    Getters and setters(Cont.)  Set methods  Also known as mutator methods  Assign values to instance variables  Should validate new values for instance variables • Can return a value to indicate invalid data  Get methods  Also known as accessor methods or query methods  Obtain the values of instance variables  Can control the format of the data it returns © 2016 by Eng. Hatem Abd El-Salam
  • 39.
    © 2016 byEng. Hatem Abd El-Salam
  • 40.
    Encapsulation  Encapsulation inJava is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding. To achieve encapsulation in Java :  Declare the variables of a class as private.  Provide public setter and getter methods to modify and view the variables values. © 2016 by Eng. Hatem Abd El-Salam
  • 41.
    Encapsulation(Cont.) Benefits of Encapsulation The fields of a class can be made read-only or write-only.  A class can have total control over what is stored in its fields.  The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code. Example:  I'll let you tell me what funny video we will watch on my computer. But I don't really trust you enough to hand over my laptop and let you bring it up yourself. © 2016 by Eng. Hatem Abd El-Salam
  • 42.
    Without setter andgetter © 2016 by Eng. Hatem Abd El-Salam
  • 43.
    With setter andgetter © 2016 by Eng. Hatem Abd El-Salam
  • 44.
    © 2016 byEng. Hatem Abd El-Salam Name : Eng. Hatem Abd El-Salam Email : Hatemabdelsalam@Hotmail.com LinkedIn : linkedin.com/in/hatemabdelsalam SlideShare : slideshare.net/HatemAbdElSalam Contact Details
  • 45.
    © 2016 byEng. Hatem Abd El-Salam