Lec 3

21
BITS Pilani, Pilani Campus BITS Pilani Pilani Campus Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani IS F213 Object Oriented Programming

description

 

Transcript of Lec 3

Page 1: Lec 3

BITS Pilani, Pilani Campus

BITS PilaniPilani Campus

Dr. Yashvardhan SharmaCSIS Dept., BITS-Pilani

IS F213 Object Oriented Programming

Page 2: Lec 3

BITS Pilani, Pilani Campus

Today’s Agenda

• Basic Java Syntax

• Java Type System

• Differences Between C & Java

• System.out.println() and System.out.print() methods

Page 3: Lec 3

BITS Pilani, Pilani Campus

Example 1

class Test1{public static void main(String[] x){System.out.println("Hello Java");}} D:\programs>javac yash.java

D:\programs>java Test1Hello Java

D:\program>java test1Exception in thread "main" java.lang.NoClassDefFoundError: test1 (wrong name: Test1)

Name of Source File is

yash.java

Page 4: Lec 3

BITS Pilani, Pilani Campus

Example 2

// oop.javaclass A{public static void main(String args[]){System.out.println("This is class A");}}class B{public static void main(String args[]){System.out.println("This is class B");}}

D:\programs>javac oop.java

D:\programs>java oopException in thread "main" java.lang.NoClassDefFoundError: oop

D:\programs>java AThis is class A

D:\programs>java BThis is class B

Page 5: Lec 3

BITS Pilani, Pilani Campus

Inheritance

• Process by which one object acquires properties of other classes• Supports Reusability of code and data• The class whose properties are extended is known as super or base or

parent class.• The class which extends the properties of super class is known as sub

or derived or child class • In Java a class can either extends another class or can implement an

interface

A

B <<class>>

<<interface>>A

B

<<class>>

<<class>>

class B extends A class B implements A

Page 6: Lec 3

BITS Pilani, Pilani Campus

Various Forms of Inheritance

A

B

Single Inheritance

A

B

Hierarchical Inheritance

X

A B C

X

A B C

MultiLevel Inheritance

A

B

C

A

B

C

A B

C

Multiple InheritanceNOT SUPPORTED BY JAVA

A B

C

SUPPORTED BY JAVA

Page 7: Lec 3

BITS Pilani, Pilani Campus

Polymorphism

• Poly means many and morph means forms. • One Interface Many forms• One Interface for several general class of actions• In Java we have two types of polymorphisms

1. Compile-Time [ Method Overloading]2. Run-Time [ Method Overriding]

Page 8: Lec 3

BITS Pilani, Pilani Campus

Method OverLoading

• Two methods are said to be overloaded if they have same name and different signatures.

• Signature of method means number of arguments to method and their types.

• Two methods are said to have different signatures if they differ in number of arguments they receive or their types

• Example Signature

1. int sum (int a, int b)

2. float sum (int a, float b)

3. double sum(double a , double b)

4. void draw();

sum(int,int)

sum(int,float)

sum(double,double)

draw()

Note : return type and name of arguments are not part of signatures

Signatures

Page 9: Lec 3

BITS Pilani, Pilani Campus

Method Overloading Examples

1. void sum(int a,int b)

2. int sum(float a, float b)

3. double sum (double a , double b)

4. int sum(int a, float b)

5. float sum(float a,int b)

Overloaded Methods

1. void sum(int a,int b)

2. int sum(int x, int y)

3. double sum (double a , double b)

4. float sum(double a1, double b)

1,2 and 3,4 Not Overloaded

1,3 and 2,4 are overloaded

Page 10: Lec 3

BITS Pilani, Pilani Campus

Java Type System

• Type specifies set values + set of operations that you can perform with those values

• Java is strongly Typed Language• Every Type in Java is one of the following: (i) Primitive Types( int, short, byte, long, char, float, double, boolean) (ii) A class Type (iii) An interface Type (iv) An array Type (v) null type [void is not type in java]

Page 11: Lec 3

BITS Pilani, Pilani Campus

Java’s Primitive Types

