You are on page 1of 23

Language Fundamentals

SEED Infotech Pvt. Ltd. 1
Objectives of This Session

Describe the process of object creation in context with
memory structure
Describe and demonstrate parameter-passing mechanism in
Describe how arrays are first class objects in Java using for
e.g. Swap date
State how Garbage Collector works
Describe containment
Construct “has-a” relationship (Containment hierarchy) using
Employee & Date class
Describe Inheritance
Construct “is-a” relationship (Inheritance hierarchy) using
Employee-Manager-SalesManager classes

SEED Infotech Pvt. Ltd. 2
Structure of Memory

Stack Area
Movable Boundary

Heap Area Dynamically allocated Area

Fixed Boundary Static And Global Variables
Data Area

Code Section

SEED Infotech Pvt. Ltd. 3
Class Date

 Objects always created on heap.
public static void main(String args[])
Date d1;
d1 = new Date();

d1 Date

stack heap
SEED Infotech Pvt. Ltd. 4
Garbage Collector

Java doesn’t allow memory leakage.
GC automatically de-allocates memory.
It de-allocates memory of objects that no longer
has any references I.e. reference count is zero.

SEED Infotech Pvt. Ltd. 5
Parameter passing in Java

In Java parameter parameter passing is done “
While passing primitive data types a copy of the
variable is created on the stack.
While passing objects as parameters the
references are passed by value ( a copy of the
reference “not the object “ is created on stack )

SEED Infotech Pvt. Ltd. 6

class A class Ref_Demo{
public static void change(A a) {
a.aa = 30;
int aa ; a = null;
A(int x) { }
aa = x; public static void main(String s[]){
} A a1 = new A(20);
System.out.println( a1 );
public String toString(){
return aa; System.out.println( a1 );
} }
} }

SEED Infotech Pvt. Ltd. 7
Swap method

public static void Swap(Date d1, Date d2)
Date temp = d1 ;
d1 = d2 ;
d2 = temp ;

SEED Infotech Pvt. Ltd. 8

Are first class objects in Java.
Eg int arr[ ] ;
arr = new int[10];
Eg int arr[ ] = {10, 20, 30, 40, 50};
arr reference is stored on stack.
The actual array is created on heap.

SEED Infotech Pvt. Ltd. 9
Swap() method

public static void Swap(Date [] dArr)
Date temp = dArr[0];
dArr[0] = dArr[1];
dArr[1] = temp;

SEED Infotech Pvt. Ltd. 10

Containment represents ‘has a’ relationship
Containment Relationship means the use of an object
of a class as a member of another class.
E.g. Birth date or joining date as a part of
Employee class
The container relationship brings reusability of code.
Eg Already written Date class can be used in
Class Employee.

SEED Infotech Pvt. Ltd. 11
Containment hierarchy (has-a)

class Emp{
int EmpID;
String EmpName;
Date BirthDate;

public Emp()
EmpID = 0;
BirthDate = new Date();

SEED Infotech Pvt. Ltd. 12
Containment hierarchy (has-a)

public Emp(int i, String nm, Date dt)
EmpID = i;
EmpName= nm;
BirthDate = dt;
public static void main(String args[]){
Emp e1 = new Emp();
Emp e2 = new Emp(2, “abc”,new Date(28,6,80));

SEED Infotech Pvt. Ltd. 13

Inheritance is one of the major pillars of OO approach.
Inheritance allows creation of hierarchical
Advantage of inheritance is reusability of the code.
Once a class is defined & debugged , same class
can be used to create further derived classes.
Already written code can be extended as & when
required to adopt different situation.

SEED Infotech Pvt. Ltd. 14
Super & sub classes

Vehicle Base Class Generalization

2- 3-
Wheeler wheeler Derived Classes Specialization

sub class inherits data member & methods of it’s
super class .

SEED Infotech Pvt. Ltd. 15
super & sub class

Employee This is ‘is-a’ kind of hierarchy.
More than one class can
inherit attributes from a single
Wage super class.
A sub class can be super
class to another class.
Sales Person

SEED Infotech Pvt. Ltd. 16
Inheritance Syntax

class super_class name
// body of super class
class sub_class name extends super_class
// body of derived class
Inheritance in Java is always public

SEED Infotech Pvt. Ltd. 17
class Wage Employee

class WageEmployee extends Emp
int m_hrs; // all the Employee class data
int m_rate; // members will be inherited in
// Wage Employee class

public WageEmployee( int hr, int rate )
m_hrs = hr ; // members of Wage Employee
m_rate = rate ; // class

SEED Infotech Pvt. Ltd. 18
Wage Employee cont..

public static void main (String srt[] )
WageEmployee we1= new WageEmployee (10, 100);

SEED Infotech Pvt. Ltd. 19
Derived class constructor

Constructors are called in the sequence of super ->

When we1 is created the sequence in which
constructors get invoked is
Employee -- WageEmployee

SEED Infotech Pvt. Ltd. 20
Compute Salary for Wage Employee

class WageEmployee extends Employee
float ComputeSalary ( )
return( m_hrs * m_rate );
public static void main(String str[]){

WageEmployee we1 = new WageEmployee( …);


SEED Infotech Pvt. Ltd. 21
Sales Person

Derive Sales Person class from Wage Employee with
the added attributes to Sales Person as Sale &

Create an object of class Sales Person

SEED Infotech Pvt. Ltd. 22
Sales Person

class SalesPerson extends float ComputeSalary1( ){
WageEmployee return (
{ super.ComputeSalary( ) +
float m_sales; m_sales * m_comm );
float m_comm; }

SalesPerson(int sales, int comm) public static void main (String
arg[] ){
SalesPerson sp1= new
m_sales = sales;
SalesPerson (85000,5);
m_comm = comm; }
} }

SEED Infotech Pvt. Ltd. 23