by waqas 1. 2 3 the topmost class in the java class hierarchy is called “object”. if you declare...

106
By Waqas 1

Upload: cornelius-patterson

Post on 05-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 1

Page 2: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 2

Page 3: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 3

The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class than your class is considered to be the child class of the class Object.

Java Class Hierarchy

Page 4: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 4

Page 5: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 5

Page 6: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 6

Class Declaration

class classname{

variable 1;

variable 2;

method 1;

method 2;

}

Page 7: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 7

Car class

class Car{

Int gears;

Int wheels;

public void changeGear( );

public void brake( );

}

Page 8: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 8

Page 9: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 9

You can create an object of class with the following syntax: -

Creating Objects

type variable = new type( );

So the car class object is created as: -

Car c = new Car( );

Car c;c = new Car( );

or

Page 10: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 10

The object creation statement has three parts.

Car c = new Car( );

Declaration Instantiation Initialization

1. Declaration

2. Instantiation

3. Initialization

Page 11: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 11

DeclarationYou declare variables of int type as: -

int a;You can say that a is a variable who can refer to any type of int data.

Classes in java are also types so you can declare class type variable as: -

Car c;You can say that c is a variable who can refer to any type of Car.

Page 12: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 12

Declaring a variable do not create any object. The code Car c; does not create a new car object, it just declare a variable named c that will be used to refer to a Car object. The reference is still empty until assigned with a new keyword.The empty reference is called null reference in java.

c

Page 13: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 13

Instantiation

The new operator instantiates a class by allocating a memory for a new object. The new operator returns a reference to the object it created and this reference is assigned to the appropriate variable.

Car object

c

Page 14: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 14

Initialization

The object is always initialize by calling a constructor. A constructor is a special method which has a same name as class and used to initialize the variables of that object.In this case the Car class object is initialized by calling the constructor of Car class Car( ); , you can notice that the constructor name is same as class.

Page 15: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 15

Page 16: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 16

ConstructorConstructor is a special method in every class which are used to initialize a new object variables.A constructor has the same name as the class. For example if you have Color class the constructor of color class is also Color( );Constructors cannot return any value not even void.

Page 17: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 17

Java supports name overloading of constructors so that a class can have number of constructors with the same name.

The compiler can determine which constructor to call by the number and type of arguments that you pass into the constructor.

Page 18: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 18

If we can not provide any constructor for out class than default (no parameter) constructor is automatically provided by the runtime system.

The constructor can be private, public or protected.

Page 19: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 19

When you create an object of child class then child class constructor first calls the parent class default constructor before it performs its own initialization. The parent class constructor calls its super class constructor and so on.

This is necessary because if the parent class is not correctly initialize how the child class object is created.

Constructor Calling Order

Page 20: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 20

If you don’t want to invoke the default constructor of super class but want other constructor than you use super to invoke the parent class constructor.

super(4);

This will invoke the parent class constructor that would take one int argument. Call to parent class constructor with super would be the first statement in

super

Page 21: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 21

the child class constructor.

You can also invoke any method or variable of super class in the child class with the super keyword.

The syntax of method is: -super.methodname( );

The syntax of variable is: -super.variablename = 7;

Page 22: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 22

If you want to invoke another constructor of current class in any constructor you use this to invoke current class constructor.

this(4, 5);

This will invoke that current class constructor that would take two int arguments. Call to current class constructor with this would be the first

this

Page 23: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 23

statement in the constructor.

You can also invoke any method or variable of current class with this keyword.

The syntax of method is: -this.methodname( );

The syntax of variable is: -this.variablename = 7;

Page 24: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 24

Page 25: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 25

Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed.

The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them.

Object Destruction (Garbage Collection)

Page 26: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 26

The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object (object goes out of scope). Or, you can explicitly drop an object reference by setting the variable to the special value null.

Page 27: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 27

If you does not assign any value to any member variable all the variables are assigned a default value: -

Type Initial Value Type Initial Value

byte 0 short 0

int 0 long 0

float 0.0 double 0.0

char ‘\u0000’ boolean False

Object null

Page 28: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 28

Each object has its own copy of instance variables, which can be modified without effecting other objects variables.

Brake Brake

Changegear

Changegear

speed = 15 speed = 10

Color = red Color = blue

Number of gears = 4

Number of gears = 4

Your Car My Car

Page 29: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 29

Page 30: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 30

The runtime system allocates class variables once per class regardless of the number of instances created of that class. The system allocates memory for class variables the first time it encounters the class. All instances share the same copy of the class's class variables. You can access class variables through an instance or through the class itself.

