└── 10th June java - psa - notes.txt /10th June java - psa - notes.txt: -------------------------------------------------------------------------------- 1 | ############################################### 2 | Core Java Notes - 17th June 2025 - 10th June 3 | ############################################### 4 | 5 | What is a class? 6 | ------------------------ 7 | -> Class helps us to create object in java 8 | -> In java we use class keyword to create class. 9 | -> Keyword should be in lower case 10 | Example : 11 | 12 | class A { 13 | 14 | } 15 | -> We define the boundary of class using flower brackets 16 | 17 | Class Naming Convention 18 | ####################### 19 | -> Class name should always start with upper case 20 | -> If the class has more than one word then use camel casing, that is 21 | first letter of every word should be in upper case 22 | 23 | 24 | Example: Correct 25 | ------- 26 | class Bank { 27 | 28 | } 29 | Example: Correct 30 | ------- 31 | class BankAccountNumber { 32 | 33 | } 34 | Example: InCorrect 35 | ------- 36 | class Bank Account Number { 37 | 38 | } 39 | 40 | Example: 41 | ---------- 42 | -> Snake Casing-->Should not use for java 43 | -> Will Execute without any error 44 | 45 | public class Bank_Account_Number { 46 | 47 | } 48 | 49 | Example: Class name cannot start with a number 50 | ---------------------------------------------- 51 | -> Will give error 52 | public class 1Bank { 53 | public static void main(String[] args){ 54 | 55 | } 56 | } 57 | 58 | Example:Class name can start with a character follwed by Number 59 | --------- 60 | public class Bank1 { 61 | public static void main(String[] args){ 62 | 63 | } 64 | } 65 | 66 | Note: 67 | 1. Donot use specials characters like #,@ % & etc while creating class 68 | 2. $Class name will execute. But never create a class with special character $ 69 | 70 | 71 | Example:Correct, but not recommended 72 | ___________ 73 | public class $A { 74 | public static void main(String[] args) { 75 | 76 | } 77 | } 78 | 79 | Example: For Creating objects in java 80 | --------------------------------------- 81 | 82 | public class A { 83 | public static void main(String[] args){ 84 | A a1 = new A(); 85 | A a2 = new A(); 86 | System.out.println(a1); 87 | System.out.println(a2); 88 | } 89 | } 90 | 91 | new keyword: 92 | ############## 93 | 94 | -> Using new keyword we send request to class to create object 95 | -> Once object is created new keyword will get objects's address and stores that in a reference variable 96 | 97 | Syntax to create object: 98 | ------------------------- 99 | ClassName variableName = new ClassName(); 100 | 101 | 102 | Non-static Variable/ Instance Variable /Object Variables 103 | ############################################################ 104 | 1. We should create non static variable inside class outside method without using static keyword 105 | 2. Without creating object we cannot access non static variable 106 | 107 | 108 | Example 1: 109 | ----------- 110 | package p1; 111 | public class A { 112 | int x = 10; 113 | public static void main(String[] args) { 114 | A a1 = new A(); 115 | System.out.println(a1.x); 116 | } 117 | } 118 | Ouput: 10 119 | 120 | 121 | Example 2: 122 | ---------- 123 | 124 | public class A { 125 | int x = 10; 126 | int y = 20; 127 | public static void main(String[] args) { 128 | A a1 = new A(); 129 | System.out.println(a1.x); 130 | System.out.println(a1.y); 131 | } 132 | } 133 | Output: 134 | 10 135 | 20 136 | 137 | ----------------------------------------------------------------- 138 | 3. Every time we create object non-static variable copy is loaded to that object 139 | 4. Copy of variable in these object's are different. That is if you change the value of variable in one object, those changes cannot be seen in another object 140 | 141 | Example : 142 | ---------- 143 | package p1; 144 | public class A { 145 | int x = 10; 146 | public static void main(String[] args) { 147 | A a1 = new A(); 148 | System.out.println(a1.x);//10 149 | a1.x = 20; 150 | System.out.println(a1.x);//20 151 | A a2 = new A(); 152 | System.out.println(a2.x);//10 153 | } 154 | } 155 | Output: 156 | ------ 157 | 10 158 | 20 159 | 10 160 | 161 | 5. It is not mandatory to initialize non-static variable. Depending on data type automatically default value will get stored in it. 162 | 163 | Example: 164 | --------- 165 | public class A { 166 | int x ; 167 | public static void main(String[] args) { 168 | A a1 = new A(); 169 | System.out.println(a1.x); 170 | 171 | } 172 | } 173 | 174 | Ouput: 0 175 | 176 | Static variables: 177 | ################# 178 | 179 | 1. We should create static variables inside class but outside method with static keyword 180 | 2. To access static variable we will use below Options 181 | a. ClassName.variableName 182 | b. variableName (Contraints) 183 | c. objectAddress.variableName (Wrong Appraoch but will work) 184 | 3. It is not mandatory to initialize static variable. Depending on data type automatically default value will get stored in it. 185 | 186 | Example 1: 187 | ---------- 188 | package p1; 189 | public class A { 190 | static int x=10; 191 | public static void main(String[] args) { 192 | System.out.println(A.x); 193 | System.out.println(x); 194 | 195 | A a1 = new A(); 196 | System.out.println(a1.x);//A.x 197 | } 198 | 199 | } 200 | Output: 201 | 10 202 | 10 203 | 10 204 | 205 | Example 2: 206 | ----------- 207 | package p1; 208 | public class A { 209 | static int x=10; 210 | public static void main(String[] args) { 211 | System.out.println(A.x);//10 212 | A.x = 100; 213 | System.out.println(A.x); 214 | System.out.println(A.x); 215 | } 216 | 217 | } 218 | Output: 219 | 10 220 | 100 221 | 100 222 | 223 | Example 3: 224 | ---------- 225 | package p1; 226 | public class A { 227 | static int x; 228 | public static void main(String[] args) { 229 | System.out.println(A.x);//0 230 | } 231 | 232 | } 233 | Output: 234 | 0 235 | 236 | Note: 237 | ------ 238 | Example 1: 239 | ----------- 240 | package p1; 241 | public class A { 242 | public static void main(String[] args) { 243 | A a1 = new A(); 244 | a1.test(); 245 | } 246 | public void test() { 247 | System.out.println(100); 248 | } 249 | 250 | } 251 | Output: 252 | 100 253 | 254 | Example 2: 255 | --------- 256 | package p1; 257 | public class A { 258 | public static void main(String[] args) { 259 | A.test(); 260 | } 261 | public static void test() { 262 | System.out.println(100); 263 | } 264 | 265 | } 266 | Output: 267 | 100 268 | 269 | Types of variables in java 270 | ############################ 271 | 272 | 1. Local Variable 273 | ###################### 274 | a. Local vaiables should be created inside methods 275 | b. We should use local variable within the created method only 276 | c. Without initializing if you are using local variable then you will get an error 277 | 278 | Example 1: 279 | ------------ 280 | package p1; 281 | public class A { 282 | 283 | public static void main(String[] args) { 284 | int x = 10; 285 | System.out.println(x); 286 | } 287 | } 288 | Output: 289 | 10 290 | 291 | Example 2: 292 | ----------- 293 | package p1; 294 | public class A { 295 | 296 | public static void main(String[] args) { 297 | int x = 10;//Created 298 | System.out.println(x);//Using, Correct 299 | } 300 | public void test() { 301 | System.out.println(x);//Error 302 | } 303 | } 304 | 305 | Example 3: 306 | ---------- 307 | package p1; 308 | public class A { 309 | 310 | public static void main(String[] args) { 311 | A a1 = new A(); 312 | a1.test(); 313 | } 314 | public void test() { 315 | int x = 10;//Local, Created 316 | System.out.println(x);//Using, Correct 317 | } 318 | } 319 | Output: 320 | 10 321 | 322 | Example 4: 323 | ---------- 324 | package p1; 325 | public class A { 326 | 327 | public static void main(String[] args) { 328 | A a1 = new A(); 329 | a1.test(); 330 | System.out.println(x);//Error 331 | } 332 | public void test() { 333 | int x = 10;//Local, Created 334 | System.out.println(x);//Using, Correct 335 | } 336 | } 337 | 338 | Example 5: 339 | ---------- 340 | package p1; 341 | public class A { 342 | 343 | public static void main(String[] args) { 344 | int x;//Not Initialized 345 | System.out.println(x);//Error 346 | } 347 | } 348 | 349 | 2. Static variable: 350 | These variables has global access 351 | 352 | Example 1: 353 | --------- 354 | package p1; 355 | public class A { 356 | static int x = 10; 357 | public static void main(String[] args) { 358 | System.out.println(A.x); 359 | A a1 = new A(); 360 | a1.test(); 361 | } 362 | public void test() {//Non static 363 | System.out.println(A.x); 364 | } 365 | } 366 | Output: 367 | 10 368 | 10 369 | 370 | 3. Non-static variable / instance variable 371 | 372 | 4. Reference Variable 373 | ##################### 374 | a. Can hold either Object address or null value 375 | 376 | Example 1: 377 | ---------- 378 | package p1; 379 | public class A { 380 | public static void main(String[] args) { 381 | A a1 = null; //Object is not created, but reference variable is created 382 | A a2 = new A();//Storing Object address inside a2 383 | 384 | System.out.println(a1); 385 | System.out.println(a2); 386 | } 387 | } 388 | Ouput: 389 | null 390 | p1.A@4517d9a3 391 | 392 | b. Datatype of reference variable is class name 393 | Example 2: A x; 394 | 395 | Example 3: 396 | In the below example "a1" is created inside main and hence it is a local variable. We cannot access that outside main method 397 | 398 | package p1; 399 | public class A { 400 | public static void main(String[] args) { 401 | A a1 = new A();//local, created 402 | System.out.println(a1);//Using, Correct 403 | } 404 | public void test() { 405 | System.out.println(a1);//Error 406 | } 407 | } 408 | 409 | 410 | Example 4: 411 | Error because "a1" is local variable and not initialized 412 | 413 | public class A { 414 | 415 | public static void main(String[] args) { 416 | A a1; 417 | System.out.println(a1);//Error 418 | } 419 | } 420 | Example 5: If we donot initialize static reference variable then by default null value will get stored in it 421 | 422 | Example: 423 | public class A { 424 | static A a1; 425 | public static void main(String[] args) { 426 | System.out.println(a1); 427 | } 428 | 429 | 430 | } 431 | 432 | Example: 433 | public class A { 434 | static A a1; 435 | public static void main(String[] args) { 436 | System.out.println(a1);//null 437 | a1 = new A();//Creating Object 438 | a1.test();//Calling test 439 | } 440 | public void test() { 441 | System.out.println(a1);//Pring object address 442 | } 443 | 444 | 445 | } 446 | 447 | Note: 448 | Stack - > LIFO (Last In First Out) 449 | Heap - > Objects are created inside heap memory 450 | Garbage Collector - > Manages Memory to avoid overflow of memory by constantly removing objects that is not is use from heap memory 451 | 452 | Heap and stack memory 453 | ##################### 454 | 455 | ############### 456 | methods in java 457 | ############### 458 | 1. void keyword: A void method cannot return any value 459 | 460 | Example 1: 461 | ---------- 462 | public class C { 463 | public static void main(String[] args) { 464 | 465 | } 466 | 467 | public void test() { 468 | return 100;//Error 469 | } 470 | } 471 | 472 | Example 2: 473 | ----------- 474 | public class C { 475 | public static void main(String[] args) { 476 | C c1 = new C(); 477 | int x = c1.test(); 478 | System.out.println(x); 479 | } 480 | 481 | public int test() { 482 | return 100; 483 | } 484 | } 485 | Output: 100 486 | 487 | Example: 488 | ---------- 489 | public class C { 490 | public static void main(String[] args) { 491 | C c1 = new C(); 492 | String x = c1.test(); 493 | System.out.println(x); 494 | } 495 | 496 | public String test() { 497 | return "mike"; 498 | } 499 | } 500 | Output: 501 | mike 502 | 503 | return keyword versus return "value" 504 | ##################################### 505 | return keyword: 506 | -> A method has to be void 507 | -> It is optional 508 | -> it will return control to method calling statement 509 | 510 | Example: 511 | ------- 512 | 513 | public class C { 514 | public static void main(String[] args) { 515 | C c1 = new C(); 516 | c1.test(); 517 | } 518 | 519 | public void test() { 520 | System.out.println(100); 521 | return; 522 | } 523 | } 524 | Output: 100 525 | 526 | Note: if we write code after return keyword, then that code will 100% not run. This error is called as unreachable code error 527 | 528 | Example 529 | ------- 530 | public class A { 531 | public static void main(String[] args) { 532 | A a1 = new A(); 533 | a1.test(); 534 | } 535 | public void test() { 536 | System.out.println(100); 537 | return; 538 | System.out.println(200);//Unreachable code error 539 | } 540 | } 541 | 542 | return "value" 543 | --------------- 544 | -> The method has to be not a void method 545 | -> It is mandatory to use return "value" statement inside not a void method 546 | -> It will return control and value to method calling statement 547 | 548 | Example 549 | ------- 550 | 551 | Example: 552 | public class C { 553 | public static void main(String[] args) { 554 | 555 | } 556 | 557 | public int test() {//Error 558 | 559 | } 560 | } 561 | Output: Error because return "value" statement is missing inside test method 562 | 563 | Example: 564 | public class C { 565 | public static void main(String[] args) { 566 | 567 | } 568 | 569 | public int test() { 570 | return 100; 571 | System.out.println(300); 572 | } 573 | } 574 | Output: Unreachable code error 575 | 576 | Method Arguments 577 | ################## 578 | -> Using method arguments we supply values to method when we are calling it 579 | -> The method argument is a local variable 580 | -> arguments inside the method values that you supply to the method should match 581 | 582 | 583 | Example 1: 584 | ------- 585 | public class C { 586 | public static void main(String[] args) { 587 | C c1 = new C(); 588 | c1.test(100); 589 | } 590 | 591 | public void test(int x) { 592 | System.out.println(x); 593 | } 594 | } 595 | Output: 596 | 100 597 | 598 | Example 2: 599 | -------- 600 | public class C { 601 | public static void main(String[] args) { 602 | C c1 = new C(); 603 | c1.test(100,"mike"); 604 | } 605 | 606 | public void test(int x,String y) { 607 | System.out.println(x); 608 | } 609 | } 610 | 611 | Note: When method argument type is Object, then we can supply any kind of value to it 612 | 613 | Example 3: 614 | -------- 615 | public class C { 616 | public static void main(String[] args) { 617 | C c1 = new C(); 618 | c1.test('a'); 619 | } 620 | 621 | public void test(Object x) { 622 | System.out.println(x); 623 | } 624 | } 625 | 626 | Example 4: 627 | ---------- 628 | public class A { 629 | public static void main(String[] args) { 630 | A a1 = new A(); 631 | a1.test(1,2,3,4,5); 632 | } 633 | public void test(int ...x) {//Local variable 634 | System.out.println(x[0]); 635 | System.out.println(x[1]); 636 | System.out.println(x[2]); 637 | System.out.println(x[3]); 638 | System.out.println(x[4]); 639 | } 640 | } 641 | Output: 642 | 1 643 | 2 644 | 3 645 | 4 646 | 5 647 | 648 | Example 5: 649 | -------- 650 | public class A { 651 | public static void main(String[] args) { 652 | A a1 = new A(); 653 | a1.test("mike",1,2,3,4,5); 654 | } 655 | public void test(String y, int ...x) {//Local variable 656 | System.out.println(x[0]); 657 | System.out.println(x[1]); 658 | System.out.println(x[2]); 659 | System.out.println(x[3]); 660 | System.out.println(x[4]); 661 | System.out.println(y); 662 | } 663 | } 664 | Output: 665 | 1 666 | 2 667 | 3 668 | 4 669 | 5 670 | mike 671 | 672 | Static Methods 673 | ############### 674 | 675 | public class A { 676 | public static void main(String[] args) { 677 | A.test(); 678 | } 679 | public static void test() { 680 | System.out.println(100); 681 | } 682 | } 683 | Output: 684 | 100 685 | 686 | Example 2: 687 | ----------- 688 | 689 | public class A { 690 | public static void main(String[] args) { 691 | int x = A.test(); 692 | System.out.println(x); 693 | } 694 | public static int test() { 695 | return 100; 696 | } 697 | } 698 | Output: 699 | 100 700 | 701 | Example 3: 702 | ---------- 703 | public class A { 704 | public static void main(String[] args) { 705 | int x = A.test(100,200); 706 | System.out.println(x); 707 | } 708 | public static int test(int a, int b) { 709 | return a+b; 710 | } 711 | } 712 | Output: 713 | 300 714 | 715 | Example 4: 716 | ----------- 717 | public class A { 718 | public static void main(String[] args) { 719 | A.test(); 720 | A.test(); 721 | A.test(); 722 | } 723 | public static void test() { 724 | System.out.println(100); 725 | } 726 | } 727 | Output: 728 | 100 729 | 100 730 | 100 731 | 732 | Datatypes in java 733 | ################## 734 | Storing integer Values: 735 | ------------------------------------ 736 | 1. byte - 1 byte - default value: 0 737 | 2. short - 2 bytes - default value: 0 738 | 3. int - 4 bytes - default value: 0 739 | 4. long - 8 byte - default value: 0 740 | 741 | Floating Value 742 | ------------------------------- 743 | 1. float - 4 bytes - default value: 0.0 744 | 2. double - 8 bytes - default value: 0.0 745 | 746 | boolean(true/false) 747 | ---------------------------- 748 | 1. boolean - NA - default: false 749 | 750 | character values 751 | ----------------------- 752 | 1. char - 2 bytes - default: blank space 753 | 754 | String - Class 755 | --------------- 756 | String - NA - dynamic - default: null 757 | Note: 758 | byte: -128 to 127 (8-bit signed two's complement integer) 759 | short: -32,768 to 32,767 (16-bit signed two's complement integer) 760 | int: -2,147,483,648 to 2,147,483,647 (32-bit signed two's complement integer) 761 | long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64-bit signed two's complement integer) 762 | float: Approximately ±3.40282347E+38F (32-bit IEEE 754 single-precision floating-point) 763 | double: Approximately ±1.79769313486231570E+308 (64-bit IEEE 754 double-precision floating-point) 764 | char: 0 to 65,535 (16-bit unsigned Unicode character) 765 | boolean: true or false (size is not precisely defined, often considered 1 bit) 766 | 767 | Datatypes example: 768 | --------------------- 769 | public class C { 770 | static byte x1 ; 771 | static short x2 ; 772 | static int x3 = 23_567 ; 773 | static long x4=9632629033L ; 774 | static float x5=10.3F ; 775 | static double x6 ; 776 | static char x7 ; 777 | static boolean x8 ; 778 | static String x9; 779 | 780 | public static void main(String[] args) { 781 | System.out.println(x1); 782 | System.out.println(x2); 783 | System.out.println(x3); 784 | System.out.println(x4); 785 | System.out.println(x5); 786 | System.out.println(x6); 787 | System.out.println(x7); 788 | System.out.println(x8); 789 | System.out.println(x9); 790 | } 791 | 792 | } 793 | 794 | New type introduced in java version 10 795 | --------------------------------------- 796 | var type 797 | ######### 798 | -> This was introduced in java version 10 799 | -> It gives dynamic datatype concept. Depending on the value stored inside variable, dataype is allocated to it internally 800 | 801 | Example 1: 802 | ---------- 803 | public class A { 804 | 805 | public static void main(String[] args) { 806 | var x1 = 100; 807 | var x2 = 10.3F; 808 | var x3 = "mike"; 809 | var x4 = false; 810 | var x5 = new A(); 811 | var x6 = 'a'; 812 | 813 | System.out.println(x1); 814 | System.out.println(x2); 815 | System.out.println(x3); 816 | System.out.println(x4); 817 | System.out.println(x5); 818 | System.out.println(x6); 819 | } 820 | } 821 | Output: 822 | 100 823 | 10.3 824 | mike 825 | false 826 | p1.A@79fc0f2f 827 | a 828 | 829 | -> var type can only be local variable 830 | -> It cannot be static / non-static variable 831 | -> It cannot be method argument 832 | -> It cannot be method return type 833 | 834 | Example: 835 | -------- 836 | public class C { 837 | var x1;//Error 838 | static var x2;//Error 839 | public static void main(String[] args) { 840 | var x5 = 100;//Correct 841 | } 842 | public var test(var x3) {//Error 843 | 844 | } 845 | 846 | } 847 | 848 | ###################### 849 | Constructors in java 850 | ###################### 851 | 852 | -> Should have same name as that of class 853 | -> It is always void by default 854 | 855 | Example 1: 856 | ---------- 857 | public class A { 858 | A(){ 859 | return 100;//--->Error because constructor is always void 860 | } 861 | public static void main(String[] args) { 862 | 863 | } 864 | } 865 | Example 2: 866 | ---------- 867 | -> To call a constructor we have to create object. 868 | -> Whenever we create object constructor is being called 869 | -> Only after constructor calling object will get initialized. Initialization of object means loading of non static members to it 870 | -> Note: Constructors are never loaded to the object 871 | public class A { 872 | A(){ 873 | System.out.println(100); 874 | } 875 | public static void main(String[] args) { 876 | A a1 = new A(); 877 | A a2 = new A(); 878 | A a3 = new A(); 879 | } 880 | } 881 | Output: 882 | 100 883 | 100 884 | 100 885 | 886 | Example 1:Example for default constructor 887 | public class A {//--->Correct 888 | public static void main(String[] args) { 889 | A a1 = new A(); 890 | } 891 | 892 | } 893 | Default Constructors in java 894 | Note: When we run the above progam the following things will happen 895 | Step 1: When program is compliled, It will generate byte code that is stored inside a A.class file 896 | Step 2: As the program does not have constructor mentioned, then during compilation automatically "empty body no args constructor" will be added in .class file and hence object initialization will take place. This concept is called as default constructor 897 | Note: 898 | 1. Default constructors are not applicable when object with values are created 899 | 2. Default constructors are not applicable when object with and without values are created together 900 | 901 | Example 2: 902 | 903 | public class A { 904 | 905 | public static void main(String[] args) { 906 | A a1 = new A(100);//Default constructor not applicable, hence error 907 | } 908 | 909 | } 910 | 911 | public class A { 912 | A(int x){ 913 | } 914 | public static void main(String[] args) { 915 | A a1 = new A();//Default constructor not applicable, hence error 916 | A a1 = new A(100); 917 | } 918 | 919 | } 920 | 921 | -> If you mention return type for a constructor. Then it is treated as a method 922 | 923 | Example 4: 924 | ---------- 925 | public class C { 926 | void C(){ 927 | System.out.println(100); 928 | } 929 | public static void main(String[] args) { 930 | C c1 = new C(); 931 | } 932 | 933 | } 934 | 935 | Ouput: No Output, because void C() is a method 936 | 937 | Example 5: 938 | Note : 939 | -> Method name can be same as class name 940 | -> Variable name can be same as class name 941 | 942 | public class A { 943 | int A=1000; 944 | void A(){//-->Non static Method 945 | System.out.println(100); 946 | } 947 | public static void main(String[] args) { 948 | A a1 = new A(); 949 | a1.A(); 950 | System.out.println(a1.A); 951 | } 952 | } 953 | 954 | Output: 955 | 100 956 | 1000 957 | 958 | Contructor Overloading: 959 | ------------------------- 960 | Here we create more than one constructor in same class provided they have different number of arguments or different type of arguments 961 | 962 | Example 1: 963 | ----------- 964 | public class A { 965 | A(){//args=0 966 | System.out.println("A"); 967 | } 968 | A(int x){//args=1 969 | System.out.println(x); 970 | } 971 | A(int x,int y){//args=2 972 | System.out.println(x); 973 | System.out.println(y); 974 | } 975 | public static void main(String[] args) { 976 | A a1 = new A(); 977 | A a2 = new A(100); 978 | A a3 = new A(200,300); 979 | } 980 | 981 | } 982 | Output: 983 | A 984 | 100 985 | 200 986 | 300 987 | 988 | Example 2: 989 | ---------- 990 | public class A { 991 | A(int x){//args=1, type=int 992 | System.out.println(x); 993 | } 994 | A(float x){//args=1, type=float 995 | System.out.println(x); 996 | } 997 | 998 | public static void main(String[] args) { 999 | A a1 = new A(100); 1000 | A a2 = new A(10.3f); 1001 | } 1002 | 1003 | } 1004 | Output: 1005 | 100 1006 | 10.3 1007 | 1008 | Example: 1009 | --------- 1010 | public class A { 1011 | A(int x){//noOfArgs=1, type=int 1012 | System.out.println(x); 1013 | } 1014 | A(String x, int y){//noOfArgs=2, type=String, int 1015 | System.out.println(x); 1016 | System.out.println(y); 1017 | } 1018 | public static void main(String[] args) { 1019 | A a1 = new A("mike",100); 1020 | A a2 = new A(500); 1021 | 1022 | } 1023 | } 1024 | 1025 | Output: 1026 | ------- 1027 | mike 1028 | 100 1029 | 500 1030 | 1031 | ############ 1032 | this keyword 1033 | ############# 1034 | -> this keyword is a special reference variable that hold's object's address 1035 | 1036 | Example 1: 1037 | --------- 1038 | public class A { 1039 | 1040 | public static void main(String[] args) { 1041 | A a1 = new A(); 1042 | System.out.println(a1); 1043 | a1.test(); 1044 | } 1045 | public void test() { 1046 | System.out.println(this); 1047 | } 1048 | } 1049 | Output: 1050 | p1.A@79fc0f2f 1051 | p1.A@79fc0f2f 1052 | 1053 | -> Using this keyword we can access non-static members of the class 1054 | Example 2: 1055 | -------- 1056 | 1057 | public class A { 1058 | int x = 10; 1059 | public static void main(String[] args) { 1060 | A a1 = new A(); 1061 | System.out.println(a1.x); 1062 | a1.test(); 1063 | } 1064 | public void test() { 1065 | System.out.println(this.x); 1066 | } 1067 | } 1068 | Output: 1069 | 10 1070 | 10 1071 | 1072 | Example 3: 1073 | ---------- 1074 | public class A { 1075 | public static void main(String[] args) { 1076 | A a1 = new A(); 1077 | a1.test1(); 1078 | } 1079 | public void test1() { 1080 | this.test2(); 1081 | } 1082 | public void test2() { 1083 | System.out.println(100); 1084 | } 1085 | } 1086 | Output: 1087 | 100 1088 | 1089 | -> We cannot use this keyword inside "static method" 1090 | 1091 | Example 4: 1092 | ----------- 1093 | 1094 | public class A { 1095 | public static void main(String[] args) { 1096 | System.out.println(this);//-->Error, cannot be used inside static method 1097 | } 1098 | public static void test() { 1099 | System.out.println(this);//-->Error, cannot be used inside static method 1100 | } 1101 | 1102 | } 1103 | 1104 | Example 5: 1105 | --------------- 1106 | public class A { 1107 | int x = 10; 1108 | int y = 10; 1109 | public static void main(String[] args) { 1110 | A a1 = new A(); 1111 | a1.test(); 1112 | 1113 | } 1114 | public void test() { 1115 | System.out.println(this.x); 1116 | System.out.println(this.y); 1117 | } 1118 | 1119 | } 1120 | Output 1121 | 10 1122 | 10 1123 | 1124 | -> this keyword hold's current object address that is executing 1125 | 1126 | Example: 1127 | ------- 1128 | public class A { 1129 | public static void main(String[] args) { 1130 | A a1 = new A(); 1131 | a1.test(); 1132 | 1133 | A a2 = new A(); 1134 | a2.test(); 1135 | a1.test(); 1136 | 1137 | } 1138 | public void test() { 1139 | System.out.println(this); 1140 | } 1141 | 1142 | } 1143 | Output: 1144 | p1.A@79fc0f2f 1145 | p1.A@50040f0c 1146 | p1.A@79fc0f2f 1147 | 1148 | this()- This syntax is used to call constructor 1149 | -------------------------------------------- 1150 | -> Using this() keyword we can call a constructor 1151 | 1152 | 1153 | Example: 1154 | ------- 1155 | public class A { 1156 | A(){ 1157 | System.out.println("A"); 1158 | } 1159 | A(int x){ 1160 | this(); 1161 | } 1162 | public static void main(String[] args) { 1163 | A a1 = new A(100); 1164 | } 1165 | 1166 | } 1167 | Output: 1168 | A 1169 | 1170 | -> While calling a constructor using this() keyword, ensure calling is done from another constructor only 1171 | 1172 | Example: 1173 | -------- 1174 | public class A { 1175 | A(){ 1176 | System.out.println("A"); 1177 | } 1178 | A(int x){ 1179 | this(); 1180 | } 1181 | public static void main(String[] args) { 1182 | A a1 = new A(100); 1183 | } 1184 | public void test() { 1185 | this();//Error, we cannot call constructor from method 1186 | } 1187 | 1188 | } 1189 | 1190 | Example: 1191 | --------- 1192 | public class A { 1193 | A(int x){ 1194 | System.out.println(x); 1195 | } 1196 | A(){ 1197 | this(100); 1198 | } 1199 | public static void main(String[] args) { 1200 | A a1 = new A(); 1201 | } 1202 | 1203 | 1204 | } 1205 | Output 1206 | 100 1207 | 1208 | -> While calling a constructor using this() keyword ensure it is always first statement inside another constructor 1209 | 1210 | Example: 1211 | --------- 1212 | public class A { 1213 | A(int x){ 1214 | System.out.println(x); 1215 | } 1216 | A(){ 1217 | System.out.println(200); 1218 | this(100);//Error because it cannot be second statement while calling constructor 1219 | 1220 | } 1221 | public static void main(String[] args) { 1222 | A a1 = new A(); 1223 | } 1224 | 1225 | 1226 | } 1227 | 1228 | Example: 1229 | --------- 1230 | public class A { 1231 | int x;//-->0 1232 | A(int x){////Step 4-->x=100 1233 | this.x=x;//Step 5 1234 | System.out.println(this.x);//Step 6===>100 1235 | } 1236 | A(){//Step 2 1237 | this(100);//Step 3 1238 | } 1239 | public static void main(String[] args) { 1240 | A a1 = new A();//Step 1 1241 | } 1242 | 1243 | 1244 | } 1245 | 1246 | Constructor Chaining 1247 | --------------------- 1248 | -> When we call one constructor from another constructor it will form chain like flow structure. This is called as constructor chaining 1249 | -> This can be achieved by using this() keyword or Creating Object in another constructor 1250 | 1251 | Example 1: 1252 | ---------- 1253 | public class A { 1254 | A(){ 1255 | System.out.println("A"); 1256 | } 1257 | A(int x){ 1258 | this(); 1259 | } 1260 | public static void main(String[] args) { 1261 | A a1 = new A(100); 1262 | } 1263 | } 1264 | 1265 | Example 2: 1266 | ----------- 1267 | public class A { 1268 | A(){ 1269 | System.out.println("A"); 1270 | } 1271 | A(int x){ 1272 | A a2 = new A(); 1273 | } 1274 | public static void main(String[] args) { 1275 | A a1 = new A(100); 1276 | } 1277 | } 1278 | 1279 | ############################### 1280 | Important Shortcuts in eclipse 1281 | ################################ 1282 | 1. Ctrl + Space 1283 | ⚡ Content Assist — suggests code completions. 1284 | 1285 | Example: 1286 | a. syso (do control+ space bar) 1287 | b. main (do control+ space bar) 1288 | 1289 | 2. Ctrl + 1 1290 | 💡 Quick fix — suggests solutions for errors or warnings. 1291 | 1292 | Example: String x = 100; (Press control + 1 to get solutions to fix problem) 1293 | 1294 | 3. Ctrl + O 1295 | 🧭 Quick outline — shows methods/fields of the current class. 1296 | 1297 | Example: 1298 | public class Animal { 1299 | 1300 | private String name; 1301 | private int age; 1302 | 1303 | public Animal(String name, int age) { 1304 | this.name = name; 1305 | this.age = age; 1306 | } 1307 | 1308 | public void eat() { 1309 | System.out.println(name + " is eating."); 1310 | } 1311 | 1312 | public void sleep() { 1313 | 1314 | System.out.println(name + " is sleeping."); 1315 | } 1316 | 1317 | private void breathe() { 1318 | System.out.println(name + " is breathing."); 1319 | } 1320 | } 1321 | 1322 | -> in the above file when pressed Control + O we will get soo all content of like like shown below: 1323 | 1324 | Animal 1325 | -> name 1326 | -> age 1327 | -> Animal(String name, int age) 1328 | -> eat() 1329 | -> sleep() etc..... 1330 | 1331 | 4. F3 1332 | 🔄 Go to the declaration of a variable, method, or class. 1333 | 1334 | Example: 1335 | public class Animal { 1336 | 1337 | private String name;// Will show this step, as variable is declared here 1338 | private int age; 1339 | 1340 | public Animal(String name, int age) { 1341 | this.name = name; // When placed cusor on this.name, and pressed f3 1342 | this.age = age; 1343 | } 1344 | 1345 | public void eat() { 1346 | System.out.println(name + " is eating."); 1347 | } 1348 | 1349 | public void sleep() { 1350 | 1351 | System.out.println(name + " is sleeping."); 1352 | } 1353 | 1354 | private void breathe() { 1355 | System.out.println(name + " is breathing."); 1356 | } 1357 | } 1358 | 1359 | 5. Ctrl + . 1360 | ➡️ Jump to the next error or warning in the file. 1361 | public class Dog { 1362 | 1363 | int x//This is your first error. When pressed control + . 1364 | 1365 | public static void main(String[] args) { 1366 | int y//It will take you to this error 1367 | } 1368 | 1369 | } 1370 | ########################## 1371 | Packages in java 1372 | ########################### 1373 | a. Packages are folders created in java to store programs in organized manner 1374 | b. This will make maintainance of the project easy 1375 | c. Naming Convention for packages 1376 | -> Package name cannot be keyword like - new , static, public , in etc... 1377 | -> Donot start package name with capital letters 1378 | -> Package name not to be given as java 1379 | 1380 | d. When you create a class inside package, we have to define package keyword in our program as shown below: 1381 | 1382 | Example: 1383 | package p1; 1384 | public class A { 1385 | 1386 | } 1387 | 1388 | e. Create 2 classes in same package and perform inheritance. Import is not required 1389 | Example: 1390 | package p1; 1391 | public class A { 1392 | 1393 | } 1394 | 1395 | package p1; 1396 | public class B extends A { 1397 | 1398 | } 1399 | f. Create 2 classes in different package and perform inheritance. "Import required" 1400 | 1401 | Example: 1402 | package p1; 1403 | public class A { 1404 | 1405 | } 1406 | 1407 | package p2; 1408 | import p1.A; 1409 | public class C extends A{ 1410 | 1411 | } 1412 | 1413 | Example : 1414 | package p1; 1415 | public class A { 1416 | 1417 | } 1418 | package p1; 1419 | public class B { 1420 | public static void main(String[] args) { 1421 | A a1 = new A(); 1422 | } 1423 | } 1424 | 1425 | Example: 1426 | package p1; 1427 | public class A { 1428 | 1429 | } 1430 | 1431 | package p1; 1432 | public class B { 1433 | 1434 | } 1435 | 1436 | package p2; 1437 | import p1.*; 1438 | public class C { 1439 | public static void main(String[] args) { 1440 | A a1 = new A(); 1441 | B b1 = new B(); 1442 | } 1443 | } 1444 | 1445 | Example: 1446 | package p1; 1447 | public class A { 1448 | 1449 | } 1450 | 1451 | package p2; 1452 | public class A { 1453 | 1454 | } 1455 | 1456 | package p3; 1457 | public class B { 1458 | public static void main(String[] args) { 1459 | p1.A a1 = new p1.A();//---> p2 1460 | p2.A a2 = new p2.A();//---> p1 1461 | } 1462 | } 1463 | 1464 | --------------------------------------- 1465 | Example: 1466 | 1467 | package p1; 1468 | public class A { 1469 | 1470 | } 1471 | 1472 | package p2; 1473 | public class A { 1474 | 1475 | } 1476 | 1477 | package p3; 1478 | import p2.A; 1479 | public class B { 1480 | public static void main(String[] args) { 1481 | p1.A a1 = new p1.A();//---> p2 1482 | A a2 = new A();//---> p1 1483 | } 1484 | } 1485 | 1486 | Example 1487 | -------- 1488 | package com.tcs.service; 1489 | 1490 | public class EmailService { 1491 | public void sendEmail() { 1492 | System.out.println("Sending Email...."); 1493 | } 1494 | } 1495 | package com.tcs.auth; 1496 | 1497 | import com.tcs.service.EmailService; 1498 | 1499 | public class MainClass { 1500 | public static void main(String[] args) { 1501 | EmailService es = new EmailService(); 1502 | es.sendEmail(); 1503 | } 1504 | } 1505 | Output: 1506 | Sending Email.... 1507 | 1508 | ####################################### 1509 | Object Oriented Programming - Pillars 1510 | ######################################## 1511 | 1512 | a. inheritance 1513 | b. polymorphism 1514 | c. encapsulation 1515 | d. abstraction 1516 | 1517 | 1518 | a. inheritance 1519 | ###################### 1520 | -> Here we inherit the members of parent class to child class so that we can re-use those members 1521 | Example 1: 1522 | --------- 1523 | package app_java_1; 1524 | public class Animal { 1525 | public void eat() { 1526 | System.out.println("Eating"); 1527 | } 1528 | public void sleep() { 1529 | System.out.println("sleeping"); 1530 | } 1531 | } 1532 | 1533 | package app_java_1; 1534 | public class Dog extends Animal{ 1535 | public static void main(String[] args) { 1536 | Dog d = new Dog(); 1537 | d.eat(); 1538 | d.sleep(); 1539 | } 1540 | } 1541 | Output: 1542 | ------ 1543 | Eating 1544 | sleeping 1545 | 1546 | Example 2: 1547 | package app_java_1; 1548 | public class Animal { 1549 | public void eat() { 1550 | System.out.println("Eating"); 1551 | } 1552 | public void sleep() { 1553 | System.out.println("sleeping"); 1554 | } 1555 | } 1556 | Animal.java 1557 | 1558 | package app_java_1; 1559 | public class Dog extends Animal{ 1560 | //eat(),sleep()--->Animal 1561 | } 1562 | Dog.java 1563 | 1564 | package app_java_1; 1565 | public class Cat extends Animal{ 1566 | //eat(),sleep()--->Animal 1567 | } 1568 | Cat.java 1569 | 1570 | package app_java_1; 1571 | public class Cow extends Animal{ 1572 | //eat(),sleep()--->Animal 1573 | } 1574 | Cow.java 1575 | 1576 | package app_java_1; 1577 | public class Root { 1578 | public static void main(String[] args) { 1579 | Dog d = new Dog(); 1580 | d.eat(); 1581 | d.sleep(); 1582 | System.out.println("_______"); 1583 | Cat cat = new Cat(); 1584 | cat.eat(); 1585 | cat.sleep(); 1586 | System.out.println("_______"); 1587 | Cow cow = new Cow(); 1588 | cow.eat(); 1589 | cow.sleep(); 1590 | } 1591 | } 1592 | Root.java 1593 | 1594 | Example 3: Multilevel inheritance 1595 | ----------------------------------- 1596 | 1597 | package app_java_1; 1598 | public class A { 1599 | public void test1() { 1600 | System.out.println(1); 1601 | } 1602 | } 1603 | 1604 | package app_java_1; 1605 | //test1, test2 1606 | public class B extends A{ 1607 | public void test2() { 1608 | System.out.println(2); 1609 | } 1610 | } 1611 | package app_java_1; 1612 | 1613 | public class C extends B{ 1614 | public static void main(String[] args) { 1615 | C c1 = new C(); 1616 | c1.test1(); 1617 | c1.test2(); 1618 | } 1619 | } 1620 | output: 1621 | 1 1622 | 2 1623 | 1624 | ################################################################################################ 1625 | What is mulitlpe inheritance? 1626 | Answer: When a child class has more than one parent class, it is called as mulitple inheritance 1627 | ################################################################################################# 1628 | 1629 | Note: Java classes does not support mulitple inheritance because of diamond problem 1630 | diamond problem: Suppose we inherit a method from A->B->D and Same method is inherited from A->C->D, then confusion from which parent class method is inherited to child class D. This is called as DIAMOND PROBLEM. Hence in java classes does not support multiple inheritance 1631 | 1632 | Note: We can do mulitple inheritance on interface. But will explain this during interfaces concept 1633 | 1634 | Example 4:Mulitple inheritance error 1635 | ------------------------------------------ 1636 | package app_java_1; 1637 | public class A { 1638 | 1639 | } 1640 | 1641 | package app_java_1; 1642 | public class B{ 1643 | 1644 | } 1645 | package app_java_1; 1646 | public class C extends A,B{//Error 1647 | 1648 | } 1649 | 1650 | Example 4: Inheritance between classes present inside different packages 1651 | ---------------------------------------- 1652 | package p1; 1653 | public class A { 1654 | public void test() { 1655 | System.out.println(1); 1656 | } 1657 | } 1658 | package p2; 1659 | import p1.A; 1660 | public class B extends A{ 1661 | public static void main(String[] args) { 1662 | B b1 = new B(); 1663 | b1.test(); 1664 | } 1665 | } 1666 | 1667 | Example on non sub class: When in your project we have two class and no inheritance is happening between them then it is called as non-sub class 1668 | -------------------------------------------------------------------------------------------------------- 1669 | 1670 | Example on non sub class: When in your project we have two class and no inheritance is happening between them then it is called as non-sub class 1671 | -------------------------------------------------------------------------------------------------------- 1672 | package p1; 1673 | public class A { 1674 | public void test() { 1675 | System.out.println(1); 1676 | } 1677 | } 1678 | package p2; 1679 | import p1.A; 1680 | public class B { 1681 | public static void main(String[] args) { 1682 | A a1 = new A(); 1683 | a1.test(); 1684 | } 1685 | } 1686 | ------------------------------------------------------------ 1687 | Output: 1 1688 | 1689 | ############################### 1690 | Access Specifiers in java 1691 | ############################### 1692 | -------------------------------------------------------------------- 1693 | Questions: 1694 | 1. What happens when a variable is private/default/protected/public 1695 | 2. What happens when a constructor is private/default/protected/public 1696 | 3. What happens when a class is private/default/protected/public 1697 | 4. What happens when a interface is private/default/protected/public 1698 | ---------------------------------------------------------------------- 1699 | 1700 | 1701 | -> They define the visibility of variables, methods, class & interface. 1702 | 1703 | ------------------------------------------------------------------------------ 1704 | a. private: When a variable / method is made private then we can access that in same class but not outside class 1705 | ---------------------------------------------------------------------------------- 1706 | 1707 | Example 1: 1708 | package p1; 1709 | public class A { 1710 | private int x=10; 1711 | private void test() { 1712 | System.out.println(1); 1713 | } 1714 | public static void main(String[] args) { 1715 | A a1 = new A(); 1716 | System.out.println(a1.x); 1717 | a1.test(); 1718 | } 1719 | } 1720 | Output: 1721 | 10 1722 | 1 1723 | 1724 | Example 2: 1725 | package p1; 1726 | public class A { 1727 | private int x=10; 1728 | private void test() { 1729 | System.out.println(1); 1730 | } 1731 | } 1732 | package p1; 1733 | public class B extends A{ 1734 | public static void main(String[] args) { 1735 | B b1 = new B(); 1736 | System.out.println(b1.x);//Error 1737 | b1.test();//Error 1738 | } 1739 | } 1740 | 1741 | Example 3: 1742 | package p1; 1743 | public class A { 1744 | private int x=10; 1745 | private void test() { 1746 | System.out.println(1); 1747 | } 1748 | 1749 | } 1750 | package p1; 1751 | 1752 | public class B { 1753 | public static void main(String[] args) { 1754 | A a1 = new A(); 1755 | System.out.println(a1.x);//Error 1756 | a1.test();//Error 1757 | } 1758 | } 1759 | 1760 | Example 4: 1761 | package p1; 1762 | public class A { 1763 | private int x=10; 1764 | private void test() { 1765 | System.out.println(1); 1766 | } 1767 | 1768 | } 1769 | package p2; 1770 | import p1.A; 1771 | public class C extends A { 1772 | public static void main(String[] args) { 1773 | C c1 = new C(); 1774 | System.out.println(c1.x);//-->Error 1775 | c1.test();//-->Error 1776 | } 1777 | } 1778 | Example 5: 1779 | package p1; 1780 | public class A { 1781 | private int x=10; 1782 | private void test() { 1783 | System.out.println(1); 1784 | } 1785 | 1786 | } 1787 | package p2; 1788 | import p1.A; 1789 | public class C { 1790 | public static void main(String[] args) { 1791 | A a1 = new A(); 1792 | System.out.println(a1.x);//-->Error 1793 | a1.test();//-->Error 1794 | } 1795 | } 1796 | 1797 | ------------------------------------------------------------------------ 1798 | b. default: A variable/method with default access specifier can work only inside same class or same package, but cannot be accessed in different package 1799 | Example 1: 1800 | ------------ 1801 | 1802 | package p1; 1803 | public class A { 1804 | int x=10; 1805 | void test() { 1806 | System.out.println(1); 1807 | } 1808 | public static void main(String[] args) { 1809 | A a1 = new A(); 1810 | System.out.println(a1.x); 1811 | a1.test(); 1812 | } 1813 | 1814 | } 1815 | Example 2: 1816 | --------------------------------------------- 1817 | package p1; 1818 | public class A { 1819 | int x=10; 1820 | void test() { 1821 | System.out.println(1); 1822 | } 1823 | 1824 | } 1825 | package p1; 1826 | public class B extends A{ 1827 | public static void main(String[] args) { 1828 | B b1 = new B(); 1829 | System.out.println(b1.x); 1830 | b1.test(); 1831 | } 1832 | } 1833 | Output: 1834 | 10 1835 | 1 1836 | 1837 | ----------------------------------------------------------------------- 1838 | Example 3: 1839 | ------------ 1840 | package p1; 1841 | public class A { 1842 | int x=10; 1843 | void test() { 1844 | System.out.println(1); 1845 | } 1846 | 1847 | } 1848 | package p1; 1849 | public class B { 1850 | public static void main(String[] args) { 1851 | A a1 = new A(); 1852 | System.out.println(a1.x); 1853 | a1.test(); 1854 | } 1855 | } 1856 | 1857 | ----------------------------------------------------------------- 1858 | Example 4: 1859 | ------------------------ 1860 | package p1; 1861 | public class A { 1862 | int x=10; 1863 | void test() { 1864 | System.out.println(1); 1865 | } 1866 | 1867 | } 1868 | package p2; 1869 | import p1.A; 1870 | public class C extends A{ 1871 | public static void main(String[] args) { 1872 | C c1 = new C(); 1873 | System.out.println(c1.x);//-->Error 1874 | c1.test();//-->Error 1875 | } 1876 | } 1877 | 1878 | -------------------------------------------------------- 1879 | Example 5: 1880 | ---------------- 1881 | package p1; 1882 | public class A { 1883 | int x=10; 1884 | void test() { 1885 | System.out.println(1); 1886 | } 1887 | 1888 | } 1889 | package p2; 1890 | import p1.A; 1891 | public class C { 1892 | public static void main(String[] args) { 1893 | A a1 = new A(); 1894 | System.out.println(a1.x);//--->Error 1895 | a1.test();//--->Error 1896 | } 1897 | } 1898 | 1899 | c. protected: We can access variables/methods in same class/same package and inside different package only when inheritance is done 1900 | 1901 | Example 1: 1902 | ------------- 1903 | package p1; 1904 | public class A { 1905 | protected int x=10; 1906 | protected void test() { 1907 | System.out.println(1); 1908 | } 1909 | public static void main(String[] args) { 1910 | A a1 = new A(); 1911 | System.out.println(a1.x); 1912 | a1.test(); 1913 | } 1914 | 1915 | } 1916 | Ouput: 1917 | 10 1918 | 1 1919 | 1920 | Example 2: 1921 | ------------------ 1922 | package p1; 1923 | public class A { 1924 | protected int x=10; 1925 | protected void test() { 1926 | System.out.println(1); 1927 | } 1928 | } 1929 | package p1; 1930 | public class B extends A{ 1931 | public static void main(String[] args) { 1932 | B b1 = new B(); 1933 | System.out.println(b1.x); 1934 | b1.test(); 1935 | } 1936 | } 1937 | Output: 1938 | 10 1939 | 1 1940 | 1941 | Example 3: 1942 | ---------- 1943 | package p1; 1944 | public class A { 1945 | protected int x=10; 1946 | protected void test() { 1947 | System.out.println(1); 1948 | } 1949 | } 1950 | package p1; 1951 | public class B { 1952 | public static void main(String[] args) { 1953 | A a1 = new A(); 1954 | System.out.println(a1.x); 1955 | a1.test(); 1956 | } 1957 | } 1958 | Ouput: 1959 | 10 1960 | 1 1961 | Example 4: 1962 | ----------- 1963 | package p1; 1964 | public class A { 1965 | protected int x=10; 1966 | protected void test() { 1967 | System.out.println(1); 1968 | } 1969 | } 1970 | package p2; 1971 | import p1.A; 1972 | public class C extends A{ 1973 | public static void main(String[] args) { 1974 | C c1 = new C(); 1975 | System.out.println(c1.x); 1976 | c1.test(); 1977 | } 1978 | } 1979 | Ouput: 1980 | 10 1981 | 1 1982 | Example 5: 1983 | -------------- 1984 | package p1; 1985 | public class A { 1986 | protected int x=10; 1987 | protected void test() { 1988 | System.out.println(1); 1989 | } 1990 | } 1991 | package p2; 1992 | import p1.A; 1993 | public class C { 1994 | public static void main(String[] args) { 1995 | A a1 = new A(); 1996 | System.out.println(a1.x);//----> Error 1997 | a1.test();//----> Error 1998 | } 1999 | } 2000 | ------------------------------------------------------ 2001 | d. public: When we make variable/method public then we can access that in sam class/same package/different package 2002 | 2003 | Example 1: 2004 | package p1; 2005 | public class A { 2006 | public int x=10; 2007 | public void test() { 2008 | System.out.println(1); 2009 | } 2010 | public static void main(String[] args) { 2011 | A a1 = new A(); 2012 | System.out.println(a1.x); 2013 | a1.test(); 2014 | } 2015 | } 2016 | Ouput: 2017 | 10 2018 | 1 2019 | 2020 | Example 2: 2021 | ----------- 2022 | package p1; 2023 | public class A { 2024 | public int x=10; 2025 | public void test() { 2026 | System.out.println(1); 2027 | } 2028 | 2029 | } 2030 | package p1; 2031 | public class B extends A{ 2032 | public static void main(String[] args) { 2033 | B b1 = new B(); 2034 | System.out.println(b1.x); 2035 | b1.test(); 2036 | } 2037 | } 2038 | Ouput: 2039 | 10 2040 | 1 2041 | Example 3: 2042 | ------------------------------------------- 2043 | 2044 | package p1; 2045 | public class A { 2046 | public int x=10; 2047 | public void test() { 2048 | System.out.println(1); 2049 | } 2050 | 2051 | } 2052 | package p1; 2053 | public class B { 2054 | public static void main(String[] args) { 2055 | A a1 = new A(); 2056 | System.out.println(a1.x); 2057 | a1.test(); 2058 | } 2059 | } 2060 | Output: 2061 | 10 2062 | 1 2063 | 2064 | Example 4: 2065 | -------------- 2066 | package p1; 2067 | public class A { 2068 | public int x=10; 2069 | public void test() { 2070 | System.out.println(1); 2071 | } 2072 | 2073 | } 2074 | package p2; 2075 | import p1.A; 2076 | public class C extends A { 2077 | public static void main(String[] args) { 2078 | C c1 = new C(); 2079 | System.out.println(c1.x); 2080 | c1.test(); 2081 | } 2082 | } 2083 | Output: 2084 | 10 2085 | 1 2086 | 2087 | Example 5: 2088 | ------------ 2089 | package p1; 2090 | public class A { 2091 | public int x=10; 2092 | public void test() { 2093 | System.out.println(1); 2094 | } 2095 | 2096 | } 2097 | package p2; 2098 | import p1.A; 2099 | public class C { 2100 | public static void main(String[] args) { 2101 | A a1 = new A(); 2102 | System.out.println(a1.x); 2103 | a1.test(); 2104 | } 2105 | } 2106 | Output: 2107 | 10 2108 | 1 2109 | ######################################################## 2110 | Access Specifier on Constructors 2111 | ####################################################### 2112 | 2113 | Can we Inherit Constructor? 2114 | Answer: We cannot inherit constructor 2115 | 2116 | a. private Constructor 2117 | ---------------------------- 2118 | -> If a constructor is made private then its object cannot be created outside the class 2119 | 2120 | Example 1: 2121 | package p1; 2122 | public class A { 2123 | private A() { 2124 | 2125 | } 2126 | public static void main(String[] args) { 2127 | A a1 = new A(); 2128 | } 2129 | 2130 | } 2131 | package p1; 2132 | public class B { 2133 | public static void main(String[] args) { 2134 | A a1 = new A();//--->Error 2135 | } 2136 | } 2137 | package p2; 2138 | import p1.A; 2139 | public class C { 2140 | public static void main(String[] args) { 2141 | A a1 = new A();//-->Error 2142 | } 2143 | } 2144 | 2145 | -> When a constructor is made private then inheritance of that class is not allowed 2146 | (Note: Construct are not inherit) 2147 | 2148 | Example 2: 2149 | ---------- 2150 | package p1; 2151 | public class A { 2152 | private A() { 2153 | 2154 | } 2155 | 2156 | } 2157 | package p1; 2158 | public class B extends A{//-->Error 2159 | 2160 | } 2161 | package p2; 2162 | import p1.A; 2163 | public class C extends A{//-->Error 2164 | 2165 | } 2166 | 2167 | b. default Constructor 2168 | ------------------------- 2169 | -> If a constructor is made default then its object can be created in same class/same package but not outside the package 2170 | 2171 | Example 1: 2172 | ---------- 2173 | package p1; 2174 | public class A { 2175 | A() { 2176 | 2177 | } 2178 | public static void main(String[] args) { 2179 | A a1 = new A(); 2180 | } 2181 | } 2182 | package p1; 2183 | public class B { 2184 | public static void main(String[] args) { 2185 | A a1 = new A(); 2186 | } 2187 | } 2188 | package p2; 2189 | import p1.A; 2190 | public class C{ 2191 | public static void main(String[] args) { 2192 | A a1 = new A();//-->Error 2193 | } 2194 | } 2195 | 2196 | -> When a constructor is made default then inheritance of that class is allowed only in same package but not outside the package 2197 | (Note: Construct are not inherit) 2198 | 2199 | Example 2: 2200 | ---------- 2201 | package p1; 2202 | public class A { 2203 | A() { 2204 | 2205 | } 2206 | 2207 | } 2208 | package p1; 2209 | public class B extends A { 2210 | 2211 | } 2212 | package p2; 2213 | import p1.A; 2214 | public class C extends A{//-->Error 2215 | 2216 | } 2217 | 2218 | e. protected Constructor 2219 | -------------------------- 2220 | -> If a constructor is made protected then its object can be created in same class/same package but not outside the package 2221 | 2222 | Example 1: 2223 | --------- 2224 | package p1; 2225 | public class A { 2226 | protected A() { 2227 | 2228 | } 2229 | public static void main(String[] args) { 2230 | A a1 = new A(); 2231 | } 2232 | } 2233 | package p1; 2234 | public class B { 2235 | public static void main(String[] args) { 2236 | A a1 = new A(); 2237 | } 2238 | } 2239 | package p2; 2240 | import p1.A; 2241 | public class C { 2242 | public static void main(String[] args) { 2243 | A a1 = new A();//-->Error 2244 | } 2245 | } 2246 | 2247 | -> When a constructor is made protected then inheritance of that class is allowed in same package / outside the package (both) 2248 | (Note: Construct are not inherit) 2249 | 2250 | Example 2: 2251 | ---------- 2252 | package p1; 2253 | public class A { 2254 | protected A() { 2255 | 2256 | } 2257 | 2258 | } 2259 | package p1; 2260 | public class B extends A { 2261 | 2262 | } 2263 | package p2; 2264 | import p1.A; 2265 | public class C extends A { 2266 | 2267 | } 2268 | 2269 | e. public Constructor 2270 | ------------------------ 2271 | -> If a constructor is made public then its object can be created in same class/same package and outside the package also 2272 | 2273 | Example 1: 2274 | --------- 2275 | package p1; 2276 | public class A { 2277 | public A() { 2278 | 2279 | } 2280 | public static void main(String[] args) { 2281 | A a1 = new A(); 2282 | } 2283 | } 2284 | package p1; 2285 | public class B { 2286 | public static void main(String[] args) { 2287 | A a1 = new A(); 2288 | } 2289 | } 2290 | package p2; 2291 | import p1.A; 2292 | public class C { 2293 | public static void main(String[] args) { 2294 | A a1 = new A(); 2295 | } 2296 | } 2297 | 2298 | -> When a constructor is made public then inheritance of that class is allowed in same package / outside the package (both) 2299 | (Note: Construct are not inherit) 2300 | 2301 | Example 2: 2302 | ---------- 2303 | package p1; 2304 | public class A { 2305 | public A() { 2306 | 2307 | } 2308 | 2309 | } 2310 | package p1; 2311 | public class B extends A { 2312 | 2313 | } 2314 | package p2; 2315 | import p1.A; 2316 | public class C extends A { 2317 | 2318 | } 2319 | 2320 | ######################################## 2321 | Access Specifier on Class 2322 | ######################################## 2323 | -> A class can be only public / default 2324 | -> A class cannot be private / protected 2325 | 2326 | What happens when a class is made default? 2327 | Answer: 2328 | -> It's object can be created in same class/same package but not inside different package 2329 | -> Inheritance is allowed in same package 2330 | 2331 | Example 1: 2332 | package p1; 2333 | class A { 2334 | public static void main(String[] args) { 2335 | A a1 = new A(); 2336 | } 2337 | } 2338 | package p1; 2339 | public class B { 2340 | public static void main(String[] args) { 2341 | A a1 = new A(); 2342 | } 2343 | } 2344 | package p2; 2345 | import p1.A;//--->Error 2346 | public class C { 2347 | public static void main(String[] args) { 2348 | A a1 = new A();//--->Error 2349 | } 2350 | } 2351 | 2352 | Example 2: 2353 | package p1; 2354 | class A { 2355 | 2356 | } 2357 | package p1; 2358 | public class B extends A{ 2359 | 2360 | } 2361 | package p2; 2362 | import p1.A;//-->Error 2363 | public class C extends A{//-->Error 2364 | 2365 | } 2366 | 2367 | --------------------------------------------------------------] 2368 | What happens when a class is made public? 2369 | Answer: 2370 | -> It's object can be created in same class/same package & inside different package 2371 | -> Inheritance is allowed in same package & inside different package 2372 | 2373 | Example 1: 2374 | ---------- 2375 | package p1; 2376 | public class A { 2377 | public static void main(String[] args) { 2378 | A a1 = new A(); 2379 | } 2380 | } 2381 | package p1; 2382 | public class B{ 2383 | public static void main(String[] args) { 2384 | A a1 = new A(); 2385 | } 2386 | } 2387 | package p2; 2388 | import p1.A; 2389 | public class C{ 2390 | public static void main(String[] args) { 2391 | A a1 = new A(); 2392 | } 2393 | } 2394 | 2395 | Example 2: 2396 | ----------------- 2397 | 2398 | package p1; 2399 | public class A { 2400 | 2401 | } 2402 | package p1; 2403 | public class B extends A{ 2404 | 2405 | } 2406 | package p2; 2407 | import p1.A; 2408 | public class C extends A{ 2409 | 2410 | } 2411 | 2412 | Note: A class cannot be made private 2413 | --------------------------------------- 2414 | Example : 2415 | package p1; 2416 | private class A {//-->Error 2417 | 2418 | } 2419 | 2420 | Note: A class cannot be made protected 2421 | --------------------------------------- 2422 | Example : 2423 | package p1; 2424 | protected class A {//-->Error 2425 | 2426 | } 2427 | 2428 | 2429 | 2430 | ######################################## 2431 | Polymorphism in java 2432 | ######################################### 2433 | 2434 | Here we develop a feature such that it can take more than one form depending on the situation 2435 | Note: Polymorphism is applicable only on methods and not variables 2436 | 2437 | 2438 | Two types of polymorphism 2439 | 2440 | a. Overriding (Run time polymorphism): 2441 | -> Here we modify the logic of inherited method in child class 2442 | 2443 | Example 1: 2444 | ----------- 2445 | package p1; 2446 | public class GoldAccount { 2447 | public void onlineBanking() { 2448 | System.out.println("yes"); 2449 | } 2450 | public void rateOfInterest() { 2451 | System.out.println("nill"); 2452 | } 2453 | } 2454 | GoldAccount.java 2455 | 2456 | package p1; 2457 | public class PlatinumAccount extends GoldAccount { 2458 | public void rateOfInterest() { 2459 | System.out.println("6% PA"); 2460 | } 2461 | public static void main(String[] args) { 2462 | PlatinumAccount p = new PlatinumAccount(); 2463 | p.onlineBanking(); 2464 | p.rateOfInterest();//-->6% PA 2465 | System.out.println("--------"); 2466 | GoldAccount g = new GoldAccount(); 2467 | g.onlineBanking(); 2468 | g.rateOfInterest();//-->nill 2469 | } 2470 | } 2471 | PlatinumAccount.java 2472 | 2473 | Output: 2474 | yes 2475 | 6% PA 2476 | -------- 2477 | yes 2478 | nill 2479 | 2480 | Example 2: 2481 | -> In version java 5 , A concept of annotations was introduced 2482 | -> Annotations can instruct compiler to perform a specific task 2483 | -> @AnnotationName 2484 | -> @Override: This annotation instructs compiler to check whether overriding is happening or not 2485 | 2486 | ################################### 2487 | Note: 2488 | a. Compile Time: Here our java compiler converts .java files to .class files(byte code) 2489 | b. Run Time: Here we run .class file(byte code using Java Runtime Environment-JRE). 2490 | ################################# 2491 | 2492 | package p1; 2493 | public class GoldAccount { 2494 | 2495 | public void rateOfInterest() { 2496 | System.out.println("nill"); 2497 | } 2498 | } 2499 | GoldAccount.java 2500 | 2501 | package p1; 2502 | public class PlatinumAccount extends GoldAccount { 2503 | @Override 2504 | public void rateOfInterests() {//-->Error because method name mismatch 2505 | System.out.println("6% PA"); 2506 | } 2507 | 2508 | } 2509 | PlatinumAccount.java 2510 | 2511 | Example 3: 2512 | --------- 2513 | Question: Access Specifier should it be same or can be different during Overriding? 2514 | Answer: During Overriding we can increase the scope of access specifier but we cannot reduce the scope of access specifier 2515 | package p1; 2516 | public class GoldAccount { 2517 | 2518 | void rateOfInterest() { 2519 | System.out.println("nill"); 2520 | } 2521 | } 2522 | GoldAccount.java 2523 | 2524 | package p1; 2525 | public class PlatinumAccount extends GoldAccount { 2526 | //No Error Because default scope is increase to public 2527 | @Override 2528 | public void rateOfInterest() { 2529 | System.out.println("6% PA"); 2530 | } 2531 | 2532 | } 2533 | PlatinumAccount.java 2534 | 2535 | Example 4: 2536 | ---------- 2537 | package p1; 2538 | public class GoldAccount { 2539 | 2540 | public void rateOfInterest() { 2541 | System.out.println("nill"); 2542 | } 2543 | } 2544 | 2545 | package p1; 2546 | public class PlatinumAccount extends GoldAccount { 2547 | //Error Because public scope is reduced to default 2548 | @Override 2549 | void rateOfInterest() { 2550 | System.out.println("6% PA"); 2551 | } 2552 | 2553 | } 2554 | 2555 | Example 5: 2556 | ----------- 2557 | package p1; 2558 | public class GoldAccount { 2559 | 2560 | void rateOfInterest() { 2561 | System.out.println("nill"); 2562 | } 2563 | } 2564 | 2565 | package p1; 2566 | public class PlatinumAccount extends GoldAccount { 2567 | //No Error Because default scope is increased to protected 2568 | @Override 2569 | protected void rateOfInterest() { 2570 | System.out.println("6% PA"); 2571 | } 2572 | } 2573 | Example 6: 2574 | ----------- 2575 | package p1; 2576 | public class GoldAccount { 2577 | 2578 | private void rateOfInterest() { 2579 | System.out.println("nill"); 2580 | } 2581 | } 2582 | 2583 | package p1; 2584 | public class PlatinumAccount extends GoldAccount { 2585 | //Error Because private methods we cannot inherit 2586 | //Without inheritance Overriding cannot be done 2587 | 2588 | @Override 2589 | protected void rateOfInterest() { 2590 | System.out.println("6% PA"); 2591 | } 2592 | 2593 | } 2594 | 2595 | #################################### 2596 | Note: 2597 | -> Static members cannot be inherited 2598 | Example : 2599 | package p1; 2600 | public class GoldAccount { 2601 | static int x = 100; 2602 | public static void rateOfInterest() { 2603 | System.out.println("nill"); 2604 | } 2605 | } 2606 | package p1; 2607 | 2608 | public class PlatinumAccount extends GoldAccount { 2609 | 2610 | public static void main(String[] args) { 2611 | PlatinumAccount.rateOfInterest(); 2612 | System.out.println(PlatinumAccount.x); 2613 | //GoldAccount.rateOfInterest(); 2614 | //GoldAccount.x 2615 | } 2616 | 2617 | } 2618 | //PlatinumAccount.rateOfInterest()---->Converted to ------>GoldAccount.rateOfInterest(); 2619 | //PlatinumAccount.x--->Converted---->GoldAccount.x 2620 | //Hence we are getting output 2621 | 2622 | ################################################ 2623 | Question : Can we Override static methods? 2624 | -> We cannot override static methods, as we cannot inherit static methods 2625 | ######################################## 2626 | 2627 | ################################################ 2628 | Question : Can we Override static methods? 2629 | -> We cannot override static methods, as we cannot inherit static methods 2630 | ######################################## 2631 | 2632 | Example 1: 2633 | package p1; 2634 | public class GoldAccount { 2635 | 2636 | public static void test() { 2637 | System.out.println(100); 2638 | } 2639 | } 2640 | package p1; 2641 | 2642 | public class PlatinumAccount extends GoldAccount { 2643 | 2644 | @Override 2645 | public static void test() {//--->Error, cannot override static methods 2646 | System.out.println(100); 2647 | } 2648 | 2649 | } 2650 | 2651 | Example 2: During Overriding method return type should match 2652 | 2653 | Example 2: During Overriding method return type should match 2654 | package p1; 2655 | public class GoldAccount { 2656 | 2657 | public int test() { 2658 | return 100; 2659 | } 2660 | } 2661 | package p1; 2662 | 2663 | public class PlatinumAccount extends GoldAccount { 2664 | 2665 | @Override 2666 | public void test() {//-> Error because method return type is not matching 2667 | System.out.println(100); 2668 | } 2669 | 2670 | } 2671 | 2672 | b. Overloading (Compile time polymorphism) 2673 | -> Here we develop more than one method with same name in same class provided they have different number of arguments or different type of arguments 2674 | 2675 | Example: 2676 | -> I want to send email with attachment & Also an email to be sent without attachment 2677 | -> Email feature is taking more than one form 2678 | 2679 | package p1; 2680 | 2681 | public class EmailService { 2682 | public void sendEmail(String to, String subject, String message) { 2683 | System.out.println("Email sending...."); 2684 | } 2685 | public void sendEmail(String to, String subject, String message, String filePath) { 2686 | System.out.println("Email sending with attachment....."); 2687 | } 2688 | public static void main(String[] args) { 2689 | EmailService service = new EmailService(); 2690 | service.sendEmail("mike@gmail.com", "Welcome", "Some Message","G:\\image.png"); 2691 | } 2692 | } 2693 | 2694 | ################################ 2695 | Final Keyword 2696 | ################################ 2697 | -> If you make a variable final then we cannot change it's value(Just like constant variables) 2698 | -> If you make static/non-static variable final then initialization is mandatory 2699 | -> If you make a method final then overriding is not allowed 2700 | -> If you make a class final then inheritance is not allowed 2701 | 2702 | 2703 | Example 1: 2704 | ---------- 2705 | package p1; 2706 | 2707 | public class A { 2708 | public static void main(String[] args) { 2709 | final int x = 10; 2710 | x=20;//-->Cannot change the value of final variable 2711 | System.out.println(x); 2712 | } 2713 | } 2714 | 2715 | package p1; 2716 | 2717 | public class A { 2718 | final int x;//->final mandatory to be initialized 2719 | final static int y;//->final mandatory to be initialized 2720 | //These above errors are called as blank field errors 2721 | public static void main(String[] args) { 2722 | 2723 | } 2724 | } 2725 | 2726 | Example 3: 2727 | ---------- 2728 | package p1; 2729 | 2730 | public class A { 2731 | final public void test() { 2732 | System.out.println(100); 2733 | } 2734 | } 2735 | package p1; 2736 | 2737 | public class B extends A{ 2738 | @Override 2739 | public void test() {//--> Error because we cannot override final methods 2740 | System.out.println(100); 2741 | } 2742 | } 2743 | 2744 | Example 4: 2745 | ---------- 2746 | package p1; 2747 | 2748 | final public class A { 2749 | 2750 | } 2751 | package p1; 2752 | 2753 | public class B extends A{//---> Error because we cannot inherit final class 2754 | 2755 | } 2756 | 2757 | ##################################### 2758 | interfaces in java - abstraction 2759 | ####################################### 2760 | -> An interface can consist of only incomplete methods/abstract methods in it ( Java Version 7) 2761 | -> All Variables in an interface by default are final & static 2762 | -> Interfaces defines what needs to be developed and not how it is to be developed 2763 | -> Interfaces are like contract that a class follows, and the class has to implements these methods 2764 | -> An Object of interface cannot be created. Because objects cannot have incomplete methods in it 2765 | -> loosely coupled 2766 | -> It forces all the implementation classes to have same method name resulting in good design 2767 | 2768 | Example 1: 2769 | --------- 2770 | package p1; 2771 | 2772 | public interface A { 2773 | 2774 | public void test1() {//-->Error because complete methods are not allowed in an interface 2775 | 2776 | } 2777 | } 2778 | 2779 | Example 2: 2780 | ----------- 2781 | package p1; 2782 | 2783 | public interface A { 2784 | public void test1() ; 2785 | } 2786 | 2787 | Example 3: 2788 | ---------- 2789 | package p1; 2790 | 2791 | public interface NotificationService { 2792 | 2793 | //What needs to be implemented 2794 | public void emailService(); 2795 | public void whatsAppService(); 2796 | public void smsService(); 2797 | } 2798 | 2799 | package p1; 2800 | 2801 | public class NotificationServiceImpl 2802 | implements NotificationService 2803 | { 2804 | //How it will be implemented 2805 | @Override 2806 | public void emailService() { 2807 | System.out.println("Email Sending"); 2808 | } 2809 | @Override 2810 | public void whatsAppService() { 2811 | System.out.println("Whats app Sending"); 2812 | } 2813 | @Override 2814 | public void smsService() { 2815 | System.out.println("SMS Sending"); 2816 | } 2817 | public static void main(String[] args) { 2818 | NotificationServiceImpl impl = new NotificationServiceImpl(); 2819 | impl.emailService(); 2820 | impl.smsService(); 2821 | impl.whatsAppService(); 2822 | } 2823 | } 2824 | 2825 | Example 4: 2826 | ---------- 2827 | package p1; 2828 | 2829 | public interface PropertyService { 2830 | 2831 | public void addProperty(); 2832 | public void updateProperty(); 2833 | public void deleteProperty(); 2834 | public void searchProperty(); 2835 | } 2836 | package p1; 2837 | 2838 | public class PropertyServiceImpl 2839 | implements PropertyService 2840 | { 2841 | @Override 2842 | public void addProperty() { 2843 | System.out.println("Added Property"); 2844 | } 2845 | @Override 2846 | public void updateProperty() { 2847 | System.out.println("Updated Property"); 2848 | } 2849 | @Override 2850 | public void deleteProperty() { 2851 | System.out.println("Deleted Property"); 2852 | } 2853 | @Override 2854 | public void searchProperty() { 2855 | System.out.println("Search Property"); 2856 | } 2857 | public static void main(String[] args) { 2858 | PropertyServiceImpl service = new PropertyServiceImpl(); 2859 | service.addProperty(); 2860 | service.updateProperty(); 2861 | service.deleteProperty(); 2862 | service.searchProperty(); 2863 | } 2864 | 2865 | } 2866 | 2867 | ############################# 2868 | Note: Drawback of inheritance 2869 | ############################# 2870 | -> Inheritance results in tightly couple programming. 2871 | 2872 | Example: 2873 | --------- 2874 | package p1; 2875 | 2876 | public class Dog { 2877 | 2878 | public void noise() { 2879 | System.out.println("Bow Bow"); 2880 | } 2881 | } 2882 | 2883 | package p1; 2884 | 2885 | public class Cat extends Dog{ 2886 | public static void main(String[] args) { 2887 | Cat c = new Cat(); 2888 | c.noise(); 2889 | } 2890 | } 2891 | 2892 | Example 5: 2893 | ---------- 2894 | package p1; 2895 | 2896 | public interface Calculator { 2897 | public void add(int x, int y); 2898 | public void mul(int x, int y); 2899 | } 2900 | 2901 | package p1; 2902 | public class OrdinaryCalc implements Calculator{ 2903 | 2904 | @Override 2905 | public void add(int x, int y) { 2906 | System.out.println(x+y); 2907 | } 2908 | 2909 | @Override 2910 | public void mul(int x, int y) { 2911 | System.out.println(x*y); 2912 | } 2913 | 2914 | } 2915 | 2916 | package p1; 2917 | 2918 | public class SciCalc implements Calculator{ 2919 | 2920 | @Override 2921 | public void add(int x, int y) { 2922 | System.out.println(x*x+y*y); 2923 | } 2924 | 2925 | @Override 2926 | public void mul(int x, int y) { 2927 | int num1 = x*x; 2928 | int num2 = y*y; 2929 | System.out.println(num1*num2); 2930 | } 2931 | 2932 | } 2933 | package p1; 2934 | 2935 | public class MainCalc { 2936 | public static void main(String[] args) { 2937 | OrdinaryCalc oc = new OrdinaryCalc(); 2938 | oc.add(10, 20); 2939 | oc.mul(10, 20); 2940 | 2941 | SciCalc sc = new SciCalc(); 2942 | sc.add(10, 20); 2943 | sc.mul(10, 20); 2944 | 2945 | 2946 | } 2947 | } 2948 | 2949 | Advantages of interfaces 2950 | --------------------------- 2951 | 2952 | 2953 | 1. abstraction: hiding of implementation details. That is we define what needs to be developed and not how it needs to be developed 2954 | 2. It imposes contract on a class to implement all methods of an interface 2955 | 3. Results in good design 2956 | 4. loosely coupled 2957 | 2958 | -> All Variables by default in an interface are final and static 2959 | ---------------------------------------------------------------- 2960 | package p1; 2961 | 2962 | //-> All variables in the given program are final & static 2963 | //-> Hence intialization is mandatory 2964 | public interface A { 2965 | int MAX_VAL=100; 2966 | static final int MIN_VAL=0; 2967 | } 2968 | package p1; 2969 | 2970 | public class B { 2971 | public static void main(String[] args) { 2972 | System.out.println(A.MAX_VAL); 2973 | System.out.println(A.MIN_VAL); 2974 | } 2975 | } 2976 | Output: 2977 | 100 2978 | 0 2979 | 2980 | ################################################# 2981 | Note: 2982 | -> Class to Class Inheritance --> extends 2983 | -> interface to interface Inheritance --> extends 2984 | -> interface to class Inheritance --> implements 2985 | ################################################# 2986 | 2987 | 2988 | Example 1: 2989 | --------- 2990 | package p1; 2991 | 2992 | public interface A { 2993 | public void test1(); 2994 | } 2995 | package p1; 2996 | 2997 | public interface B extends A{ 2998 | public void test2(); 2999 | } 3000 | package p1; 3001 | 3002 | public class C implements B { 3003 | 3004 | @Override 3005 | public void test1() { 3006 | System.out.println("From test1"); 3007 | } 3008 | 3009 | @Override 3010 | public void test2() { 3011 | System.out.println("From test2"); 3012 | } 3013 | public static void main(String[] args) { 3014 | C c1 = new C(); 3015 | c1.test1(); 3016 | c1.test2(); 3017 | } 3018 | 3019 | } 3020 | 3021 | Example 2: 3022 | ----------- 3023 | package p1; 3024 | 3025 | public interface A { 3026 | public void test2(); 3027 | } 3028 | package p1; 3029 | 3030 | public interface B { 3031 | public void test1(); 3032 | } 3033 | package p1; 3034 | 3035 | public interface C extends A,B { 3036 | 3037 | 3038 | } 3039 | 3040 | Example 3: 3041 | ---------- 3042 | -> If you do mulitple inheritance on a class then it's parent should be interfaces only 3043 | 3044 | package p1; 3045 | public interface A { 3046 | public void test2(); 3047 | } 3048 | 3049 | package p1; 3050 | public interface B { 3051 | public void test1(); 3052 | } 3053 | 3054 | package p1; 3055 | public class C implements A,B { 3056 | 3057 | @Override 3058 | public void test1() { 3059 | System.out.println("From test1"); 3060 | } 3061 | 3062 | @Override 3063 | public void test2() { 3064 | System.out.println("From test2"); 3065 | } 3066 | public static void main(String[] args) { 3067 | C c1 = new C(); 3068 | c1.test1(); 3069 | c1.test2(); 3070 | } 3071 | 3072 | } 3073 | 3074 | Example 4: 3075 | ---------- 3076 | -> On class can we perform extends implements together - yes 3077 | -> But ensure extends is used first and then implements 3078 | 3079 | package p1; 3080 | public interface A { 3081 | public void test2(); 3082 | } 3083 | 3084 | package p1; 3085 | public class B { 3086 | public void test1() { 3087 | System.out.println("From test1"); 3088 | } 3089 | } 3090 | 3091 | package p1; 3092 | public class C extends B implements A { 3093 | @Override 3094 | public void test2() { 3095 | System.out.println("From test2"); 3096 | } 3097 | public static void main(String[] args) { 3098 | C c1 = new C(); 3099 | c1.test1(); 3100 | c1.test2(); 3101 | } 3102 | } 3103 | 3104 | ############################################################### 3105 | Question: Can i develop incomplete static method in an interface? 3106 | Answer: We cannot inherit static method & override, hence incomplete static methods are not allowed in an interface 3107 | 3108 | Example: 3109 | package p1; 3110 | public interface A { 3111 | public static void test2();//-------> Error 3112 | } 3113 | 3114 | Note: In an interface a method can only be public/default, it cannot be private/protected 3115 | Example 3116 | -------- 3117 | package p1; 3118 | 3119 | public interface A { 3120 | protected void test1();//----->Error 3121 | private void test2();//--->Error 3122 | void test3();//----->No Error 3123 | public void test4();//--->No Error 3124 | } 3125 | 3126 | Example 2: 3127 | --------- 3128 | package p1; 3129 | 3130 | public interface A { 3131 | 3132 | } 3133 | 3134 | package p1; 3135 | //An object of interface cannot be created 3136 | //Reference variable of interface can be created 3137 | public class B { 3138 | public static void main(String[] args) { 3139 | A a1 = new A();//--->Error 3140 | A a2;//--->No Error 3141 | } 3142 | } 3143 | 3144 | ################## 3145 | What are marker interface? 3146 | Answer: Empty Interface is called as marker interface in java 3147 | ######################### 3148 | 3149 | Java 8 Features 3150 | ################### 3151 | 1. default keyword: 3152 | -> Using default keyword we can create complete methods inside interface. This was introduced in java version 8 3153 | 3154 | Example 1: 3155 | -------- 3156 | package p1; 3157 | public interface A { 3158 | default public void test1() { 3159 | System.out.println("From test1"); 3160 | } 3161 | } 3162 | 3163 | package p1; 3164 | public class B implements A { 3165 | public static void main(String[] args) { 3166 | B b1 = new B(); 3167 | b1.test1(); 3168 | } 3169 | } 3170 | Output: 3171 | From test1 3172 | 3173 | Example 2: 3174 | ---------- 3175 | package p1; 3176 | public interface A { 3177 | default public void test1() { 3178 | System.out.println("From test1"); 3179 | } 3180 | public void test2(); 3181 | } 3182 | 3183 | package p1; 3184 | public class B implements A { 3185 | public static void main(String[] args) { 3186 | B b1 = new B(); 3187 | b1.test1(); 3188 | b1.test2(); 3189 | } 3190 | @Override 3191 | public void test2() { 3192 | System.out.println("From test2"); 3193 | } 3194 | } 3195 | Output: 3196 | ------- 3197 | From test1 3198 | From test2 3199 | 3200 | 3201 | 2. Functional Interface: 3202 | --------------------------- 3203 | -> A functional interface should consist of exactly one incomplete method inside it 3204 | -> Inside functional interface we can develop any number of complete methods using default keyword 3205 | -> During inheritance/multiple inheritance if more than one method is inherited to Functional Interface then you will get error. As shown in below examples 3206 | 3207 | Example 1: 3208 | -------------------- 3209 | @FunctionalInterface 3210 | public interface A {//Error-> Because cannot have ZERO incomplete method 3211 | } 3212 | 3213 | Example 2: 3214 | ---------- 3215 | @FunctionalInterface 3216 | public interface A { 3217 | public void test1(); 3218 | } 3219 | 3220 | Output: 3221 | No Error because it has exactly one incomplete method 3222 | 3223 | Example 3: 3224 | ---------- 3225 | @FunctionalInterface 3226 | public interface A {//Error->A functional interface should consist of exactly one //incomplete method inside it 3227 | public void test1(); 3228 | public void test2(); 3229 | } 3230 | 3231 | public static void main(String[] args) { 3232 | B b1 = new B(); 3233 | b1.test1(); 3234 | b1.test2(); 3235 | b1.test3(); 3236 | b1.test4(); 3237 | } 3238 | 3239 | @Override 3240 | public void test1() { 3241 | System.out.println("From test 1"); 3242 | } 3243 | 3244 | } 3245 | Output: 3246 | --------- 3247 | From test 1 3248 | From Test 2 3249 | From Test 3 3250 | From Test 4 3251 | 3252 | Example 5: 3253 | ----------- 3254 | package p1; 3255 | public interface A { 3256 | public void test1(); 3257 | } 3258 | package p1; 3259 | @FunctionalInterface 3260 | public interface B extends A{ 3261 | 3262 | } 3263 | Output: No Error 3264 | 3265 | Example 2: 3266 | ---------- 3267 | package p1; 3268 | public interface A { 3269 | public void test1(); 3270 | public void test2(); 3271 | } 3272 | package p1; 3273 | @FunctionalInterface 3274 | public interface B extends A{ 3275 | 3276 | } 3277 | Output: Error because we are inheriting 2 method to functional interface 3278 | 3279 | Example 6: 3280 | --------- 3281 | package p1; 3282 | public interface A { 3283 | public void test1(); 3284 | 3285 | } 3286 | 3287 | package p1; 3288 | @FunctionalInterface 3289 | public interface B extends A{ 3290 | public void test2(); 3291 | } 3292 | Output: Output: Error because we are inheriting 1 method to functional interface and functional interface has a method which totals to 2 methods in an interface 3293 | 3294 | 3. lambdas expression - 3295 | ------------------------- 3296 | -> It was introduced in java 8 3297 | -> Reduces number of lines of code 3298 | -> Works with functional interfaces 3299 | -> We can use lambdas expression in stream API 3300 | 3301 | How Lambdas Expression Works? 3302 | 3303 | Note: Fuctional Programming defines you say "what you want, not how to do it step-by-step" 3304 | 3305 | -> Its creates Annonymous class behind the scene. A class without any name is called Annonymous . 3306 | -> Then lambdas expression creates an object and load the method by implementing inside object. 3307 | -> Then we use object reference to call that implemented method 3308 | 3309 | Example 1: 3310 | ------------ 3311 | package p1; 3312 | @FunctionalInterface 3313 | public interface A { 3314 | public void test1(); 3315 | 3316 | } 3317 | package p1; 3318 | public class B{ 3319 | 3320 | public static void main(String[] args) { 3321 | A a1 = ()->{ 3322 | System.out.println(100); 3323 | }; 3324 | a1.test1(); 3325 | } 3326 | } 3327 | 3328 | Example 2: 3329 | ----------- 3330 | package p1; 3331 | @FunctionalInterface 3332 | public interface A { 3333 | public void test1(int x); 3334 | 3335 | } 3336 | package p1; 3337 | public class B{ 3338 | public static void main(String[] args) { 3339 | A a1 = (int x)->{ 3340 | System.out.println(x); 3341 | }; 3342 | a1.test1(100); 3343 | } 3344 | } 3345 | 3346 | Example 3: 3347 | ---------- 3348 | package p1; 3349 | @FunctionalInterface 3350 | public interface A { 3351 | public void test1(int x); 3352 | default public void test2() { 3353 | System.out.println(2); 3354 | } 3355 | default public void test3() { 3356 | System.out.println(3); 3357 | } 3358 | } 3359 | package p1; 3360 | public class B{ 3361 | 3362 | public static void main(String[] args) { 3363 | A a1 = (int y) -> { 3364 | System.out.println(y); 3365 | }; 3366 | a1.test1(100); 3367 | a1.test2(); 3368 | a1.test3(); 3369 | } 3370 | } 3371 | 3372 | 4. stream API 3373 | --------------------- 3374 | Note: to learn Stream API Complete the following first 3375 | a. Collection (Data Structure) 3376 | b. Functional Programming 3377 | 3378 | 5. Optional class - We have to learn exception concept in java first 3379 | -------------------------------------------- 3380 | 3381 | Note: 3382 | What are marker interfaces in java? 3383 | -> Empty interfaces in java are called as marker interface 3384 | 3385 | Example: For marker interface 3386 | package p1; 3387 | public interface A { 3388 | 3389 | } 3390 | 3391 | --------------------------------------------------- 3392 | Abstract class in java 3393 | ---------------------------- 3394 | -> We can create both complete / incomplete non static methods 3395 | -> To define non static incomplete method, it mandatory to use abstract keyword 3396 | -> In an abstract class we can create both static / non static members 3397 | -> We cannot create static incomplete methods here but we can create complete static methods 3398 | -> We can create main method inside abstract class 3399 | -> We cannot create object of abstract class because abstract class can consist of incomplete methods 3400 | -> We cannot perform multiple inheritance 3401 | Example 1: 3402 | ---------- 3403 | package p1; 3404 | abstract public class A { 3405 | public void test1() { 3406 | 3407 | } 3408 | abstract public void test2(); 3409 | public static void test3() { 3410 | 3411 | } 3412 | public static void test4();//Error 3413 | public static void main(String[] args) { 3414 | A a1 = new A();//Error 3415 | } 3416 | } 3417 | 3418 | Example 2: Error because multiple inheritance cannot be done 3419 | ------------------------------------------------------------- 3420 | package p1; 3421 | abstract public class A { 3422 | 3423 | } 3424 | package p1; 3425 | abstract public class B{ 3426 | 3427 | 3428 | } 3429 | package p1; 3430 | public abstract class C extends A,B{//Error 3431 | 3432 | } 3433 | 3434 | Difference between interface & abstract class 3435 | ------------------------------------------------- 3436 | a. interface supports mulitple inheritance but abstract class does not support multiple inheritance 3437 | b. All variables in an interface by default are final & static whereas inside abstract class we can create static/non static/final variables 3438 | 3439 | Example: 3440 | ------- 3441 | package p1; 3442 | abstract public class A { 3443 | static int x = 10; 3444 | public static void test() { 3445 | System.out.println(100); 3446 | } 3447 | public static void main(String[] args) { 3448 | System.out.println(A.x); 3449 | A.test(); 3450 | } 3451 | } 3452 | Output: 3453 | 10 3454 | 100 3455 | 3456 | Example : 3457 | --------- 3458 | package p1; 3459 | abstract public class A { 3460 | int x = 10; 3461 | public void test() { 3462 | System.out.println(100); 3463 | } 3464 | } 3465 | package p1; 3466 | public class B extends A { 3467 | public static void main(String[] args) { 3468 | B b1 = new B(); 3469 | System.out.println(b1.x); 3470 | b1.test(); 3471 | } 3472 | 3473 | } 3474 | Output: 3475 | 10 3476 | 100 3477 | 3478 | Example : 3479 | ----------- 3480 | package p1; 3481 | 3482 | public interface A { 3483 | public void test1(); 3484 | } 3485 | 3486 | public abstract class B implements A{ 3487 | abstract public void test2(); 3488 | } 3489 | 3490 | 3491 | 3492 | package p1; 3493 | public class C extends B{ 3494 | @Override 3495 | public void test1() { 3496 | System.out.println("From test1"); 3497 | } 3498 | @Override 3499 | public void test2() { 3500 | System.out.println("From test2"); 3501 | } 3502 | public static void main(String[] args) { 3503 | C c1 = new C(); 3504 | c1.test1(); 3505 | c1.test2(); 3506 | } 3507 | 3508 | } 3509 | Output: 3510 | From test1 3511 | From test2 3512 | 3513 | ############################################################################# 3514 | Note:In java 8 we can develop complete static methods & main method in an interface 3515 | ################################################################################# 3516 | 3517 | package p1; 3518 | public interface A { 3519 | static int x = 100; 3520 | public static void test1() { 3521 | System.out.println("From test1"); 3522 | } 3523 | public static void main(String[] args) { 3524 | System.out.println(A.x); 3525 | A.test1(); 3526 | } 3527 | } 3528 | Output: 3529 | 100 3530 | From test1 3531 | 3532 | Exception and Exception Handling In Java 3533 | ------------------------------- 3534 | -> Unexpected events are called exceptions. 3535 | -> Exceptions will hault your program execution. 3536 | -> Exceptions will Occur when bad user input is given 3537 | 3538 | Example 1: 3539 | package p1; 3540 | 3541 | public class A { 3542 | public static void main(String[] args) { 3543 | int x = 10; 3544 | int y = 0; 3545 | int z = x / y;//Stop Program Here 3546 | System.out.println(z); 3547 | System.out.println("Welcome"); 3548 | } 3549 | } 3550 | Output: 3551 | Exception in thread "main" java.lang.ArithmeticException: / by zero 3552 | at app_java_1/p1.A.main(A.java:7) 3553 | 3554 | ####################### 3555 | Exception handling 3556 | ####################### 3557 | -> To handle exception we will use "try & catch" block in java 3558 | -> When exception occurs in try block, it will create exception object and that object's address is given to catch block. Catch block will suppress that exception and further code will continue to execute 3559 | 3560 | 3561 | 3562 | Example : 3563 | ---------- 3564 | package p1; 3565 | public class A { 3566 | public static void main(String[] args) { 3567 | try { 3568 | int x = 10; 3569 | int y = 0; 3570 | int z = x/y; 3571 | System.out.println(z); 3572 | } catch (Exception e) { 3573 | e.printStackTrace(); 3574 | } 3575 | System.out.println("Welcome"); 3576 | } 3577 | } 3578 | 3579 | Types of Exception in java 3580 | ########################## 3581 | 3582 | There are two types of exception in java 3583 | 3584 | a. Compiletime / Checked Exception - These Exception will occur when .java file is compiled to .class file. That is during compilation we will get this exception 3585 | 3586 | b. Runtime / Unchecked Exception - These Exception will occur when we run .class file. That is during runtime 3587 | 3588 | Runtime / unchecked Exceptions 3589 | ############################### 3590 | 3591 | 1. ArithmeticException: This exception will occur when invalid mathematical operations are performed 3592 | 3593 | public static void main(String[] args) { 3594 | try { 3595 | int x = 10; 3596 | int y = 0; 3597 | int z = x/y; 3598 | System.out.println(z); 3599 | } catch (ArithmeticException a) { 3600 | a.printStackTrace(); 3601 | } 3602 | System.out.println("Welcome"); 3603 | } 3604 | } 3605 | Output: 3606 | java.lang.ArithmeticException: / by zero 3607 | at app_java_1/p1.A.main(A.java:7) 3608 | Welcome 3609 | 3610 | Example 2: %ZERO 3611 | ---------- 3612 | package p1; 3613 | public class A { 3614 | public static void main(String[] args) { 3615 | try { 3616 | int x = 11; 3617 | int y = 0; 3618 | int z = x%y; 3619 | System.out.println(z); 3620 | } catch (ArithmeticException a) { 3621 | a.printStackTrace(); 3622 | } 3623 | System.out.println("Welcome"); 3624 | } 3625 | } 3626 | 3627 | Output: 3628 | ------- 3629 | java.lang.ArithmeticException: / by zero 3630 | at app_java_1/p1.A.main(A.java:7) 3631 | Welcome 3632 | 3633 | 2. Null PointerException: This exception will occur when with null reference variable we try to access non static members of the class 3634 | Example: 3635 | -------- 3636 | 3637 | Example: 3638 | -------- 3639 | package p1; 3640 | public class A { 3641 | int x = 10; 3642 | public static void main(String[] args) { 3643 | try { 3644 | A a1 = null; 3645 | System.out.println(a1.x); 3646 | } catch (NullPointerException e) { 3647 | e.printStackTrace(); 3648 | } 3649 | System.out.println("Welcome"); 3650 | } 3651 | } 3652 | Output: 3653 | java.lang.NullPointerException: Cannot read field "x" because "a1" is null 3654 | at app_java_1/p1.A.main(A.java:7) 3655 | Welcome 3656 | 3657 | Example 3658 | ---------- 3659 | package p1; 3660 | public class A { 3661 | static int x = 10; 3662 | public static void main(String[] args) { 3663 | try { 3664 | A a1 = null; 3665 | System.out.println(a1.x);//A.x 3666 | } catch (NullPointerException e) { 3667 | e.printStackTrace(); 3668 | } 3669 | System.out.println("Welcome"); 3670 | } 3671 | } 3672 | Output: 3673 | 10 3674 | Welcome 3675 | 3676 | Assignment 1 (Unary Operators): https://www.youtube.com/playlist?list=PLQVUMrcSsQIRZOM2vZoh8bPG1nq6X-nTe 3677 | Assignment 2(Data types & Type Casting): https://www.youtube.com/watch?v=h3TxueBRxnE 3678 | 3679 | Note: Default Constructors in java? 3680 | During compilation automatically empty body ZERO args constructor is added to .class file when developer doesnot creates a constructor explicitly 3681 | 3682 | Example 1: 3683 | package p1; 3684 | public class A { 3685 | 3686 | public static void main(String[] args) { 3687 | A a1 = new A(); 3688 | } 3689 | } 3690 | //No Error because default constructor will get created 3691 | 3692 | Example 2: 3693 | package p1; 3694 | public class A { 3695 | 3696 | public static void main(String[] args) { 3697 | A a1 = new A(100); 3698 | } 3699 | } 3700 | //Error because default constructor will not get created for constructor with arguments 3701 | 3702 | Example 3: 3703 | --------- 3704 | package p1; 3705 | public class A { 3706 | A(int x){ 3707 | 3708 | } 3709 | public static void main(String[] args) { 3710 | A a1 = new A(100); 3711 | A a2 = new A(); 3712 | } 3713 | } 3714 | //Error because default constructor will not get created when object with and without args are created in same program 3715 | 3716 | Note: 3717 | 3718 | Primitive Wrapper Class 3719 | byte Byte 3720 | short Short 3721 | int Integer 3722 | long Long 3723 | float Float 3724 | double Double 3725 | char Character 3726 | 3727 | Example 1: I want to convert String to Integer 3728 | 3729 | public class A { 3730 | public static void main(String[] args) { 3731 | String x = "100"; 3732 | int y = Integer.parseInt(x); 3733 | System.out.println(y); 3734 | } 3735 | } 3736 | Output 3737 | 100 3738 | 3739 | Example 3:I want to convert String to Double 3740 | 3741 | public class A { 3742 | public static void main(String[] args) { 3743 | String x = "10.3"; 3744 | double y = Double.parseDouble(x); 3745 | System.out.println(y); 3746 | 3747 | } 3748 | } 3749 | 3750 | Example 4: 3751 | ----------- 3752 | package p1; 3753 | 3754 | public class A { 3755 | public static void main(String[] args) { 3756 | try { 3757 | String x = "10.3dasdsd"; 3758 | double y = Double.parseDouble(x); 3759 | System.out.println(y); 3760 | } catch (NumberFormatException e) { 3761 | e.printStackTrace(); 3762 | } 3763 | System.out.println("Welcome"); 3764 | 3765 | } 3766 | } 3767 | Output: 3768 | java.lang.NumberFormatException: For input string: "10.3dasdsd" 3769 | at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) 3770 | at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) 3771 | at java.base/java.lang.Double.parseDouble(Double.java:792) 3772 | at app_java_1/p1.A.main(A.java:7) 3773 | Welcome 3774 | 3775 | ###################### 3776 | What is class upcasting? 3777 | ######################## 3778 | -> Here we store child class object address into parent class reference variable, so that reference variable becomes re-usable 3779 | 3780 | Example: Reusing reference variable 3781 | 3782 | public class A { 3783 | 3784 | } 3785 | 3786 | public class B extends A{ 3787 | 3788 | } 3789 | 3790 | public class C extends A{ 3791 | public static void main(String[] args) { 3792 | A a1 = new B(); 3793 | System.out.println(a1); 3794 | a1 = new C(); 3795 | System.out.println(a1); 3796 | } 3797 | } 3798 | 3799 | Example 2: How we can implement polymorphism for multiple child classes by using same reference variable 3800 | 3801 | public class A { 3802 | void display() { 3803 | System.out.println("Inside class A"); 3804 | } 3805 | } 3806 | 3807 | public class B extends A{ 3808 | @Override 3809 | void display() { 3810 | System.out.println("Inside class B"); 3811 | } 3812 | } 3813 | 3814 | public class C extends A{ 3815 | @Override 3816 | void display() { 3817 | System.out.println("Inside class C"); 3818 | } 3819 | public static void main(String[] args) { 3820 | 3821 | A a1 = new B(); 3822 | a1.display(); // Calls B's version => "Inside class B" 3823 | a1 = new C(); 3824 | a1.display(); // Calls C's version => "Inside class C" 3825 | } 3826 | } 3827 | 3828 | 3829 | Note: 3830 | 3831 | 🧠 Why do we do it? 3832 | 1. To reuse the parent class reference for multiple child class objects. 3833 | 2. To enable polymorphism on mulitple child classes. 3834 | 3. To write generic, flexible code that works with different subclasses. 3835 | 3836 | Note: 3837 | instanceof - It will check which class object address is present inside the reference variable 3838 | 3839 | 3840 | Note: 3841 | instanceof - It will check which class object address is present inside the reference variable 3842 | 3843 | Example: 3844 | 3845 | public class A { 3846 | 3847 | } 3848 | 3849 | public class B extends A{ 3850 | 3851 | } 3852 | 3853 | public class C extends A{ 3854 | 3855 | public static void main(String[] args) { 3856 | 3857 | A a1 = new B(); 3858 | System.out.println(a1 instanceof B); 3859 | } 3860 | } 3861 | 3862 | Output: 3863 | true 3864 | 3865 | What is class downcasting? 3866 | ########################## 3867 | -> Here we store parent class object address into child class reference variable 3868 | -> To perform downcasting, firstly do upcasting and then perform downcasting 3869 | Example: 3870 | --------- 3871 | public class A { 3872 | 3873 | } 3874 | public class B extends A{ 3875 | 3876 | } 3877 | public class C extends A{ 3878 | 3879 | public static void main(String[] args) { 3880 | 3881 | A a1 = new B(); 3882 | if(a1 instanceof B) { 3883 | B b1 = (B)a1; 3884 | System.out.println("Downcasting successful!"); 3885 | } 3886 | } 3887 | } 3888 | 3889 | Example: 3890 | -------- 3891 | public class A { 3892 | public void display() { 3893 | System.out.println("Inside class A"); 3894 | } 3895 | } 3896 | public class B extends A{ 3897 | public void showB() { 3898 | System.out.println("Inside class B"); 3899 | } 3900 | } 3901 | public class C extends A { 3902 | public void showC() { 3903 | System.out.println("Inside class C"); 3904 | } 3905 | } 3906 | public class MainClass { 3907 | public static void processObject(A obj) { 3908 | if (obj instanceof B) { 3909 | B b = (B) obj; // Downcasting to B 3910 | b.showB(); 3911 | } else if (obj instanceof C) { 3912 | C c = (C) obj; // Downcasting to C 3913 | c.showC(); 3914 | } else { 3915 | obj.display(); 3916 | } 3917 | } 3918 | public static void main(String[] args) { 3919 | processObject(new B()); 3920 | } 3921 | } 3922 | 3923 | Note: 3924 | -Using upcasting we can access only parent class members, you cannot access child class members 3925 | -After upcating to access child class members we have to perform downcasting 3926 | 3927 | Example: 3928 | --------- 3929 | package p1; 3930 | 3931 | public class A { 3932 | public void display() { 3933 | System.out.println("Inside class A"); 3934 | } 3935 | } 3936 | public class B extends A{ 3937 | public void showB() { 3938 | System.out.println("Inside class B"); 3939 | } 3940 | public static void main(String[] args) { 3941 | A a1 = new B(); 3942 | if(a1 instanceof B) { 3943 | B b1 = (B)a1; 3944 | b1.showB(); 3945 | } 3946 | 3947 | } 3948 | } 3949 | 3950 | Arrays in java: 3951 | ############### 3952 | -> In java arrays holds collection of value 3953 | -> Anything that holds colection of values is called as a data struture 3954 | -> Array in java is treated as an object 3955 | -> Arrays starts with index zero 3956 | -> To store and read values we use this formula in arrays - start_address + index*memory_size 3957 | -> Memory allocation happens to be in sequence(Continuous). 3958 | 3959 | Example 1: 3960 | ---------- 3961 | package p1; 3962 | public class A { 3963 | public static void main(String[] args) { 3964 | int[] age = new int[4]; 3965 | age[0] = 90; 3966 | age[1] = 89; 3967 | age[2] = 67; 3968 | age[3] = 97; 3969 | 3970 | System.out.println(age[0]); 3971 | System.out.println(age[1]); 3972 | System.out.println(age[2]); 3973 | System.out.println(age[3]); 3974 | } 3975 | 3976 | } 3977 | 3978 | -> To find size of an array dynamically we will use public final field "length". When array is created this variable will get initiliazed 3979 | Example : age.length 3980 | 3981 | Example 2: 3982 | ---------- 3983 | package p1; 3984 | 3985 | public class A { 3986 | public static void main(String[] args) { 3987 | int[] age = new int[40]; 3988 | System.out.println(age.length); 3989 | } 3990 | 3991 | } 3992 | Ouput: 40 3993 | 3994 | ########################## 3995 | Unary Operators in java 3996 | ############################## 3997 | 3998 | Types: 3999 | 1. Increment Operator 4000 | 1.1 Post Increment 4001 | 1.2 Pre Increment 4002 | 4003 | 2. Decrement Operator 4004 | 2.1 Post Decrement 4005 | 2.2 Pre Decrement 4006 | 4007 | 1.1 Post Increment - Increment the value of the variable next time you see the same variable 4008 | 4009 | Example 1: 4010 | ---------- 4011 | package p1; 4012 | 4013 | public class A { 4014 | public static void main(String[] args) { 4015 | int i=0; 4016 | int j = i++; 4017 | System.out.println(i); 4018 | System.out.println(j); 4019 | } 4020 | 4021 | } 4022 | Output: 4023 | 1 4024 | 0 4025 | 4026 | Example 2: 4027 | ---------- 4028 | package p1; 4029 | 4030 | public class A { 4031 | public static void main(String[] args) { 4032 | int i=10; 4033 | int j = i++ + i++ + i++; 4034 | System.out.println(i); 4035 | System.out.println(j); 4036 | } 4037 | 4038 | } 4039 | Ouput: 4040 | 13 4041 | 33 4042 | 4043 | Example 3: 4044 | ---------- 4045 | package p1; 4046 | 4047 | public class A { 4048 | public static void main(String[] args) { 4049 | int i=10; 4050 | 4051 | int j = i++ + i++ + i++ + i++ + i++; 4052 | System.out.println(i); 4053 | System.out.println(j); 4054 | } 4055 | 4056 | } 4057 | Ouput: 4058 | 15 4059 | 60 4060 | 4061 | 1.2 Pre Increment - Increment the value of variable in same step 4062 | 4063 | Example 1: 4064 | ----------- 4065 | package p1; 4066 | 4067 | public class A { 4068 | public static void main(String[] args) { 4069 | int i=10; 4070 | 4071 | int j = ++i; 4072 | 4073 | System.out.println(i); 4074 | System.out.println(j); 4075 | } 4076 | 4077 | } 4078 | Ouput: 4079 | 11 4080 | 11 4081 | 4082 | Example 2: 4083 | ---------- 4084 | package p1; 4085 | 4086 | public class A { 4087 | public static void main(String[] args) { 4088 | int i=10; 4089 | 4090 | int j = ++i + ++i + ++i; 4091 | 4092 | System.out.println(i); 4093 | System.out.println(j); 4094 | } 4095 | 4096 | } 4097 | Ouput: 4098 | 13 4099 | 36 4100 | 4101 | Example 3: 4102 | --------- 4103 | package p1; 4104 | 4105 | public class A { 4106 | public static void main(String[] args) { 4107 | int i=10; 4108 | 4109 | int j = ++i + i++ + ++i + i++ + ++i; 4110 | 4111 | System.out.println(i); 4112 | System.out.println(j); 4113 | } 4114 | 4115 | } 4116 | Output: 4117 | 15 4118 | 63 4119 | 4120 | 2.1 Post Decrement - Here we decrement the value of i by 1 when we next time see the same variable: 4121 | 4122 | Example 1: 4123 | --------- 4124 | package p1; 4125 | 4126 | public class A { 4127 | public static void main(String[] args) { 4128 | int i=10; 4129 | 4130 | int j = i-- + i--; 4131 | 4132 | System.out.println(i); 4133 | System.out.println(j); 4134 | } 4135 | 4136 | } 4137 | Output: 4138 | ------ 4139 | 8 4140 | 19 4141 | 4142 | Example 2: 4143 | ----------- 4144 | package p1; 4145 | 4146 | public class A { 4147 | public static void main(String[] args) { 4148 | int i=10; 4149 | 4150 | int j = i-- + i++ + i-- + i--; 4151 | 4152 | System.out.println(i); 4153 | System.out.println(j); 4154 | } 4155 | 4156 | } 4157 | Output: 4158 | 8 4159 | 38 4160 | 4161 | 2.2 Pre Decrement - Here we decrement the value of the variable in same step by 1 4162 | 4163 | Example 1: 4164 | ---------- 4165 | package p1; 4166 | 4167 | public class A { 4168 | public static void main(String[] args) { 4169 | int i=10; 4170 | 4171 | int j = --i + --i; 4172 | 4173 | System.out.println(i); 4174 | System.out.println(j); 4175 | } 4176 | 4177 | } 4178 | 4179 | ################# 4180 | Scanner class - Is used to provide user input 4181 | ################ 4182 | 4183 | Example 1: 4184 | ---------- 4185 | package p1; 4186 | import java.util.Scanner; 4187 | public class A { 4188 | public static void main(String[] args) { 4189 | 4190 | Scanner scan = new Scanner(System.in); 4191 | System.out.println("Enter your name:"); 4192 | String name = scan.next();//Can read one String word only 4193 | System.out.println("Your name is "+name); 4194 | System.out.println("Enter your age:"); 4195 | int age = scan.nextInt();//Can read on Integer value only 4196 | System.out.println("Your age is "+age); 4197 | System.out.println("Enter your weight:"); 4198 | float weight = scan.nextFloat();//Can read on float values 4199 | System.out.println("Your weight is "+weight); 4200 | System.out.println("Enter your Answer:"); 4201 | boolean ans = scan.nextBoolean();//Can read on float values 4202 | System.out.println("Your ans is "+ans); 4203 | scan.close(); 4204 | 4205 | } 4206 | 4207 | } 4208 | Output: 4209 | Enter your name: 4210 | mike 4211 | Your name is mike 4212 | Enter your age: 4213 | 100 4214 | Your age is 100 4215 | Enter your weight: 4216 | 98.34 4217 | Your weight is 98.34 4218 | Enter your Answer: 4219 | true 4220 | Your ans is true 4221 | 4222 | Example 2: How to read multiple String words using nextLine() method 4223 | -------------------------------------------------------------------------- 4224 | package p1; 4225 | import java.util.Scanner; 4226 | public class A { 4227 | public static void main(String[] args) { 4228 | 4229 | Scanner scan = new Scanner(System.in); 4230 | System.out.println("Enter your name:"); 4231 | String name = scan.nextLine();//Can read Multiple String words 4232 | System.out.println(name); 4233 | scan.close(); 4234 | } 4235 | 4236 | } 4237 | 4238 | Assignment 3 (IIB): https://www.youtube.com/watch?v=_3O2bPWckoI&list=PLbLiFkJgsmq6j_fFlZuy0xIs6upvrML26&index=11 4239 | Assignment 4 (SIB): https://www.youtube.com/watch?v=tQmvABQgi8k&list=PLbLiFkJgsmq6j_fFlZuy0xIs6upvrML26&index=12 4240 | Assignment 5 (Super keyword): https://www.youtube.com/watch?v=KETJakbA6tw&list=PLbLiFkJgsmq6j_fFlZuy0xIs6upvrML26&index=14 4241 | 4242 | ############## 4243 | Loops in java 4244 | ############## 4245 | 1. for loop 4246 | Example 1; 4247 | --------- 4248 | public class A { 4249 | public static void main(String[] args) { 4250 | 4251 | for (int i = 0; i < 4; i++) {//0 to 3 4252 | System.out.println(i); 4253 | } 4254 | } 4255 | 4256 | } 4257 | 4258 | Example 2: 4259 | a. User will enter the pin number 1234 4260 | b. If the entered pin number is valid , then program should print welcome and stop 4261 | c. If pin number is invalid, user can attempt to enter pin maximum 3 times. 4262 | d. If All 3 times pin number is invalid then card blocked message we should get 4263 | 4264 | Code: 4265 | package p1; 4266 | import java.util.Scanner; 4267 | //break 4268 | //1. Can be used only inside loops and switch case statements 4269 | //2. It stops the for loop execution 4270 | public class A { 4271 | public static void main(String[] args) { 4272 | Scanner scan = new Scanner(System.in); 4273 | 4274 | for(int i=0;i<3;i++) {//0 to 2 4275 | System.out.println("Enter the pin number"); 4276 | int pinNumber = scan.nextInt(); 4277 | if(pinNumber==1234) { 4278 | System.out.println("Welcome"); 4279 | break; 4280 | }else { 4281 | System.out.println("Invalid Pin number"); 4282 | if(i==2) { 4283 | System.out.println("Card is blocked"); 4284 | } 4285 | } 4286 | } 4287 | scan.close(); 4288 | } 4289 | 4290 | } 4291 | 4292 | 2. while loop 4293 | Example 1: 4294 | ---------- 4295 | 4296 | 2. while loop 4297 | Example 1: 4298 | ---------- 4299 | package p1; 4300 | public class A { 4301 | public static void main(String[] args) { 4302 | int i=0; 4303 | while(i<3) {//If condition true will enter while loop 4304 | System.out.println(i); 4305 | i++; 4306 | } 4307 | } 4308 | 4309 | } 4310 | 4311 | Example 2: 4312 | -------- 4313 | a. Enter the amount, and we should get message please collect cash 4314 | b. Then prompt a message do you want to continue(yes or no) 4315 | c. If yes, then again enter the amount and we should get message please collect cash 4316 | d. If you enter no, then program should stop 4317 | 4318 | Code 4319 | ___________ 4320 | public class A { 4321 | public static void main(String[] args) { 4322 | Scanner scan = new Scanner(System.in); 4323 | String cdn = "yes"; 4324 | while(cdn.equals("yes")) { 4325 | System.out.println("Enter the amount"); 4326 | int amount = scan.nextInt(); 4327 | System.out.println("Please collect rs. "+amount); 4328 | System.out.println("Do you want to continue(yes/no)?"); 4329 | cdn = scan.next(); 4330 | 4331 | if(!cdn.equals("yes")) { 4332 | System.out.println("Thank you. Visit again"); 4333 | } 4334 | } 4335 | } 4336 | } 4337 | 4338 | 3. do while loop - Will run first time without condition check. For second iteration condition will be evaluated 4339 | 4340 | Example 1: 4341 | --------- 4342 | package p1; 4343 | public class A { 4344 | public static void main(String[] args) { 4345 | int i=100; 4346 | do { 4347 | System.out.println(i); 4348 | i++; 4349 | }while(i<3); 4350 | } 4351 | 4352 | } 4353 | 4354 | Example 2: 4355 | ---------- 4356 | package p1; 4357 | public class A { 4358 | public static void main(String[] args) { 4359 | int i=0; 4360 | do { 4361 | System.out.println(i); 4362 | i++; 4363 | }while(i<3); 4364 | } 4365 | 4366 | } 4367 | 4368 | 4. for each loop - 4369 | -> Works only with data structure 4370 | -> Can only read value of data structure one by one 4371 | -> Loop will run until all values reading is not completed in the given data structure 4372 | 4373 | 4374 | Example: 4375 | package p1; 4376 | public class A { 4377 | public static void main(String[] args) { 4378 | int[] age = new int[3]; 4379 | age[0] = 100; 4380 | age[1] = 101; 4381 | age[2] = 120; 4382 | 4383 | for(int x:age) { 4384 | System.out.println(x); 4385 | } 4386 | } 4387 | 4388 | } 4389 | 4390 | #################### 4391 | Continuation of Arrays: 4392 | ###################### 4393 | 4394 | Example 1: 4395 | ---------- 4396 | package p1; 4397 | public class A { 4398 | public static void main(String[] args) { 4399 | int[] age = new int[3]; 4400 | age[0] = 100; 4401 | age[1] = 101; 4402 | age[2] = 120; 4403 | 4404 | for (int i = 0; i < age.length; i++) {//0 to 2 4405 | System.out.println(age[i]); 4406 | } 4407 | } 4408 | 4409 | } 4410 | 4411 | Example 4412 | -------- 4413 | public class A { 4414 | public static void main(String[] args) { 4415 | try { 4416 | int[] age = new int[3]; 4417 | age[0] = 100; 4418 | age[1] = 101; 4419 | age[2] = 120; 4420 | age[3] = 103; 4421 | } catch (ArrayIndexOutOfBoundsException e) { 4422 | e.printStackTrace(); 4423 | } 4424 | System.out.println("Welcome"); 4425 | } 4426 | } 4427 | 4428 | Output: 4429 | java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 4430 | at app_java_1/p1.A.main(A.java:9) 4431 | Welcome 4432 | 4433 | #################### 4434 | Continue Keyword in java 4435 | ##################### 4436 | 4437 | The continue keyword is used inside loops (for, while, or do-while) to skip the current iteration and jump to the next one. 4438 | 4439 | package p1; 4440 | public class A { 4441 | public static void main(String[] args) { 4442 | for (int i = 0; i < 5; i++) { 4443 | 4444 | if(i==3) { 4445 | continue; 4446 | } 4447 | System.out.println(i);//0 1 2 4 4448 | } 4449 | 4450 | } 4451 | 4452 | } 4453 | Output: 4454 | 0 4455 | 1 4456 | 2 4457 | 4 4458 | 4459 | ###################### 4460 | else-if ladder statement 4461 | ###################### 4462 | -> If the user enters yes, it should print pass 4463 | -> If the user enters no, it should print fail 4464 | -> If the user enters something else, it should print invalid input 4465 | 4466 | Example: 4467 | ------------- 4468 | package p1; 4469 | 4470 | import java.util.Scanner; 4471 | 4472 | public class A { 4473 | public static void main(String[] args) { 4474 | Scanner scan = new Scanner(System.in); 4475 | System.out.println("Enter the input(yes/no)"); 4476 | String value = scan.next(); 4477 | if(value.equals("yes")) { 4478 | System.out.println("Pass"); 4479 | } else if(value.equals("no")) { 4480 | System.out.println("Fail"); 4481 | } else { 4482 | System.out.println("Invalid Input"); 4483 | } 4484 | } 4485 | 4486 | } 4487 | 4488 | Operators in java Example 4489 | --------------------------- 4490 | package p1; 4491 | 4492 | //Operators in java 4493 | 4494 | public class A { 4495 | public static void main(String[] args) { 4496 | System.out.println(2==3); 4497 | System.out.println(2>1); 4498 | System.out.println(2<1); 4499 | System.out.println(2>=2); 4500 | System.out.println(2<=2); 4501 | System.out.println(2!=3); 4502 | 4503 | } 4504 | } 4505 | 4506 | 4507 | ############################ 4508 | Switch Case 4509 | ############################ 4510 | 4511 | package p1; 4512 | 4513 | import java.util.Scanner; 4514 | 4515 | public class A { 4516 | public static void main(String[] args) { 4517 | Scanner scan = new Scanner(System.in); 4518 | System.out.println("Enter the floor number"); 4519 | int key=scan.nextInt(); 4520 | switch (key) { 4521 | case 1: 4522 | System.out.println("1st Floor"); 4523 | break; 4524 | case 2: 4525 | System.out.println("2nd Floor"); 4526 | break; 4527 | case 3: 4528 | System.out.println("3rd Floor"); 4529 | break; 4530 | default: 4531 | System.out.println("Invalid input"); 4532 | break; 4533 | } 4534 | } 4535 | 4536 | } 4537 | 4538 | ########################################### 4539 | Removing Duplicate elements from an array 4540 | ########################################### 4541 | -> For the below logic array should be sorted 4542 | 4543 | Steps 4544 | ------ 4545 | 1. Create a temp array which same size as that of x 4546 | int[] x = {3,3,4,5,6,6,7}; 4547 | int[] temp = new int[x.length]; 4548 | 4549 | 2. Compare i with i+1 of array x, if not equal copy the value from x[i] to temp[j] and increment j by 1 4550 | 4551 | 3. When you reach last index of array x, copy that value directly to temp[j]; 4552 | 4553 | Code: 4554 | package p1; 4555 | public class A { 4556 | public static void main(String[] args) { 4557 | int[] x = {3,3,4,5,6,6}; 4558 | int[] temp = new int[x.length]; 4559 | int j=0; 4560 | for(int i=0;i Compare x[i] > x[i+1], if true swap the element 4605 | -> End of first round of comparision heavy element will go to the botton of the array 4606 | -> Compare x[i] > x[i+1], array length-1 times maximum 4607 | 4608 | Code: 4609 | ---- 4610 | package p1; 4611 | public class A { 4612 | public static void main(String[] args) { 4613 | int[] x = {35,21,22,14}; 4614 | //x[i]->x 4615 | //x[i+1]-> y 4616 | int temp; 4617 | for(int j=0;jx[i+1]) { 4620 | temp = x[i+1]; 4621 | x[i+1] = x[i]; 4622 | x[i]= temp; 4623 | } 4624 | } 4625 | } 4626 | 4627 | for(int arr:x) { 4628 | System.out.println(arr); 4629 | } 4630 | } 4631 | 4632 | } 4633 | 4634 | ################# 4635 | 1. Sort and remove duplicate elements 4636 | 2. Get Highest & Lowest Value in an array 4637 | ################# 4638 | 4639 | ###################### 4640 | File Handling in Java 4641 | ####################### 4642 | 4643 | Note: Special Characters 4644 | System.out.print(200); 4645 | System.out.print("\n"); 4646 | System.out.print(300); 4647 | System.out.print("\n"); 4648 | System.out.print(200); 4649 | System.out.print("\t"); 4650 | System.out.print(300); 4651 | 4652 | a. File 4653 | ----------- 4654 | Example 1:It holds the given path in variable "f" without check the file exist 4655 | ---------------------------------------------------- 4656 | package p1; 4657 | import java.io.File; 4658 | public class A { 4659 | public static void main(String[] args) { 4660 | File f = new File("E:\\april\\t1.txt"); 4661 | System.out.println(f); 4662 | } 4663 | } 4664 | 4665 | 1. exists() - 4666 | ------------ 4667 | a. It is a non static method present inside File class 4668 | b. Return type of this method is boolean 4669 | c. It checks whether the file exists in the given path. 4670 | d. If file exists, it will return true or else false 4671 | 4672 | Example: 4673 | --------- 4674 | package p1; 4675 | import java.io.File; 4676 | public class A { 4677 | public static void main(String[] args) { 4678 | File f = new File("G:\\april\\t100.txt"); 4679 | boolean val = f.exists(); 4680 | System.out.println(val); 4681 | } 4682 | } 4683 | 4684 | 2. delete() - 4685 | ------------ 4686 | a. It is a non static method present inside File class 4687 | b. Return type of this method is boolean 4688 | c. It deletes the file exists in the given path. 4689 | d. If file is deleted, it will return true or else false 4690 | 4691 | Example: 4692 | -------- 4693 | package p1; 4694 | import java.io.File; 4695 | public class A { 4696 | public static void main(String[] args) { 4697 | File f = new File("G:\\april\\t1.txt"); 4698 | boolean val = f.delete(); 4699 | System.out.println(val); 4700 | } 4701 | } 4702 | 4703 | 3.mkdir() - 4704 | ------------ 4705 | a. It is a non static method present inside File class 4706 | b. Return type of this method is boolean 4707 | c. It creates folder in the given path. 4708 | d. If folder is created, it will return true or else false 4709 | e. It will not replace existing folder 4710 | 4711 | Example: 4712 | ------- 4713 | package p1; 4714 | import java.io.File; 4715 | public class A { 4716 | public static void main(String[] args) { 4717 | File f = new File("G:\\april\\p2"); 4718 | boolean val = f.mkdir(); 4719 | System.out.println(val); 4720 | } 4721 | } 4722 | Example: To delete Folder using delete() method 4723 | ------------------------------------------------ 4724 | public class A { 4725 | public static void main(String[] args) { 4726 | File f = new File("G:\\april\\p2"); 4727 | boolean val = f.delete(); 4728 | System.out.println(val); 4729 | } 4730 | } 4731 | 4732 | Note: delete() method can delete folder/file both 4733 | 4734 | 4.length() - 4735 | ------------ 4736 | a. It is a non static method present inside File class 4737 | b. Return type of this method is long 4738 | c. It counts characters with white spaces in the given file. 4739 | 4740 | Example- Pankaj Sir academy->18 chars 4741 | 4742 | Example: 4743 | -------- 4744 | package p1; 4745 | import java.io.File; 4746 | public class A { 4747 | public static void main(String[] args) { 4748 | File f = new File("G:\\april\\t1.txt"); 4749 | long val = f.length(); 4750 | System.out.println(val); 4751 | } 4752 | } 4753 | 4754 | Note: 4755 | Compiletime Exception/ Checked Exception 4756 | _____________________________________________ 4757 | -> These exceptions will occur even when the program is correct/Incorrect 4758 | -> Handling exception becomes mandatory when it is compile time 4759 | 4760 | 5. createNewFile()- 4761 | ------------------- 4762 | a. It is non static method present inside File Class 4763 | b. It's return type is boolean 4764 | c. It Throws Compiletime exception. So should be handled before executing the program 4765 | d. If file is created, it will return true or else false 4766 | e. Will not replace exisiting file 4767 | 4768 | 4769 | Example: 4770 | ------ 4771 | package p1; 4772 | import java.io.File; 4773 | import java.io.IOException; 4774 | public class A { 4775 | public static void main(String[] args) { 4776 | try { 4777 | File f = new File("G:\\april\\t1.txt"); 4778 | boolean val = f.createNewFile(); 4779 | System.out.println(val); 4780 | } catch (IOException e) { 4781 | e.printStackTrace(); 4782 | } 4783 | 4784 | } 4785 | } 4786 | 4787 | ################## 4788 | IOException in Java 4789 | ############ 4790 | The IOException is a checked exception in Java, part of the java.io package. It is thrown when an Input/Output operation fails or is interrupted. 4791 | 4792 | Scenario Example 4793 | File not found Reading a file that doesn't exist 4794 | File not readable Permission denied or locked file 4795 | Network I/O failure Socket disconnected unexpectedly 4796 | Stream closed unexpectedly Trying to read from a closed stream 4797 | Writing to a full disk OutputStream write failure 4798 | 4799 | 6. list()- 4800 | ------------------- 4801 | a. It is non static method present inside File Class 4802 | b. It's return type is String[] array 4803 | c. It gets all files/folders names from a given path 4804 | 4805 | Example: 4806 | ------- 4807 | package p1; 4808 | import java.io.File; 4809 | public class A { 4810 | public static void main(String[] args) { 4811 | File f = new File("G:\\april\\"); 4812 | String[] val = f.list(); 4813 | for(String x: val) { 4814 | System.out.println(x); 4815 | } 4816 | System.out.println(val.length); 4817 | } 4818 | } 4819 | 4820 | 2. FileReader: 4821 | ####################### 4822 | -> It can read text file content 4823 | ->read(): int 4824 | 4825 | Example 1: 4826 | ----------- 4827 | package app_java_3; 4828 | import java.io.FileReader; 4829 | public class A { 4830 | public static void main(String[] args) { 4831 | try { 4832 | FileReader fr = new FileReader("G:\\april\\t1.txt"); 4833 | System.out.println((char)fr.read()); 4834 | } catch (Exception e) { 4835 | e.printStackTrace(); 4836 | } 4837 | } 4838 | } 4839 | 4840 | 4841 | Example 2: 4842 | ---------- 4843 | 4844 | package app_java_3; 4845 | import java.io.File; 4846 | import java.io.FileReader; 4847 | public class A { 4848 | public static void main(String[] args) { 4849 | try { 4850 | File f = new File("G:\\april\\t1.txt"); 4851 | FileReader fr = new FileReader(f); 4852 | for (int i = 0; i < f.length(); i++) {//0 to 3 4853 | System.out.print((char)fr.read()); 4854 | } 4855 | fr.close(); 4856 | } catch (Exception e) { 4857 | e.printStackTrace(); 4858 | } 4859 | } 4860 | } 4861 | 4862 | -> read(char[] ch) 4863 | 4864 | Example 4865 | ------- 4866 | package app_java_3; 4867 | import java.io.File; 4868 | import java.io.FileReader; 4869 | public class A { 4870 | public static void main(String[] args) { 4871 | try { 4872 | File f = new File("G:\\april\\t1.txt"); 4873 | FileReader fr = new FileReader(f); 4874 | char[] ch = new char[(int)f.length()]; 4875 | fr.read(ch); 4876 | 4877 | for(char c:ch) { 4878 | System.out.print(c); 4879 | } 4880 | 4881 | fr.close(); 4882 | 4883 | } catch (Exception e) { 4884 | e.printStackTrace(); 4885 | } 4886 | } 4887 | } 4888 | 4889 | ##################### 4890 | FileWriter class 4891 | ##################### 4892 | 1. It will write content to text file 4893 | 2. If File is not present in given path, it will create a new file 4894 | 3. If the file is present in the given path, then it will replace the existing file by default 4895 | 4. During FileWriter object creation if true is given, then it will not replace existing content of the file. But will create new file if file was not present in given path 4896 | Example: FileWriter fw = new FileWriter("G:\\april\\t4.txt",true); 4897 | 5. During FileWriter object creation if false / no boolean value is given, then it will replace existing content of the file and will create new file if file was not present in given path 4898 | Example: FileWriter fw = new FileWriter("G:\\april\\t4.txt",false); 4899 | Example: FileWriter fw = new FileWriter("G:\\april\\t4.txt"); 4900 | 4901 | 4902 | Example: 4903 | --------- 4904 | package app_java_3; 4905 | 4906 | import java.io.FileWriter; 4907 | 4908 | public class A { 4909 | public static void main(String[] args) { 4910 | try { 4911 | FileWriter fw = new FileWriter("G:\\april\\t4.txt",false); 4912 | fw.write(97);//write(int x) 4913 | fw.write("100");//write(String x) 4914 | 4915 | char[] ch = {'a','b','c'}; 4916 | fw.write(ch);//write(char[] x) 4917 | 4918 | fw.close(); 4919 | } catch (Exception e) { 4920 | e.printStackTrace(); 4921 | } 4922 | } 4923 | } 4924 | 4925 | Example 2: 4926 | ---------- 4927 | package app_java_3; 4928 | 4929 | import java.io.FileWriter; 4930 | 4931 | public class A { 4932 | public static void main(String[] args) { 4933 | try { 4934 | FileWriter fw = new FileWriter("G://april//t1.txt"); 4935 | fw.write("mike"); 4936 | fw.write("\n"); 4937 | fw.write("stallin"); 4938 | fw.write("\n"); 4939 | fw.write("adam"); 4940 | fw.close();//Save the file content and closes the file 4941 | } catch (Exception e) { 4942 | e.printStackTrace(); 4943 | } 4944 | } 4945 | } 4946 | 4947 | ######################## 4948 | BufferedReader 4949 | ####################### 4950 | 1. This will improve file reading performance 4951 | 2. This should be used with FileReader class, If we use only BufferedReader then FileReading cannot be done 4952 | 3. It has readLine() method. Using this method we can read the entire line from the file 4953 | 4954 | Example 1: 4955 | ---------- 4956 | package p1; 4957 | import java.io.BufferedReader; 4958 | import java.io.FileReader; 4959 | public class A { 4960 | public static void main(String[] args) { 4961 | try { 4962 | FileReader fr = new FileReader("G://april//t1.txt"); 4963 | BufferedReader br = new BufferedReader(fr); 4964 | System.out.println((char)br.read()); 4965 | br.close(); 4966 | fr.close(); 4967 | } catch (Exception e) { 4968 | e.printStackTrace(); 4969 | } 4970 | } 4971 | } 4972 | 4973 | Example 2: 4974 | ---------- 4975 | package p1; 4976 | import java.io.BufferedReader; 4977 | import java.io.FileReader; 4978 | public class A { 4979 | public static void main(String[] args) { 4980 | try { 4981 | FileReader fr = new FileReader("G://f1//t1.txt"); 4982 | BufferedReader br = new BufferedReader(fr); 4983 | char[] ch = new char[4]; 4984 | br.read(ch); 4985 | for(char c: ch) { 4986 | System.out.print(c); 4987 | } 4988 | br.close(); 4989 | fr.close(); 4990 | } catch (Exception e) { 4991 | e.printStackTrace(); 4992 | } 4993 | } 4994 | } 4995 | 4996 | Example 3: 4997 | ----------- 4998 | package p1; 4999 | import java.io.BufferedReader; 5000 | import java.io.FileReader; 5001 | public class A { 5002 | public static void main(String[] args) { 5003 | try { 5004 | FileReader fr = new FileReader("G://f1//t1.txt"); 5005 | BufferedReader br = new BufferedReader(fr); 5006 | for(int i=0;i<3;i++) { 5007 | System.out.println(br.readLine()); 5008 | } 5009 | br.close(); 5010 | fr.close(); 5011 | } catch (Exception e) { 5012 | e.printStackTrace(); 5013 | } 5014 | } 5015 | } 5016 | 5017 | ##################### 5018 | BufferedWriter 5019 | ##################### 5020 | -> It improves file writing performance 5021 | -> This should be used with FileWriter class, If we use only BufferedWriter then FileWriting cannot be done 5022 | -> BufferedWriter class has newline() method 5023 | Example 1: 5024 | ---------- 5025 | import java.io.BufferedWriter; 5026 | import java.io.FileWriter; 5027 | public class A { 5028 | public static void main(String[] args) { 5029 | try { 5030 | FileWriter fw = new FileWriter("G://april//t1.txt",true); 5031 | BufferedWriter bw = new BufferedWriter(fw); 5032 | bw.write("mike"); 5033 | bw.newLine(); 5034 | bw.write(97); 5035 | char[] ch = {'b','c','d'}; 5036 | bw.newLine(); 5037 | bw.write(ch); 5038 | 5039 | bw.close(); 5040 | fw.close(); 5041 | 5042 | 5043 | } catch (Exception e) { 5044 | e.printStackTrace(); 5045 | } 5046 | } 5047 | } 5048 | 5049 | ################################ 5050 | Serialization & Deserialization 5051 | ################################ 5052 | 5053 | -> Serialization is a process of converting object to byte code and storing that in a file system 5054 | -> De-Serialization is a process of reading byte code from the file and form Object based on byte code 5055 | -> To skip writing certain content to object during serialization we can use transient keyword 5056 | 5057 | ################################ 5058 | Serialization & Deserialization 5059 | ################################ 5060 | 5061 | -> Serialization is a process of converting object to byte code and storing that in a file system 5062 | -> De-Serialization is a process of reading byte code from the file and form Object based on byte code 5063 | -> To skip writing certain content to object during serialization we can use transient keyword 5064 | 5065 | Example: 5066 | -------- 5067 | package p1; 5068 | import java.io.Serializable; 5069 | public class A implements Serializable{ 5070 | String name="mike"; 5071 | transient String password = "testing"; 5072 | } 5073 | 5074 | package p1; 5075 | import java.io.FileOutputStream; 5076 | import java.io.ObjectOutputStream; 5077 | public class B { 5078 | public static void main(String[] args) { 5079 | A a1 = new A(); 5080 | try { 5081 | FileOutputStream fos = new FileOutputStream("G:\\april\\file.ser"); 5082 | ObjectOutputStream oos = new ObjectOutputStream(fos); 5083 | oos.writeObject(a1); 5084 | 5085 | oos.close(); 5086 | fos.close(); 5087 | } catch (Exception e) { 5088 | 5089 | e.printStackTrace(); 5090 | } 5091 | 5092 | } 5093 | } 5094 | 5095 | package p1; 5096 | import java.io.FileInputStream; 5097 | import java.io.ObjectInputStream; 5098 | public class C { 5099 | public static void main(String[] args) { 5100 | try { 5101 | FileInputStream fis = new FileInputStream("G:\\april\\file.ser"); 5102 | ObjectInputStream ois = new ObjectInputStream(fis); 5103 | A a1 = (A)ois.readObject(); 5104 | System.out.println(a1.name); 5105 | System.out.println(a1.password); 5106 | 5107 | ois.close(); 5108 | 5109 | 5110 | } catch (Exception e) { 5111 | 5112 | e.printStackTrace(); 5113 | } 5114 | 5115 | } 5116 | } 5117 | 5118 | ########################################## 5119 | JDBC - Java Database Connnectivity 5120 | ########################################## 5121 | -> Database: Here we store data in the form of tables permanently 5122 | -> Popular Databases: MySQl, Postgres, Oracle, SQlServer, MongoDB, Derby, h2Database 5123 | -> Install MySql Database 5124 | Download from here: https://dev.mysql.com/downloads/windows/installer/8.0.html 5125 | -> When you download MySql Workbench you will get the following 5126 | a. IDE - To Write SQl Query (Structured Query Language) 5127 | (SQl query is use to interact with the database) 5128 | b. MySQl Server 5129 | 5130 | Installation Doc: https://www.prowesstics.com/blogs/mysql-workbench-installation/ 5131 | 5132 | -> Launch Mysqlworkbench 5133 | -> Create psaDB connection 5134 | -> Create Database: Create Database psaDB 5135 | -> Connect to Database: use psaDB 5136 | -> Create Table Student: 5137 | Create table student( 5138 | name varchar(45), 5139 | email varchar(128), 5140 | mobile varchar(10) 5141 | ) 5142 | -> Read Table Content: Select * from student 5143 | -> Insert data to table: insert into student values('mike','mike@gmail.com','9632629033') 5144 | -> Read Table Content: Select * from student 5145 | 5146 | Assignment: Complete SQL Recorded Classes. Access will be given in PSA App. Nearly covered 250+ examples 5147 | 5148 | After Installing MySQl Workbench, We can now start with JDBC Concept 5149 | ------------------------------------------------------------------------ 5150 | 5151 | Example 1: JDBC code to insert data 5152 | ----------------------------------- 5153 | package jdbc_examples; 5154 | 5155 | import java.sql.Connection; 5156 | import java.sql.DriverManager; 5157 | import java.sql.Statement; 5158 | 5159 | public class A { 5160 | public static void main(String[] args) { 5161 | try { 5162 | //Connect to database - use psaDB (SQL) 5163 | 5164 | Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/psadb1","root","test"); 5165 | 5166 | //Write & execute SQl query 5167 | Statement stmnt = con.createStatement(); 5168 | stmnt.executeUpdate("insert into student values('adam','adam@gmail.com','9632629555')"); 5169 | 5170 | //Close database connection 5171 | con.close(); 5172 | } catch (Exception e) { 5173 | e.printStackTrace(); 5174 | } 5175 | } 5176 | 5177 | } 5178 | 5179 | Example 2: JDBC Code to delete a record 5180 | --------------------------------------- 5181 | import java.sql.Connection; 5182 | import java.sql.DriverManager; 5183 | import java.sql.Statement; 5184 | 5185 | public class B { 5186 | public static void main(String[] args) { 5187 | try { 5188 | //Connect to database - use psaDB (SQL) 5189 | Connection con = 5190 | DriverManager. 5191 | getConnection("jdbc:mysql://localhost:3306/psadb1","root","test"); 5192 | 5193 | //Write & execute SQl query 5194 | 5195 | Statement stmnt = con.createStatement(); 5196 | stmnt.executeUpdate("Delete from student where email='adam@gmail.com'"); 5197 | 5198 | //Close database connection 5199 | con.close(); 5200 | } catch (Exception e) { 5201 | e.printStackTrace(); 5202 | } 5203 | } 5204 | 5205 | } 5206 | 5207 | Example 3: JDBC Code to update Record 5208 | ------------------------------------- 5209 | import java.sql.*; 5210 | 5211 | public class C { 5212 | public static void main(String[] args) { 5213 | try { 5214 | //Step Connect to Database 5215 | Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/psadb1", "root", "test"); 5216 | 5217 | //Step 2: Execute SQl Query 5218 | Statement stmnt = con.createStatement(); 5219 | stmnt.executeUpdate("Update student set mobile='9632882052' where email='mike@gmail.com'"); 5220 | 5221 | //Step 3: Close Database Connection 5222 | con.close(); 5223 | } catch (Exception e) { 5224 | e.printStackTrace(); 5225 | } 5226 | } 5227 | } 5228 | 5229 | Example 4: JDBC Code to read data from database 5230 | ----------------------------------------------- 5231 | package p1; 5232 | 5233 | import java.sql.*; 5234 | 5235 | public class D { 5236 | public static void main(String[] args) { 5237 | try { 5238 | //Step Connect to Database 5239 | Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/psadb1", "root", "test"); 5240 | 5241 | //Step 2: Execute SQl Query 5242 | Statement stmnt = con.createStatement(); 5243 | ResultSet result = stmnt.executeQuery("Select * from student"); 5244 | 5245 | while(result.next()) { 5246 | System.out.println(result.getString(1)); 5247 | System.out.println(result.getString(2)); 5248 | System.out.println(result.getString(3)); 5249 | } 5250 | 5251 | //Step 3: Close Database Connection 5252 | con.close(); 5253 | } catch (Exception e) { 5254 | e.printStackTrace(); 5255 | } 5256 | } 5257 | } 5258 | 5259 | ############################################### 5260 | Custom Exceptions in java 5261 | ############################################## 5262 | "throw keyword is used to throw custom exceptions/builtin exception explicitly" 5263 | 5264 | Example 1: 5265 | package app_java_exceptions; 5266 | 5267 | public class InsufficientFunds extends RuntimeException{ 5268 | public InsufficientFunds(String msg) { 5269 | super(msg); 5270 | } 5271 | } 5272 | package app_java_exceptions; 5273 | 5274 | import java.util.Scanner; 5275 | 5276 | public class A { 5277 | public static void main(String[] args) { 5278 | int balance = 250; 5279 | Scanner scan = new Scanner(System.in); 5280 | System.out.println("Enter the amount"); 5281 | int amount = scan.nextInt(); 5282 | if(balance It will throw method / constructor exceptions to calling statement / compiler 5331 | 5332 | package app_java_exceptions; 5333 | 5334 | import java.io.FileReader; 5335 | 5336 | public class A { 5337 | A() throws Exception{ 5338 | FileReader fr = new FileReader("G:\\test\\t1.txt"); 5339 | } 5340 | public static void main(String[] args) { 5341 | 5342 | try { 5343 | A a1 = new A(); 5344 | a1.test(); 5345 | } catch (Exception e) { 5346 | e.printStackTrace(); 5347 | } 5348 | } 5349 | 5350 | public void test() throws Exception { 5351 | FileReader fr = new FileReader("G:\\test\\t1.txt"); 5352 | } 5353 | } 5354 | 5355 | Example 2 5356 | ######### 5357 | package app_java_exceptions; 5358 | 5359 | import java.io.FileReader; 5360 | 5361 | public class A { 5362 | A() throws Exception{ 5363 | FileReader fr = new FileReader("G:\\test\\t1.txt"); 5364 | } 5365 | public static void main(String[] args) throws Exception { 5366 | A a1 = new A(); 5367 | a1.test(); 5368 | } 5369 | 5370 | public void test() throws Exception { 5371 | FileReader fr = new FileReader("G:\\test\\t1.txt"); 5372 | } 5373 | } 5374 | 5375 | Multi catch blocks: 5376 | --------------------- 5377 | 5378 | -> We can create multiple catch block. 5379 | -> Always start with child exception class followed by parent exception class 5380 | 5381 | Example: 5382 | --------- 5383 | package p1; 5384 | 5385 | public class E { 5386 | int x = 10; 5387 | public static void main(String[] args) { 5388 | try { 5389 | Integer.parseInt("ahsjkdhas687"); 5390 | E a1 = null; 5391 | System.out.println(a1.x); 5392 | int x = 10/0; 5393 | }catch (ArithmeticException e) { 5394 | System.out.println(1); 5395 | }catch (NullPointerException e) { 5396 | System.out.println(2); 5397 | }catch (Exception e) { 5398 | System.out.println(3); 5399 | } 5400 | 5401 | } 5402 | } 5403 | 5404 | 5405 | finally block in java 5406 | ########################### 5407 | 5408 | -> this is extension of try catch block 5409 | -> The code that we write in finally block will run 100%, regardless of exception 5410 | 5411 | Example: 5412 | -------- 5413 | public class E { 5414 | int x = 10; 5415 | public static void main(String[] args) { 5416 | try { 5417 | int x = 100/0; 5418 | }catch (Exception e) { 5419 | e.printStackTrace(); 5420 | }finally { 5421 | System.out.println("Finally"); 5422 | } 5423 | 5424 | } 5425 | } 5426 | Output: Finally 5427 | 5428 | Example: 5429 | -------- 5430 | public class E { 5431 | int x = 10; 5432 | public static void main(String[] args) { 5433 | try { 5434 | int x = 100/2; 5435 | }catch (Exception e) { 5436 | e.printStackTrace(); 5437 | }finally { 5438 | System.out.println("Finally"); 5439 | } 5440 | 5441 | } 5442 | } 5443 | Output: Finally 5444 | 5445 | Example: Can we write only try & catch block: yes 5446 | -------- 5447 | public class E { 5448 | int x = 10; 5449 | public static void main(String[] args) { 5450 | try { 5451 | int x = 100/0; 5452 | }finally { 5453 | System.out.println("Finally"); 5454 | } 5455 | 5456 | System.out.println("Welcome"); 5457 | 5458 | } 5459 | } 5460 | Output: 5461 | Finally 5462 | Exception in thread "main" java.lang.ArithmeticException: / by zero 5463 | at jdbc_examples.E.main(E.java:7) 5464 | 5465 | Note: Can u give practical example where finally block can be used? 5466 | Ans: 5467 | a. We can peform file closing operation / Database closing Operation 5468 | 5469 | Example : 5470 | package p1; 5471 | 5472 | import java.sql.*; 5473 | 5474 | public class A { 5475 | public static void main(String[] args) { 5476 | Connection con = null; 5477 | try { 5478 | //Step Connect to Database 5479 | con = DriverManager.getConnection("jdbc:mysql://localhost:3306/psadb1", "root", "test"); 5480 | 5481 | //Step 2: Execute SQl Query 5482 | Statement stmnt = con.createStatement(); 5483 | stmnt.executeUpdate("insert into student values('adam','adam@gmail.com','9632629455')"); 5484 | 5485 | 5486 | } catch (Exception e) { 5487 | e.printStackTrace(); 5488 | }finally { 5489 | try { 5490 | //Step 3: Close Database Connection 5491 | con.close(); 5492 | } catch (Exception e2) { 5493 | e2.printStackTrace(); 5494 | } 5495 | } 5496 | } 5497 | } 5498 | 5499 | Interview Question: 5500 | a. Difference between final versus finally versus finalize 5501 | ============================================================= 5502 | final 5503 | -------- 5504 | -> final makes variable constant 5505 | -> final prevents overriding of methods 5506 | -> final on class stops inheritance 5507 | 5508 | finally 5509 | ------- 5510 | -> this is extension of try catch block 5511 | -> The code that we write in finally block will run 100%, regardless of exception 5512 | 5513 | finalize(): 5514 | ---------- 5515 | -> This is a method inside Object class and here Garbage collection logic is implemented 5516 | -> We can try calling garbage collector using System.gc(). But will not guarantee 100% execution. 5517 | 5518 | ######################### 5519 | Encapsulation in Java 5520 | ######################### 5521 | -> Encapsulation is one of the four fundamental Object-Oriented Programming (OOP) principles in Java (along with inheritance, polymorphism, and abstraction). 5522 | -> It is the mechanism of wrapping variables and methods together as a single unit and restricting direct access to variables. 5523 | 5524 | How to perform Encapsulation: 5525 | ------------------------------- 5526 | a. Data hiding – Fields (variables) of a class are made private so they cannot be accessed directly from outside the class. 5527 | 5528 | Public getter and setter methods – These methods are provided to access and update the value of private fields. 5529 | 5530 | Example : 5531 | ----------- 5532 | public class A { 5533 | //Data hiding 5534 | private int x; 5535 | private int y; 5536 | public int getX() { 5537 | return x; 5538 | } 5539 | public void setX(int x) { 5540 | this.x = x; 5541 | } 5542 | public int getY() { 5543 | return y; 5544 | } 5545 | public void setY(int y) { 5546 | this.y = y; 5547 | } 5548 | 5549 | } 5550 | 5551 | 5552 | public class B { 5553 | public static void main(String[] args) { 5554 | A a1 = new A(); 5555 | a1.setX(100); 5556 | a1.setY(200); 5557 | System.out.println(a1.getX()); 5558 | System.out.println(a1.getX()); 5559 | } 5560 | } 5561 | 5562 | ##################### 5563 | Threads in java: - Assignment 5564 | ################## 5565 | https://www.youtube.com/watch?v=Uw1tocKJNZw&list=PLbLiFkJgsmq6j_fFlZuy0xIs6upvrML26&index=44 5566 | 5567 | 5568 | 5569 | 5570 | 5571 | 5572 | 5573 | 5574 | 5575 | 5576 | --------------------------------------------------------------------------------