P.1
Inheritance
ghadeer-al-hasan ghadeerof@gamil.com
Inheritance in Java P.2
 Is a mechanism in which one acquires all the properties and behavior
of parent object
 You can create new classes that are built upon exist classes
 You can reuse methods and fields of patent class, and add new.
 Inheritance represents the IS-A relationship.
The extends keyword indicates that you are making
a new class that derives from an existing class
Inheritance Cont. : P.3
C IS-A D and B IS-A D
class D
class Cclass B
There can be three types of inheritance in Java:
Single, multilevel and hierarchical
Note: Multiple inheritance is not supported in java through class.
Example : P.4
P.5
Aggregation
Aggregation In Java : P.6
 Is class have an entity reference
 Aggregation represents HAS-A relationship
Example : P.7
P.8
Encapsulation
Encapsulation in Java P.9
 Is a process of wrapping code and data together into a single unit.
 We can create a fully encapsulated class in Java by making all the data
member of the class private.
 Now we can use setter and getter methods to set and get the data in it.
 It provides you the control over the data.
Class Student : P.10
Overloading In Java : P.11
 Is class has multiple methods by same name but different parameters.
 Increases the readability of the program.
There are two ways to overload the method:
1. by changing by of arguments
2. by changing the data type
We can not overloading by changing the return type of the method
Overloading Cont. : P.12
Overloading Cont. : P.13
Overloading Cont. : P.14
One type is promoted to another implicitly if no matching datatype is found
cdf
Java static keyword: P.15
 Is used for memory management mainly.
 Variable , methods, blocks and nested class.
Static variable :
If you declare any variable as static, it is know static variable.
• Can be used to refer the common property of all object.
• Gets memory only once in class area at the time of class loading.
s1
s2
num=1
fullname=“Maala:
num=2
fullname=“Elian”
college=“IT”
Stack memory
Heap memory
Example 1: P.16
Example 2: P.17
Static method: P.18
Apply static keyword with any method.
 belongs to the class rather than object of a class.
 Can be invoked without the need for creating an instance of a class.
 Can access static data member and can change the value of it.
Example 1: P.19
P.S:
1. The static method can not use non static data member
or call non static method directly.
2. This and super cannot be used in static context.
Static block P.20
 Is used to initialize the static data member.
 It is executed before main at the time of class loading.
P.S:
You can execute a program without main() method, with static block but
In previous version of JDK not in JDK 1.7
Java this keyword: P.21
this is a reference variable that refer to the current object.
a.) can be used to refer current class instance variable.
this keyword Cont. : P.22
b.) this() can be used to invoked current class constructor:
P.23
c.) you can used this() constructor call:
this keyword Cont. :
Call to this() must be the first
statement in constructor
P.24
d.) this keyword can be used to invoke current class method (implicitly)
this keyword Cont. :
P.25this keyword Cont. :
e.) this keyword can be passed as an argument in the method:
The End
ghadeer-al-hasan ghadeerof@gamil.com

4- Inheritance, Aggregation, Encapsulation and Overloading

  • 1.
  • 2.
    Inheritance in JavaP.2  Is a mechanism in which one acquires all the properties and behavior of parent object  You can create new classes that are built upon exist classes  You can reuse methods and fields of patent class, and add new.  Inheritance represents the IS-A relationship. The extends keyword indicates that you are making a new class that derives from an existing class
  • 3.
    Inheritance Cont. :P.3 C IS-A D and B IS-A D class D class Cclass B There can be three types of inheritance in Java: Single, multilevel and hierarchical Note: Multiple inheritance is not supported in java through class.
  • 4.
  • 5.
  • 6.
    Aggregation In Java: P.6  Is class have an entity reference  Aggregation represents HAS-A relationship
  • 7.
  • 8.
  • 9.
    Encapsulation in JavaP.9  Is a process of wrapping code and data together into a single unit.  We can create a fully encapsulated class in Java by making all the data member of the class private.  Now we can use setter and getter methods to set and get the data in it.  It provides you the control over the data.
  • 10.
  • 11.
    Overloading In Java: P.11  Is class has multiple methods by same name but different parameters.  Increases the readability of the program. There are two ways to overload the method: 1. by changing by of arguments 2. by changing the data type We can not overloading by changing the return type of the method
  • 12.
  • 13.
  • 14.
    Overloading Cont. :P.14 One type is promoted to another implicitly if no matching datatype is found
  • 15.
    cdf Java static keyword:P.15  Is used for memory management mainly.  Variable , methods, blocks and nested class. Static variable : If you declare any variable as static, it is know static variable. • Can be used to refer the common property of all object. • Gets memory only once in class area at the time of class loading. s1 s2 num=1 fullname=“Maala: num=2 fullname=“Elian” college=“IT” Stack memory Heap memory
  • 16.
  • 17.
  • 18.
    Static method: P.18 Applystatic keyword with any method.  belongs to the class rather than object of a class.  Can be invoked without the need for creating an instance of a class.  Can access static data member and can change the value of it.
  • 19.
    Example 1: P.19 P.S: 1.The static method can not use non static data member or call non static method directly. 2. This and super cannot be used in static context.
  • 20.
    Static block P.20 Is used to initialize the static data member.  It is executed before main at the time of class loading. P.S: You can execute a program without main() method, with static block but In previous version of JDK not in JDK 1.7
  • 21.
    Java this keyword:P.21 this is a reference variable that refer to the current object. a.) can be used to refer current class instance variable.
  • 22.
    this keyword Cont.: P.22 b.) this() can be used to invoked current class constructor:
  • 23.
    P.23 c.) you canused this() constructor call: this keyword Cont. : Call to this() must be the first statement in constructor
  • 24.
    P.24 d.) this keywordcan be used to invoke current class method (implicitly) this keyword Cont. :
  • 25.
    P.25this keyword Cont.: e.) this keyword can be passed as an argument in the method:
  • 26.