Object-Oriented Programming Concepts in Java

chapter 5 object and class n.w
1 / 156
Embed
Share

Dive into the world of Object-Oriented Programming with Java. Explore classes, objects, instance variables, methods, constructors, static keywords, and more through practical examples. Enhance your knowledge of Java programming by learning about instance variables, methods, and code reusability for optimal performance.

  • Java Programming
  • Object-Oriented Programming
  • Classes
  • Methods
  • Constructors

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter 5 Object and Class object and class instance variable overload method method constructor static keyword this keyword 1

  2. Object 2

  3. 3 state: behavior: identity: ID 3

  4. Class data member method constructor block class and interface 4

  5. class <class_name> { data member; method; } ------------------------------- class Student1{ int id; //data member instance variable) String name; //data member instance variable ---------------------------------- public static void main(String args[]){ Student1 s1=new Student1(); // object student System.out.println(s1.id); System.out.println(s1.name); } } 5

  6. Instance variable Java . Instance variable (compile time) (runtime) new 6

  7. Method Java function (Code Reusability) (Code Optimization) 7

  8. class Student2{ int rollno; String name; void insertRecord(int r, String n){ rollno=r; name=n; } //method void displayInformation(){ System.out.println(rollno+" "+name); } //method 8

  9. public static void main(String args[]){ Student2 s1=new Student2(); Student2 s2=new Student2(); s1.insertRecord(111,"Karan"); s2.insertRecord(222,"Aryan"); s1.displayInformation(); s2.displayInformation(); } } 9

  10. 10

  11. class Rectangle{ int length; int width; void insert(int l,int w){ length=l; width=w; } void calculateArea(){System.out.println(length*width);} public static void main(String args[]){ Rectangle r1=new Rectangle(); Rectangle r2=new Rectangle(); 11

  12. r1.insert(11,5); r2.insert(3,15); r1.calculateArea(); r2.calculateArea(); } } Output:55 45 12

  13. new keyword newInstance() method clone() method factory method etc. 13

  14. Annonymous object Object reference class AnoSample{ void display( ){ System.out.println( Hello anonymous ); } public static void main(String args[]){ new AnoSample().display(); // method annonymous AnoSample object } } 14

  15. Method Overloading method parameters parameters 15

  16. method a(int,int) 2 parameters b(int,int,int) 2 parameters method overload Add(int a ,int b) Add(int a ,int b, int c) 16

  17. Change parameters numbers class Calculation{ void sum(int a,int b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} public static void main(String args[]){ Calculation obj=new Calculation(); obj.sum(10,10,10); obj.sum(20,20); } } Output:30 40 17

  18. Change parameter types class Calculation2{ void sum(int a,int b){System.out.println(a+b);} void sum(double a,double b){System.out.println(a+b);} public static void main(String args[]){ Calculation2 obj=new Calculation2(); obj.sum(10.5,10.5); obj.sum(20,20); } } Output:21.0 40 18

  19. overload method return type method class Calculation3{ int sum(int a,int b){System.out.println(a+b);} double sum(int a,int b){System.out.println(a+b);} public static void main(String args[]){ Calculation3 obj=new Calculation3(); int result=obj.sum(20,20); //Compile Time Error } } int result=obj.sum(20,20); // method 19

  20. main methods overloading class Overloading1{ public static void main(int a){ System.out.println(a); } public static void main(String args[]){ System.out.println("main() method invoked"); main(10); } } 20

  21. Method Overloading and Type Promotion 21

  22. class OverloadingCalculation1{ void sum(int a,long b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} public static void main(String args[]){ OverloadingCalculation1 obj=new OverloadingCalculation1(); obj.sum(20,20); // parameter int long obj.sum(20,20,20); } } Output:40 60 22

  23. Type Promotion class OverloadingCalculation3{ void sum(int a,long b){System.out.println("a method invoked");} void sum(long a,int b){System.out.println("b method invoked");} public static void main(String args[]){ OverloadingCalculation3 obj=new OverloadingCalculation3(); obj.sum(20,20); // } } Output:Compile Time Error 23

  24. (Constructor) method object. object object 2 Default Constructor ( parameter) Parameterized Constructor ( parameter) 24

  25. constructor return type 25

  26. Default Constructor Syntax of default constructor: <class_name>(){ } class Bike1{ Bike1(){System.out.println("Bike is created");} public static void main(String args[]){ Bike1 b=new Bike1(); } } Output: Bike is created 26

  27. Default Constructor constructor class default constructor . 27

  28. default constructor class Student3{ int id; String name; void display(){System.out.println(id+" "+name); public static void main(String args[]){ Student3 s1=new Student3(); Student3 s2=new Student3(); s1.display(); s2.display(); } } Output: 0 null 0 null 28

  29. parameterized constructor object class Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } void display(){System.out.println(id+" "+name);} 29

  30. public static void main(String args[]){ Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); s1.display(); s2.display(); } } Output: 111 Karan 222 Aryan 30

  31. Constructor Overloading class constructor parameter parameters parameters class Student5{ int id; String name; int age; Student5(int i,String n){ id = i; name = n; } 31

  32. Student5(int i,String n,int a){ id = i; name = n; age=a; } void display(){System.out.println(id+" "+name+" "+age);} public static void main(String args[]){ Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } Output: 111 Karan 0 222 Aryan 25 } 32

  33. constructor and method constructor object object return type Method return type compiler 33

  34. Object constructor class Student6{ int id; String name; Student6(int i,String n){ id = i; name = n; } Student6(Student6 s){ id = s.id; name =s.name; } 34

  35. void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student6 s1 = new Student6(111,"Karan"); Student6 s2 = new Student6(s1); s1.display(); s2.display(); } } Output: 111 Karan 111 Karan 35

  36. Object constructor class Student7{ int id; String name; Student7(int i,String n){ id = i; name = n; } Student7(){} void display(){System.out.println(id+" "+name); } 36

  37. public static void main(String args[]){ Student7 s1 = new Student7(111,"Karan"); Student7 s2 = new Student7(); s2.id=s1.id; s2.name=s1.name; s1.display(); s2.display(); } } Output: 111 Karan 111 Karan 37

  38. static keyword 2 static static keyword static variables, methods, blocks nested class. static class object class . 38

  39. static variable //Program of static variable class Student8{ int rollno; String name; static String college ="ITS"; Student8(int r,String n){ rollno = r; name = n; } void display (){System.out.println(rollno+" "+name+" "+college);} 39

  40. public static void main(String args[]){ Student8 s1 = new Student8(111,"Karan"); Student8 s2 = new Student8(222,"Aryan"); s1.display(); s2.display(); } } Output:111 Karan ITS 222 Aryan ITS 40

  41. static variable 41

  42. Program of counter without static variable public static void main(String ar gs[]){ class Counter{ int count=0;//will get memory when instance is created Counter c1=new Counter(); Counter c2=new Counter(); Counter c3=new Counter(); Counter(){ count++; System.out.println(count); } } } Output:1 1 1 42

  43. Program of counter by static variable public static void main(String ar gs[]){ class Counter2{ static int count=0;//will get me mory only once and retain it s value Counter2 c1=new Counter2(); Counter2 c2=new Counter2(); Counter2 c3=new Counter2(); Counter2(){ count++; System.out.println(count); } } } Output:1 2 43

  44. static method class object class class object static method (read/write) static data . 44

  45. class Student9{ int rollno; String name; static String college = "ITS"; static void change(){ college = "BBDIT"; } Student9(int r, String n){ rollno = r; name = n; } void display (){System.out.println(rollno+" "+name+" "+college);} 45

  46. public static void main(String args[]){ Student9.change(); Student9 s1 = new Student9 (111,"Karan"); Student9 s2 = new Student9 (222,"Aryan"); Student9 s3 = new Student9 (333,"Sonoo"); s1.display(); s2.display(); s3.display(); } } Output:111 Karan BBDIT 222 Aryan BBDIT 333 Sonoo BBDIT 46

  47. //Program to get cube of a given number by static method class Calculate{ static int cube(int x){ return x*x*x; } public static void main(String args[]){ int result=Calculate.cube(5); System.out.println(result); } } Output:125 47

  48. static method static method non static data member non-static method object . this super static class A{ int a=40;//non static public static void main(String args[]){ System.out.println(a); } } Output:Compile Time Error 48

  49. static block static data member. main method class A2{ static{System.out.println("static block is invoked");} public static void main(String args[]){ System.out.println("Hello main"); } } Output: static block is invoked Hello main 49

  50. this keyword 1. this instance variable class . 2. this() constructor class 3. this method class 4. this argument parameter method 5. this argument parameter constructor 6. this return object class 50

More Related Content