Download
is f213 object oriented programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
IS F213 Object Oriented Programming PowerPoint Presentation
Download Presentation
IS F213 Object Oriented Programming

IS F213 Object Oriented Programming

140 Views Download Presentation
Download Presentation

IS F213 Object Oriented Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. Today’s Agenda • Compiling/Executing Java Programs (Review) • Object Basics • Three Pillars of OOP • Basic Java Syntax • Java Type System • Differences Between C & Java • System.out.println() and System.out.print() methods

  3. Java Features • Compiled and Interpreted • Platform Independent • Architectural Neutral

  4. Java Program Structure • Documentation Section • /**…… */ Documentation Comments • // Single Line Comment • /*…… */ Multi line Comments Package Statement Import Statements Interface Statements Class Definitions Main Method class

  5. Compiling/Executing a Java Application Source Code << .java file>> << javac .java >> Java Compiler ByteCode << .class file>> Java Interpreter << java [name of class] >> Machine Code

  6. 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)

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

  8. Methods Methods Data Methods Methods What’s Object • Term Object Means Combination of Data (Attributes) and Logic (Behavior, Functions, Operations) of some real world entity • Every object has two parts : - Data Part [ Instance Fields in Java, Attributes or properties] - Operations Part [ methods in java, Behavior] • Examples : 1. Account : Attributes : Account Holder, Account type [saving , current] , Balance Operations : deposit, withdraw, 2. BOX: Attributes : length, width, height, color Operations : compute area, compute volume Object keeps Data Part + Operation Part Together

  9. What’s class • Objects are grouped in classes • Class collections of objects having similar behavior and properties • A single object is simply an instance of class. • Objects can not be instantiated (or created) without defining a class • Classes are defined whereas objects are created.

  10. Object State • Properties/Attribute Values at a particular moment represents the state of an object • Object may or may not change state in response to an outside stimuli. • Objects whose state can not be altered after creation are known as immutable objects and class as immutable class [ String class in Java] • Objects whose state can be altered after creation are known as mutable objects and class as mutable class [ StringBuffer class in Java] States of Three Different INSTANCES of CAR CLASS MARUTI800 Name : maruti800 Price : 200789 Color : Red MarutiESTEEM Name : marutiEsteem Price : 500789 Color : whilte MARUTIZEN Name : marutiZen Price : 200789 Color : metallic white

  11. Data Abstraction • Way of managing complexity • Abstraction refers to the act of representing essential features that are of interest of the users without including background details or explanation • Classes use the concept of encapsulation for hiding unnecessary implementation details

  12. Pillars of OOP • Encapsulation • Inheritance • Polymorphism

  13. Methods Methods Data Methods Methods Encapsulation • Encapsulation means wrapping up of data and methods (operations , code) together • Access to code and data is tightly controlled. • Through we can define what and what can not be accessible outside. [ public , private , protected ] Encapsulation keeps Data Part + Operation Part Together inside a capsule

  14. BOX Class area() length width height volume() Encapsulation Example class BOX { private double length; private double width; private double height; public double area() { } public double volume() { } } // End of class BOX

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

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

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

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

  19. 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)

  20. 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]

  21. Java’s Primitive Types

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

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

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

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

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

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

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

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

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

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