Creating Class Variables

Page 31: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 31

To declare a class variable follow the following syntax.

static Type variable;e.g.static int a;static double d;

If any object change the value of class variable, the change will effect the values of all the variables.

Page 32: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 32

Because the class variable belongs to the class rather than any specific variable, it can be accessed using the class name such as: -

ClassName.variableName;

Suppose you have class called Car and class variable called gears than you can access that variable like this: -

Car.gears;

Page 33: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 33

Page 34: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 34

Class variable Instance variable

Class variable is declared by using static keyword.

static int a = 4;

Instance variable is declared without static keyword.

int a = 4;

All objects share the single copy of static variable.

All objects have their own copy of instance variable.

Class vs Instance Variables

Page 35: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 35

Static variable does not depend on the single object because it belongs to a class.

Instance variable depends on the object for which it is available.

Static variable can be accessed without creating an object, by using class name.

ClassName.variable

Instance variable cannot be accessed without creating an object.

ObjectName.variable

Page 36: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 36

Page 37: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 37

MethodA method is a group of programming language statements that are given a name.

Method syntax

Return-type method-name (parameters){

statement 1;statement 2;…..

}

Page 38: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 38

void amethod ( int a, int b ){

statement 1statement 2……..

}

Declaration

Body

Method has two major parts:1. Method Declaration2. Method Body

Page 39: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 39

Method that does not return a value

If a method does not return a value, its return type must be declared void.

For example:

void abc( ){

System.out.println(“Hello World”);}

Page 40: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 40

Method that returns a value

Methods that have a return type other than void return a value to the calling routine using the following form of the return statement.

return value;

int abc( ){

return 5+5;}

Page 41: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 41

Page 42: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 42

The class (static) methods is not referenced through a particular instance of a class but through the class itself. You don’t have to create an object of the class to invoke a static method.

Creating Static Methods

Page 43: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 43

static Return-type method-name (parameters){

statement 1;statement 2;…..

}

Static Methods Syntax

Page 44: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 44

Page 45: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 45

Static Method Instance Method

Static methods are declared by using static keyword.

instance methods are declared without static keyword.

All objects share the single copy of static method.

All objects have their own copy of instance method.

Static vs Instance Methods

Page 46: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 46

Static method does not depend on the single object because it belongs to a class.

Instance method depends on the object for which it is available.

Static method can be invoked without creating an object, by using class name.

ClassName.method( );

Instance method cannot be invoked without creating an object.

ObjectName.method( );

Page 47: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 47

Static methods cannot call static methods.

Instance methods can call static methods.

Static methods cannot access the not-static variables.

Instance methods can access the non-static variables.

Static methods cannot refer to this or super.

Instance methods can refer to this and super.

Page 48: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 48

There are also static block of code in java, which have the following syntax:

static{

statements;}

Static Blocks

Page 49: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 49

NOTE:

Static variables, methods, blocks are always loaded in memory when the class is loaded. So the static blocks are used to initialize any variables when the class is loaded in memory.

Page 50: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 50

Page 51: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 51

The basic syntax for specifying an inherited class is:

Class child-class extends parent-class{

Class body…

}

Deriving Classes

Page 52: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 52

When the class extends any super class it inherits all the variables and methods from super class (except private members). The object of child class can call any non private method from the super class that is why we can say that:

A child class object is a parent class object.

In java every class can extend only one class.

Page 53: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 53

We can assign a reference variable of child class to the reference variable of parent class but opposite is not true.

Parent p = new Parent( );Child c = new Child( );p = c;c = p; //error

You have to do explicit type casting.

c = (Child) p;

Page 54: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 54

Page 55: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 55

Method Overriding

When a child class defines a method with the same name and signature as the parent, then it is said that the child’s version overrides the parent’s version in his favor.

When an overridden method is called from within a sub class, the version in the subclass will be called.

Page 56: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 56

The return type, method name, and number and type of the parameters for the overriding method in the child class must match those in the overridden method in the super class.

You can call the super class method from the child class with super keyword.

super.overriddenMethodName( );

Page 57: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 57

Page 58: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 58

Method OverloadingIn java it is possible to define two or more methods with the same name, within the same class. They share the same name as long as their parameters are different. This process is known as “method overloading”. When java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments.

Page 59: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 59

Page 60: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 60

final keywordThe keyword final has three uses:

1.Final variables.2.Final methods.3.Final classes.

1. Final variables

Final modifier used with the variables specify that the variable has a constant value and it can not changed.

Page 61: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 61

2. Final methods

When you use the keyword final with methods, then it specifies that the method cannot be overridden in the child class.

