├── Access_Modifiers.java ├── Classes_Objects_and_Constructors.java ├── Inheritence.java ├── Interfaces.java ├── Polymorphism.java ├── README.md └── all in one.java /Access_Modifiers.java: -------------------------------------------------------------------------------- 1 | public class Access_Modifiers{ 2 | public static void main(String args[]){ 3 | 4 | // creating object of Account class 5 | Account a1 = new Account(); 6 | System.out.println(a1.getPassword()); 7 | 8 | } 9 | } 10 | 11 | /* --- Access Modifiers --- */ 12 | class Account{ 13 | 14 | public String name; // visible to everyone 15 | 16 | protected String email; // visible to only this package 17 | 18 | private int password; // visible to only base-class(Account) 19 | 20 | String abc; // (default access modifiers) visible to only OOPs class 21 | 22 | // getters 23 | public int getPassword(){ 24 | setPassword(1234); 25 | return this.password; 26 | } 27 | 28 | // setters 29 | private void setPassword(int pass){ 30 | this.password = pass; 31 | } 32 | 33 | public void printInfo(){ 34 | System.out.println("this is the name : " + " " + this.name); 35 | System.out.println("this is the email : " + " " + this.email); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /Classes_Objects_and_Constructors.java: -------------------------------------------------------------------------------- 1 | public class Classes_Objects_and_Constructors{ 2 | public static void main(String args[]){ 3 | 4 | // using static variable 5 | Car.name = "this is the static keyword"; 6 | 7 | // creating object of Car class 8 | Car c1 = new Car(); 9 | c1.brand = "BMW"; 10 | c1.prise(); 11 | c1.name(); 12 | 13 | // creating 2nd object of Car class 14 | Car c2 = new Car(); 15 | c2.name(); 16 | 17 | } 18 | } 19 | 20 | /* --- Classes, Objects, Constructors and Encapsulation --- */ 21 | class Car{ 22 | 23 | // variable with static keyword 24 | static String name; 25 | 26 | int prise; 27 | String color; 28 | String brand; 29 | 30 | // constructor of Car class 31 | Car(){ 32 | System.out.println("this is the constructor of Car class"); 33 | } 34 | 35 | void name(){ 36 | System.out.println(name); 37 | } 38 | 39 | void prise(){ 40 | switch(brand){ 41 | case "audi": 42 | prise = 20000; 43 | System.out.println(prise); 44 | break; 45 | case "BMW": 46 | prise = 30000; 47 | System.out.println(prise); 48 | break; 49 | case "lamborghini": 50 | prise = 1; 51 | System.out.println(prise); 52 | break; 53 | default: 54 | break; 55 | } 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /Inheritence.java: -------------------------------------------------------------------------------- 1 | public class Inheritence{ 2 | public static void main(String args[]){ 3 | 4 | // creating object of Student class 5 | Student s1 = new Student(); 6 | s1.name = "kumkum"; 7 | s1.walking(); 8 | s1.playing(); 9 | s1.studying(); 10 | 11 | // creating object of Boy class 12 | Boy b1 = new Boy(); 13 | b1.name = "abhi"; 14 | b1.walking(); 15 | 16 | // creating object os Child class 17 | Child cc = new Child(); 18 | 19 | } 20 | } 21 | 22 | /* --- INHERITENCE --- */ 23 | 24 | // no class can inherit "FinalClass" class because of the final keyword! 25 | final class FinalClass extends Man{ 26 | void main(){ 27 | System.out.println("hello world!"); 28 | } 29 | } 30 | 31 | // base class or parant class 32 | class Man{ 33 | 34 | String name; 35 | 36 | void man(){ 37 | System.out.println("this is man of Man"); 38 | } 39 | 40 | void walking(){ 41 | System.out.println(this.name + " is walking"); 42 | } 43 | } 44 | 45 | // drived(child) class for 'Man' and parant class for 'Student' 46 | class Child extends Man{ 47 | 48 | Child(){ 49 | // the super keyword 50 | super.man(); 51 | } 52 | 53 | void playing(){ 54 | System.out.println(this.name + " is playing"); 55 | } 56 | 57 | @Override 58 | void man(){ 59 | System.out.println("this is man of child"); 60 | } 61 | 62 | } 63 | 64 | // drived(child) class for 'Child' class 65 | class Student extends Child{ 66 | 67 | void studying(){ 68 | System.out.println(this.name + " " + " is studying"); 69 | } 70 | 71 | } 72 | 73 | // drived(child) class for 'Man' class 74 | class Boy extends Man{ 75 | 76 | } -------------------------------------------------------------------------------- /Interfaces.java: -------------------------------------------------------------------------------- 1 | public class Interfaces{ 2 | public static void main(String args[]){ 3 | 4 | // creating object of Boy class 5 | Boy b = new Boy(); 6 | b.walk(); 7 | 8 | } 9 | } 10 | 11 | /* --- INTERFACES --- */ 12 | 13 | interface Human{ 14 | String name2 = "abc"; 15 | int eyes = 2; 16 | } 17 | 18 | interface Man2{ 19 | void walk(); 20 | } 21 | 22 | class Boy2 implements Human, Man2{ 23 | 24 | @Override 25 | public void walk(){ 26 | System.out.println("boy2 is walking"); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Polymorphism.java: -------------------------------------------------------------------------------- 1 | public class Polymorphism{ 2 | public static void main(String args[]){ 3 | 4 | // creating object of Robot class 5 | Robot r1 = new Robot(); 6 | 7 | r1.name = "abhi"; 8 | r1.speed = 120; 9 | 10 | r1.printInfo(r1.speed); 11 | r1.printInfo(r1.name); 12 | r1.printInfo(r1.speed, r1.name); 13 | 14 | } 15 | } 16 | 17 | /* --- POLYMORPHISM --- */ 18 | class Robot{ 19 | 20 | String name; 21 | int speed; 22 | 23 | void printInfo(int speed){ 24 | System.out.println(this.speed); 25 | } 26 | 27 | void printInfo(String name){ 28 | System.out.println(this.name); 29 | } 30 | 31 | void printInfo(int speed, String name){ 32 | System.out.println(this.speed + " " + this.name); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-OOPS 2 | All OOPS concepts in java 3 | -------------------------------------------------------------------------------- /all in one.java: -------------------------------------------------------------------------------- 1 | public class OOPS{ 2 | public static void main(String args[]){ 3 | 4 | System.out.println("-----------------classes, objects and constructors"); 5 | // classes, objects, constructors and Encapsulations 6 | Car.name = "this is the static keyword"; 7 | Car c1 = new Car(); 8 | c1.brand = "BMW"; 9 | c1.prise(); 10 | c1.name(); 11 | Car c2 = new Car(); 12 | c2.name(); 13 | 14 | 15 | System.out.println("-----------------Polymorphism"); 16 | // Polymorphism 17 | Robot r1 = new Robot(); 18 | r1.name = "abhi"; 19 | r1.speed = 120; 20 | r1.printInfo(r1.speed); 21 | r1.printInfo(r1.name); 22 | r1.printInfo(r1.speed, r1.name); 23 | 24 | 25 | System.out.println("-----------------Inheritence"); 26 | // Inheritence 27 | Student s1 = new Student(); 28 | s1.name = "kumkum"; 29 | s1.walking(); 30 | s1.playing(); 31 | s1.studying(); 32 | Boy b1 = new Boy(); 33 | b1.name = "abhi"; 34 | b1.walking(); 35 | Child cc = new Child(); 36 | 37 | 38 | System.out.println("-----------------access modifiers"); 39 | // access modifiers 40 | Account a1 = new Account(); 41 | System.out.println(a1.getPassword()); 42 | 43 | 44 | System.out.println("-----------------Interfaces"); 45 | // Interfaces 46 | Boy2 b2 = new Boy2(); 47 | b2.walk(); 48 | 49 | } 50 | } 51 | 52 | 53 | // classes, objects and constructors 54 | class Car{ 55 | static String name; 56 | int prise; 57 | String color; 58 | String brand; 59 | Car(){ 60 | System.out.println("this is the constructor of Car class"); 61 | } 62 | void name(){ 63 | System.out.println(name); 64 | } 65 | void prise(){ 66 | switch(brand){ 67 | case "audi": 68 | prise = 20000; 69 | System.out.println(prise); 70 | break; 71 | case "BMW": 72 | prise = 30000; 73 | System.out.println(prise); 74 | break; 75 | case "lamborghini": 76 | prise = 1; 77 | System.out.println(prise); 78 | break; 79 | default: 80 | break; 81 | } 82 | } 83 | } 84 | 85 | /* POLYMORPHISM */ 86 | class Robot{ 87 | String name; 88 | int speed; 89 | 90 | void printInfo(int speed){ 91 | System.out.println(this.speed); 92 | } 93 | void printInfo(String name){ 94 | System.out.println(this.name); 95 | } 96 | void printInfo(int speed, String name){ 97 | System.out.println(this.speed + " " + this.name); 98 | } 99 | } 100 | 101 | 102 | /* INHERITENCE */ 103 | final class FinalClass extends Man{ // no class can inherit "FinalClass" 104 | class because of the final keyword! 105 | void main(){ 106 | System.out.println("hello world!"); 107 | } 108 | } 109 | class Man{ // base class or parant class 110 | String name; 111 | void man(){ 112 | System.out.println("this is man of Man"); 113 | } 114 | void walking(){ 115 | System.out.println(this.name + " is walking"); 116 | } 117 | } 118 | class Child extends Man{ // drived(child) class for 'Man' and parant class for 'Student' 119 | Child(){ 120 | super.man(); // the super keyword 121 | } 122 | void playing(){ 123 | System.out.println(this.name + " is playing"); 124 | } 125 | @Override 126 | void man(){ 127 | System.out.println("this is man of child"); 128 | } 129 | } 130 | class Student extends Child{ // drived(child) class for 'Child' class 131 | void studying(){ 132 | System.out.println(this.name + " " + " is studying"); 133 | } 134 | } 135 | class Boy extends Man{ // drived(child) class for 'Man' class 136 | 137 | } 138 | 139 | 140 | /* Access Modifiers */ 141 | class Account{ 142 | public String name; // visible to everyone 143 | protected String email; // visible to only this package 144 | private int password; // visible to only base-class(Account) 145 | String abc; // (default access modifiers) visible to only OOPs class 146 | 147 | // getters and setters 148 | public int getPassword(){ 149 | setPassword(1234); 150 | return this.password; 151 | } 152 | private void setPassword(int pass){ 153 | this.password = pass; 154 | } 155 | public void printInfo(){ 156 | System.out.println("this is the name : " + " " + this.name); 157 | System.out.println("this is the email : " + " " + this.email); 158 | } 159 | } 160 | 161 | 162 | /* ENCAPSULATION */ 163 | // it is already came when we create a object 164 | 165 | 166 | 167 | /* INTERFACES */ 168 | interface Human{ 169 | String name2 = "abc"; 170 | int eyes = 2; 171 | 172 | } 173 | interface Man2{ 174 | void walk(); 175 | } 176 | class Boy2 implements Human, Man2{ 177 | @Override 178 | public void walk(){ 179 | System.out.println("boy2 is walking"); 180 | } 181 | } 182 | --------------------------------------------------------------------------------