Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 By Waqas 1

2 2

3 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

4 By Waqas 4

5 5

6 6 Class Declaration class classname { variable 1; variable 2; method 1; method 2; }

7 By Waqas 7 Car class class Car { Int gears; Int wheels; public void changeGear( ); public void brake( ); }

8 By Waqas 8

9 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

10 By Waqas 10 The object creation statement has three parts. Car c = new Car( ); DeclarationInstantiationInitialization 1. Declaration 2. Instantiation 3. Initialization

11 By Waqas 11 Declaration You 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.

12 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

13 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

14 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.

15 By Waqas 15

16 By Waqas 16 Constructor Constructor 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.

17 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.

18 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.

19 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

20 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

21 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;

22 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

23 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;

24 By Waqas 24

25 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)

26 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.

27 By Waqas 27 If you does not assign any value to any member variable all the variables are assigned a default value: - TypeInitial ValueTypeInitial Value byte0short0 int0long0 float0.0double0.0 char‘\u0000’booleanFalse Objectnull

28 By Waqas 28 Each object has its own copy of instance variables, which can be modified without effecting other objects variables. Brake Change gear speed = 15speed = 10 Color = redColor = blue Number of gears = 4 Your CarMy Car

29 By Waqas 29

30 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

31 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.

32 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;

33 By Waqas 33

34 By Waqas 34 Class variableInstance 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

35 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

36 By Waqas 36

37 By Waqas 37 Method A method is a group of programming language statements that are given a name. Method syntax Return-type method-name (parameters) { statement 1; statement 2; ….. }

38 By Waqas 38 void amethod ( int a, int b ) { statement 1 statement 2 …….. } Declaration Body Method has two major parts: 1. Method Declaration 2. Method Body

39 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”); }

40 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; }

41 By Waqas 41

42 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

43 By Waqas 43 static Return-type method-name (parameters) { statement 1; statement 2; ….. } Static Methods Syntax

44 By Waqas 44

45 By Waqas 45 Static MethodInstance 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

46 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( );

47 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.

48 By Waqas 48 There are also static block of code in java, which have the following syntax: static { statements; } Static Blocks

49 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.

50 By Waqas 50

51 By Waqas 51 The basic syntax for specifying an inherited class is: Class child-class extends parent-class { Class body … } Deriving Classes

52 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.

53 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;

54 By Waqas 54

55 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.

56 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( );

57 By Waqas 57

58 By Waqas 58 Method Overloading In 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.

59 By Waqas 59

60 By Waqas 60 final keyword The 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.

61 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( ) { }

62 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 { }

63 By Waqas 63

64 By Waqas 64 Access Modifiers Access 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. private 2. protected 3. public 4. default.

65 By Waqas 65 private The 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 ( ) { }

66 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. ABCABC ABCABC ABCABC XYZ P1 ABCABC ABCABC ABCABC DFE P2

67 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 ( ) { }

68 By Waqas 68 ABCABC ABCABC ABCABC XYZ P1 ABCABC ABCABC ABCABC DFE 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.

69 By Waqas 69 public The 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 ( ) { }

70 By Waqas 70 ABCABC ABCABC ABCABC XYZ P1 ABCABC ABCABC ABCABC DFE 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.

71 By Waqas 71 default The 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.

72 By Waqas 72 ABCABC ABCABC ABCABC XYZ P1 ABCABC ABCABC ABCABC DFE 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.

73 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.

74 By Waqas 74

75 By Waqas 75 Abstract Classes An 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.

76 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.

77 By Waqas 77 abstract class Number { int a; int b; abstract void one( ); abstract void two( ); public void three( ) { } } Syntax

78 By Waqas 78

79 By Waqas 79 Abstract Methods An 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.

80 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.

81 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.

82 By Waqas 82

83 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.

84 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.

85 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

86 By Waqas 86

87 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,

88 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.

89 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.

90 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.

91 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.

92 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.

93 By Waqas 93

94 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

95 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.

96 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

97 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 { }

98 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.

99 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.

100 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

101 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;

102 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

103 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.”

104 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.

105 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.

106 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.


Download ppt "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."

Similar presentations


Ads by Google