220 likes | 317 Views
IS F213 Object Oriented Programming. Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani. Today’s Agenda. Basic Java Syntax Java Type System Differences Between C & Java System.out.println () and System.out.print () methods . Example 1. class Test1 { public static void main(String[] x)
E N D
IS F213 Object Oriented Programming Dr. Yashvardhan Sharma CSIS Dept., BITS-Pilani
Today’s Agenda • Basic Java Syntax • Java Type System • Differences Between C & Java • System.out.println() and System.out.print() methods
Example 1 class Test1 { public static void main(String[] x) { System.out.println("Hello Java"); } } Name of Source File is yash.java D:\programs>javac yash.java D:\programs>java Test1 Hello Java D:\program>java test1 Exception in thread "main" java.lang.NoClassDefFoundError: test1 (wrong name: Test1)
Example 2 // oop.java class 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>javacoop.java D:\programs>java oop Exception in thread "main" java.lang.NoClassDefFoundError: oop D:\programs>java A This is class A D:\programs>java B This is class B
A <<class>> <<class>> B class B extends A 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 <<interface>> <<class>> B class B implements A
Various Forms of Inheritance Hierarchical Inheritance Single Inheritance A A X X B B A B C A B C NOT SUPPORTED BY JAVA MultiLevel Inheritance Multiple Inheritance SUPPORTED BY JAVA A A A B A B B B C C C C
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 • Compile-Time [ Method Overloading] • Run-Time [ Method Overriding]
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 Signatures sum(int,int) • int sum (int a, int b) • float sum (int a, float b) • double sum(double a , double b) • void draw(); sum(int,float) sum(double,double) draw() Note : return type and name of arguments are not part of signatures
Method Overloading Examples • void sum(int a,int b) • int sum(float a, float b) • double sum (double a , double b) • int sum(int a, float b) • float sum(float a,int b) Overloaded Methods 1,2 and 3,4 Not Overloaded 1,3 and 2,4 are overloaded • void sum(int a,int b) • int sum(int x, int y) • double sum (double a , double b) • float sum(double a1, double b)
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]
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.
Example D:\java\bin>javac test1.java test1.java:6: possible loss of precision found : int required: byte byte b1 = 678; ^ test1.java:8: possible loss of precision found : int required: char char y = 70000; ^ test1.java:9: possible loss of precision found : int required: char char y1 = -25; ^ test1.java:10: possible loss of precision found : int required: short short x1 = 238999; ^ test1.java:11: possible loss of precision found : double required: float float f = 678.45; ^ 5 errors class 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; } }
Example 2 In C In Java int a=10; if(10) printf("Hello"); else printf("Hi"); } int a=10; if(10) S.O.P("Hello"); else S.O.P("Hi"); } OUTPUT D:\java\bin>javac test100.java test100.java:6: incompatible types found : int required: boolean if(a) ^ 1 error OUTPUT Hello
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 test101 10 1.2200000000000006
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 -4096 1073741568
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
Hello10 30 1020 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 Hello20is my age
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
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 test104 HelloI am fine It is OK
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 test105 HelloI am fine It is OK It is OK Again