Day2

Java Essentials

Primitive Data Types in Java
‡ Java Programming language is strongly typed
± All variables must be first declared before they can be used

‡ Byte: 8 bit signed integer. Values minimum ± (-128) to max 127(inclusive) ‡ Short: 16 bit signed integer. Values min(-32,768) to 32767(inclusive) ‡ Int: 32 bit signed integer. Values minimum(2,147,483,648 ) to (2,147,483,647 ) ‡ Long:64 bit signed integer ‡ Float: 32 bit(3.4e-038 to 3.4e+038) ‡ Double:64 bit ‡ Char:16 bit Unicode character ‡ Boolean can only take true or false
2

Scope
‡ Local Scope: Variable will not be available outside its scope ‡ public static void main(String args[]) { int x; if(x==10) { int y = 20; System.out.println(³x is ´+ x +´y is´ + y); } System.out.println(³y is ³ +y); //error }
3

Scope Error
public static void main(String args[]){ int bar =1; { int bar =2; } }

4

Type Casting
‡ Automatic Type Conversions
± Two types are compatible ± Destination type is larger than source type ± This type of conversion is called widening conversion

‡ Casting Incompatible Types int a; byte b; b = (byte) a;

5

6 .23. b = b*2. ‡ Automatic Type Promotion byte b = 50.Type Casting ‡ Truncation Conversion int i. i = (integer)d. double d = 1.

e. Act as a blueprint ‡ A class is a logical construct ‡ Object is nothing but an instance of the class ‡ An object has physical reality i.Classes and objects ‡ A class creates a new data type that can be used to create objects. it occupies space in memory 7 .

Declaring Objects ‡ Class-var = new classname(). ‡ Classname followed by parentheses specifies the constructor for the class ‡ A constructor defines what occurs when an object of a class is created 8 .

9 .

Introducing methods ‡ Although it is perfectly fine to create a class that only contains data. 10 . Can be any valid type. return value. rarely happens ‡ Most of the time methods to access the instance variables defined by class ‡ What if another part of the program want volume? ‡ Type name (parameter-list){ body of method } Type specifies the type of data returned.

11 .

} Int x = square(5).Method that takes parameter ‡ Adding a Method That Takes Parameters int square(){ Return 10*10 } Int square(int i){ return i*I. 12 .

Method that takes parameter ‡ Parameter is a variable defined by a method that receives a value when method is called ‡ An argument is a value that is passed to a method when it is invoked 13 .

volume(). 20. depth = d.Method that takes parameter ‡ Can use a parameterized method to improve Box class ‡ In previous example mybox1. 5). double d){ Width = w. etc Code is clumsy and error prone Never a good idea to access variables directly Void setDim(double w. height = h. mybox1.width=10. Vol=mybox1. 14 . double h.setDim(10.

Constructors ‡ It can be tedious to initialize all of the variables in a class each time an instance is created ‡ Even setDim is not right ‡ A constructor initializes an object immediately upon creation ‡ It has the same name as the class in which it resides and is syntactically similar to method ‡ Once defined. before new operator completes 15 . constructor is automatically called immediately after the object is created.

not even void ‡ Implicit return type of a class constructor is the class type itself ‡ Creating an instance will have fully initialized usable object immediately ‡ Rework Box example so that dimensions are automatically initialized ‡ Replace setDim with a constructor 16 .Constructors ‡ Constructors do not have any return type.

Constructors Box2(){ ‡ System.} 17 . ‡ width = 10.println("Constructing Box"). ‡ depth = 10.out. ‡ height = 10.

double d){ ‡ Width=w. ‡ } 18 .Parameterized Constructors ‡ In the previous example it is not very useful ‡ So add parameters to constructor ‡ Box(double w. height=h. double h.

java defines the this keyword ‡ ³this´ can be used inside any method to refer to the current object ‡ Is always a reference to the object on which method was invoked 19 .The this keyword ‡ Sometimes the method will need to refer to the object that invoked it ‡ To allow this java.

‡ this.Instance Variable Hiding ‡ Illegal in java to declare to declare two local variables with same name inside the same or enclosing space ‡ Box(double width. ‡ this.height=height.depth=depth ‡ } 20 .width=width. double height. double depth){ ‡ this.

Overloading methods ‡ In java it is possible to define two or more methods within the same class that share the same name as long as different parameter declaration ‡ This is called method overloading ‡ One of the ways java implements polymorphism 21 .

³one interface. abs method is overloaded to handle all numeric types ‡ Left to the compiler to chose right version 22 . multiple methods´ ‡ In languages that do not support method overloading.Overloading methods ‡ One way java implements polymorphism i. each method has unique name ‡ In C.e. abs() ± absolute value of integer ‡ labs() ± absolute value of a long integer ‡ fabs() ± absolute value of a floating point value ‡ In java standard.

Changes made to parameter will change argument ‡ When a simple type is passed . it is done by use of call by value. Objects are passed by use of call-byreference 23 . Therefore changes made to parameter of the subroutine have no effects on the argument used to call.Argument Passing ‡ Call-by-value: Copies value of an argument into formal parameters. ‡ Call-by-reference: A reference to argument. not value is passed.

tail length) Dogs (trained. sex. indoor/outdoor) Labrador 24 . etc) Canine (Hunting Skills.Inheritance ‡ Very important component of object-oriented programming as allows hierarchical classifications ‡ Each class can use things that are unique to it ‡ Animal (Age. weight) Mammal (Gestation period.

Inheritance ‡ ‡ ‡ ‡ Enables you to create more specific classes Enables software reuse Parent class is called superclass Child class is called subclass 25 .

Inheritance Example Class A { int i. showij(){ System. int j.println(³I and j:´+ i+ ³´ + j).out. } } 26 .

} } 27 . { System.out. } void sum(){ system.println(i+j+k). void showk().out.Inheritance Example Class B extends A{ int k.println(k).

Inheritance Example Class SimpleInheritance{ Public static void main(String args[]} A superObj = new A(). superObj.j = 20. subObj. superObj.showk(). subObj.i = 10.i = 8. subObj. superObj.k = 9.showij().i = 7. subObj. subObj. B subObj = new B(). 28 .showij().

Java does not support the inheritance of multiple classes into a single sub class ‡ Although a subclass includes all of the members of its super class. cannot access private members 29 .Inheritance ‡ We can use a super class directly ‡ Unlike C.

setij(int x. } } 30 . j=y. private int j.Cannot Access Private Data Class A { int i. int y){ i=x.

void sum() { total = i+j } } 31 .Error Accessing Private Data Class B extends A { int total.

only access to those parts which are defined in superclass Boxweight weightbox = new Boxweight(3.9). « plainbox = weightbox. Box plainbox = new Box().5. 32 . «.Super class variable can Refrence sub class variable When a reference to a subclass object is assigned to a superclass reference variable.7.

// but cannot access ford specific features coz the parent class does not know about them 33 .Super class variable can Refrence sub class variable { Car x. x = escort. Ford escort.

inefficient ‡ Also subclass must be granted access to these members ‡ But usually superclass keeps details of its implementations to itself i. ‡ Duplicate in superclass.Using super ‡ Classes derived from box were not implemented as efficiently and robustly. encapsulation ‡ Java provides a solution to this with keyword ³super´ 34 .e.

Parameter list specifies any parameters needed by the constructor in superclass Super() must always be the first statement inside subclass constructor 35 .Using super ‡ A subclass can call a constructor method defined by its superclass by use of following form super(parameter-list).

super(w. } 36 . BoxWeight(double w.d). double d.Using Super Class BoxWeight extends Box{ double weight. double m).h. weight = m. double h.

Second use of super ‡ super.member 37 .

} Class B extends A{ int i. 2). i=b.i = a. System. B(int a. obj1. } 38 .i).out.} void show(){ System.println(³I in superclass ´ + super.show().out.println(³ I in subclass: ´ + i). } Class useSuper{ public static void main(String args[]) B obj1 = new B(1. int b){ super.Using Super Class A{ int i.

} Class B extends A{ B() { System.println(³ inside A¶s const´).out.println(³ inside B¶s const´). } } 39 .println(³ inside C¶s const´).out. } } Class C extends B{ C() { System.When Constructors are Called Class A{ A() { System.out.

} Output Inside A¶s Constructor Inside B¶s Constructor Inside C¶s Constructor 40 .Class callConst { C obj = new C().

Method Overriding ‡ When method in a subclass has same name and type signature as method in its superclass then method in subclass is said to override method in superclass 41 .

println(³ inside B¶s call me´).out.out.Dynamic Method Despatch Class A{ callme() { System. } Class B extends A{ callme() { System. } } 42 .println(³ inside A¶s callme´).println(³ inside C¶s callme´).out. } } Class C extends B{ callme() { System.

r. C c = new C().callme(). } 43 .callme(). A r . r = a. r =c. r. B b = new B(). r.Class Dispatch { public static void main(String args[]) A a = new A(). r = b.callme().

Abstract Classes ‡ Super class that declares the structure of a given abstraction without providing complete implementation ‡ Super class that defines a generalized form and leave specific implementation to subclasses ‡ All methods which needs to be declared are declared as abstract ‡ Any class which contains abstract methods is defined as abstract class 44 .

out.println(³This is a concrete method´). void callme() { System.Abstract Classes abstract class A { abstract void callme().out.println(³B¶s implementation of callme´). } } 45 . void callmetoo() { System. } class B extends A{ B b = new B().

it prevents the method from being overriden ‡ When used with a class name it prevents the class from being inherited. 46 .Using final ‡ Keyword final has three uses ‡ Create equivalent of a named constant ‡ When used with method name.

Passing Objects as Parameters ‡ Both Common and right to pass objects to classes ‡ Already defined classes as well as custom classes can be sent as parameters 47 .

Argument Passing ‡ Two Ways through which a computer Language can pass Arguments ± Call-by-value Method Copies the value of an argument into formal parameters of subroutine ± Call-by-reference Reference to an argument (not values of the arguments) is passed to the parameter. Reference used to access actual arguments 48 .

The Object Class ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ Object Clone() boolean equals(Object obj) Void finalize() Class getClass() String toString() Void notify Void notifyAll Void wait() 49 .

Lab3 50 .

Sign up to vote on this title
UsefulNot useful