final int a = 34;

final void volume( ){

}

Page 62: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 62

3. Final classes

The keyword final can be applied to classes. If this is done, the class cannot be inherited. This provides security features and stops further modifications.

final class City{

}

Page 63: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 63

Page 64: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 64

Access ModifiersAccess modifiers define varying levels of access between class members and the outside world (other objects). They allow us to define the encapsulation characteristics of an object.There are four access modifiers in java: 1. private2. protected3. public 4. default.

Page 65: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 65

privateThe private access modifier is the most restrictive; it specifies that class members are accessible only by the class in which they are defined. This means that no other class has access to private class members, even subclasses.

private int a = 4;private void show ( ){}

Page 66: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 66

If A,B,and C are declared private inside X class, then Y,Z,D,E and F all classes cannnot access the A,B and C. A,B and C are only accessible inside X class.

ABC

ABC

ABC

X YZ

P1

ABC

ABC

ABC

D FE

P2

Page 67: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 67

protected

This modifier specifies that class members are accessible only to methods in that class, the classes inside the package and subclasses of that class outside the package.

protected int a = 4;protected void show ( ){}

Page 68: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 68

ABC

ABC

ABC

X YZ

P1

ABC

ABC

ABC

D FE

P2

If A,B and C are declared protected inside X class then Y and Z can access A,B and C, further if any class in P2 package extends X class in P1 package then it can also access A,B and C.

Page 69: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 69

publicThe public access modifier specifies that class variables and methods are accessible to anyone, both inside and outside the class. This means that public class members have global visibility and can be accessed by any other object.

public int a = 4;public void show ( ){}

Page 70: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 70

ABC

ABC

ABC

X YZ

P1

ABC

ABC

ABC

D FE

P2

If A,B and C are declared public inside X class then all classes Y,Z,D,E and F can access A,B and C whether they are declared in any package.

Page 71: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 71

defaultThe default (package) access modifier specifies that only classes in the same package can have access to a class's variables and methods. Class members with default access have a visibility limited to other classes within the same package. There is no actual keyword for declaring the default access modifier; it is applied by default in the absence of an access modifier.

Page 72: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 72

ABC

ABC

ABC

X YZ

P1

ABC

ABC

ABC

D FE

P2

If A,B and C are declared without any access specifier (default or package) in X class then only Y and Z classes in the P1 package can access A,B and C.

Page 73: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 73

int a = 4;void show ( ){

}

NOTE:

public and default modifiers can also be used with classes. Top level classes can not used private and protected modifiers.

Page 74: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 74

Page 75: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 75

Abstract ClassesAn abstract class is a class that is partially implemented and whose purpose is solely represent abstract concept. Abstract classes are made up of one or more abstract methods, which are methods that are declared but left bodiless (unimplemented). Abstract classes cannot be instantiated because they represent abstract concept.

Page 76: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 76

For example: Food represents the abstract concept of things that we can eat, that is why we never saw the instance of food what we see instead are instances of burger, apple, chocolate. In OOP classes such as Number represents the abstract concept of numbers therefore should not be instantiated. What we instantiate the subclasses of Number such as Float, Integer etc.

Page 77: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 77

abstract class Number{

int a;int b;abstract void one( );abstract void two( );public void three( ){ }

}

Syntax

Page 78: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 78

Page 79: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 79

Abstract MethodsAn abstract method is a method which have no implementation (body). All the body of this method is provided by a subclass of the class in which the abstract method is declared. The syntax of abstract method is:

abstract void draw( );

There are no { } brackets in the declaration.

Page 80: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 80

Abstract Classes & Methods Concepts

• Abstract class can not be instantiated it means that you can not create object of abstract class with new keyword.

• Abstract class can only be sub classed.

• A class derived from abstract class must override all of the parent class’s abstract methods. If it does not define all methods then it will declare himself as abstract.

Page 81: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 81

• An abstract class can inter mix abstract and non abstract methods.

• Abstract class and method cannot be declared as final.

• abstract method cannot be declared as static.

• Abstract method cannot be declared as private.

Page 82: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 82

Page 83: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 83

Nested Classes

Java lets you define a class as a member of another class. Such a class is called nested class. As a member of its enclosing class, a nested class has a special privilege: It has unlimited access to its enclosing class's members, even if they are declared private.

Page 84: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 84

public class OuterClass{

class InnerClass{

}}

Like other members, a nested class can be declared static. A static nested is called just a static nested class. A nonstatic nested class is called inner class.

Page 85: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 85