Type Size Minimum Value Maximum Valuebyte 1 -128 +127

short 2 -32768 +32767

int 4 -2,147,483,648 +2,147,483,647

long 8 -9,223,372,036,854,775,808

+9,223,372,036,854,775,807

float 4 3.4e-038 3.4+038

double 8 1.7e-308 1.7e+308

char 2 0 65535

boolean 1 bit

Page 12: Lec 3

BITS Pilani, Pilani Campus

C vs Java

• Does not have statements like goto, sizeof, typedef• Does not support data types as struct,union • No Explicit Pointer Type• No auto, extern, register, signed, unsigned • Java requires that function with no return type should be

explicitly declared as void void show() ; void print();• Java adds a new operator instanceof and >>> (unsigned

right shift)• Java adds a labelled break and continue statements• Return type of all conditional, logical or relational

expressions is boolean in java and not integer as in C.

Page 13: Lec 3

BITS Pilani, Pilani Campus

Exampleclass test1{public static void main(String args[]){byte b = 24; byte b1 = 678;char x = 45;char y = 70000;char y1 = -25;short x1 = 238999;float f = 678.45;double f1 = 56.67;}}

D:\java\bin>javac test1.javatest1.java:6: possible loss of precisionfound : intrequired: bytebyte b1 = 678; ^test1.java:8: possible loss of precisionfound : intrequired: charchar y = 70000; ^test1.java:9: possible loss of precisionfound : intrequired: charchar y1 = -25; ^test1.java:10: possible loss of precisionfound : intrequired: shortshort x1 = 238999; ^test1.java:11: possible loss of precisionfound : doublerequired: floatfloat f = 678.45; ^5 errors

Page 14: Lec 3

BITS Pilani, Pilani Campus

Example 2

int a=10;if(10)printf("Hello");elseprintf("Hi");}

OUTPUT

Hello

int a=10;if(10)S.O.P("Hello");elseS.O.P("Hi");}OUTPUTD:\java\bin>javac test100.javatest100.java:6: incompatible typesfound : intrequired: booleanif(a) ^1 error

In C In Java

Page 15: Lec 3

BITS Pilani, Pilani Campus

Example 3% Operator

class test101{public static void main(String args[]){int a=100, b=90;System.out.println(a%b);double a1= 10.56, b1 =4.67;System.out.println(a1%b1);}}

D:\java\bin>java test101101.2200000000000006

Page 16: Lec 3

BITS Pilani, Pilani Campus

Example 4 >>,<<,>>>

class test103{public static void main(String args[]){int x = -1024;System.out.println(x>>2);System.out.println(x<<2);System.out.println(x>>>2);}} D:\java\bin>java test103

-256-40961073741568

Page 17: Lec 3

BITS Pilani, Pilani Campus

System.out.println()

• Prints/Displays output and shifts the print control to new line (Similar printf(“\n”) in C)

• Displays output only in String form• If parameter to it is not in String form then it

will be converted to string form by internally calling toString()

• + operator can be used to concatenate data from different types

Page 18: Lec 3

BITS Pilani, Pilani Campus

Examples

• System.out.println(“Hello”+10);• System.out.println(10+20);• System.out.println(“10”+20);• System.out.println(“Hello: ”+20+”is my age”);

Note :+ opeartor is used for dual purpose addition,concatenation

Hello1030

1020

Hello20is my age

Page 19: Lec 3

BITS Pilani, Pilani Campus

System.out.print()

• Prints/Displays output starting from the same line (Similar printf() in C)

• Displays output only in String form• If parameter to it is not in String form then it

will be converted to string form by internally calling toString()

• + operator can be used to concatenate data from different types

Page 20: Lec 3

BITS Pilani, Pilani Campus

Examples

class test104{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");}}

D:\java\bin>java test104HelloI am fine It is OK

Page 21: Lec 3

BITS Pilani, Pilani Campus

Example 2

class test105{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");System.out.println(" It is OK Again");}}

D:\java\bin>java test105HelloI am fine It is OK It is OK Again