1.Like static methods static nested class cannot directly refer to the instance methods and variables.

2.Nested classes can be declared abstract.

3.Nested classes can be declared final.4.Nested classes can also be declared

private, public, protected or default just as other member methods.

Some Facts about Nested Classes

Page 86: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 86

Page 87: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 87

Interfaces

An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface. Because an interface is simply a list of unimplemented, and therefore abstract,

Page 88: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 88

methods, you might wonder how an interface differs from an abstract class. The differences are significant.

1.An interface cannot implement any method where as an abstract class can.

2.A class can implement many interfaces but can implement only one super class.

3.An interface is not part of the hierarchy. Unrelated classes can implement the same interface.

Page 89: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 89

Interface

Hierarchy A Hierarchy B

Interface is just like bridge between two unrelated classes so that they can share common data and implementations.

Page 90: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 90

Interface Syntax

interface Data{

public final static int a = 4;public final static int b = 5;public abstract void show( );public abstract void volume( );

}

In interface, all variables are by default public final and static, and all methods are by default public abstract.

Page 91: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 91

Implementing Interface

Public class HelloWorld implements Data, Driver{

}

Class can implement more than one interface. The above class must need to define all the methods in the interface Data and Driver, otherwise it declares himself abstract.

Page 92: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 92

Extending Interface

Interface Data extends First,Second{

}

An interface can extends any number of interfaces. If any class implements the above interface it must have to define all the methods in the Data interface and all declared in First and Second.

Page 93: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 93

Page 94: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 94

A package is a collection of related classes and interfaces.

To make classes easier to find out to use, to avoid naming conflicts and to control access, programmers bundle groups of related classes and interfaces into packages.For example you have classes graphics objects like Circle, Square, Rectangle etc.

Packages

Page 95: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 95

You should bundle these classes into package for the following reasons:

1.You are other programmers can easily determine that these classes are related.

2.You and other programmers know that where to find classes that provide graphics related functions.

3.The name of your class wouldn’t conflict with class names in other packages.

Page 96: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 96

4.You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package

To create a package we put the package statement at the top of the source file in which the classes and interfaces are defined.

Creating Packages

Page 97: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 97

For example:To put Circle the class inside graphics package, the following code appears inside the Circle.java file.

package graphics;public class Circle{

}

Page 98: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 98

To put Rectangle the class inside graphics package, the following code appears inside the Rectangle.java file.

package graphics;public class Rectangle{

}

Now both Circle and Rectangle classes are in the graphics package.

Page 99: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 99

Only public package members are accessible outside the package in which they are defined.So for example if you want Circle class to available outside the package. You must use the class fully qualified name as follows:

graphics.Circle c = new graphics.Circle( );

This makes your code difficult to read.

Page 100: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 100

import Statement

Java provide a solution to limit the use of fully qualified name each time you create an object. You can just import the class.To import a specific member into the current file, put an import statement at the beginning of your file before any class or interface definitions but after the package statement, if there is one. Here's how you would import the Circle class from the graphics package

Page 101: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 101

import graphics.Circle;

Now you can refer to Circle class by its simple name:

Circle c = new Circle( ):

To import all the classes in the graphics package you can use * as wild card character as follows:

import graphics.Rectangle;

Page 102: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 102

Managing CLASSPATH

When you create a package such as graphics. A new directory is created in the file system with the package name and the class file (bytecode) would be compiled inside that package folder.The graphics folder might be any where in the file system.When the compiler encounters a new class as its compiling your program, it must be able to find the class so as to resolve

Page 103: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 103

names, do type checking, and so on.Similarly, when the interpreter encounters a new class as its running your program, it must be able to find the class to invoke its methods, and so on. Both the compiler and the interpreter search for classes in each directory or ZIP file listed in your class path.

“A class path is an ordered list of directories or ZIP files in which to search

for class files.”

Page 104: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 104

Compile every class which has package statement (such as Circle.java) with the -d option of javac command:

javac –d C:\ Circle.java

Here –d specifies that place the package and class file in the given directory.

C:\ specifies that place the package on the C:\ drive.

Page 105: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 105

Compile every class which import the package with the -classpath option of javac command:

javac –classpath C:\ Test.java

Here –classpath specifies the place where java compiler need to search the import classes.C:\ specifies that place the package on the C:\ drive.

Page 106: By Waqas 1. 2 3 The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class

By Waqas 106

Run every class which import the package with the -classpath option of javac command:

javac –classpath .;C:\ Test

Here –classpath specifies the place where java compiler need to search the import classes..;C:\ specifies that place the package on the C:\ drive and also keep the previous path set.