{"id":26384442,"url":"https://github.com/sabberrahman/java-oop","last_synced_at":"2025-09-22T08:24:18.950Z","repository":{"id":282754390,"uuid":"924477284","full_name":"sabberrahman/Java-OOP","owner":"sabberrahman","description":"understanding java and OOP principles","archived":false,"fork":false,"pushed_at":"2025-03-16T18:11:35.000Z","size":78,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T19:27:21.689Z","etag":null,"topics":["java","oop","oops-in-java","polymorphism","threads"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sabberrahman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-30T04:32:29.000Z","updated_at":"2025-03-16T18:11:38.000Z","dependencies_parsed_at":"2025-03-16T19:27:24.435Z","dependency_job_id":"2cc90415-ba61-4cd4-abf9-13f8c211b48c","html_url":"https://github.com/sabberrahman/Java-OOP","commit_stats":null,"previous_names":["sabberrahman/java-oop"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sabberrahman/Java-OOP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabberrahman%2FJava-OOP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabberrahman%2FJava-OOP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabberrahman%2FJava-OOP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabberrahman%2FJava-OOP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sabberrahman","download_url":"https://codeload.github.com/sabberrahman/Java-OOP/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabberrahman%2FJava-OOP/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276369587,"owners_count":25630250,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-22T02:00:08.972Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","oop","oops-in-java","polymorphism","threads"],"created_at":"2025-03-17T07:29:21.149Z","updated_at":"2025-09-22T08:24:18.929Z","avatar_url":"https://github.com/sabberrahman.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Variables\n```java\ntype variableName = value;\n\nint myNum = 5;\nfloat myFloatNum = 5.99f;\nchar myLetter = 'A';\nboolean myBool = false;\nString myText = \"Hello, world\";\n\n```\n\n## Data Types\n\n### Primitive Data Types\n\n- `byte`, `short`, `int`, `long`, `float`, `double`, `boolean`, `char`\n    \n- `float`: Stores fractional numbers (6-7 decimal digits).\n    \n- `double`: Stores fractional numbers (15-16 decimal digits).\n    \n\n### Non-Primitive Data Types\n\n- `String`, `Array`, `Classes`\n    \n- Non-primitive types are called **reference types** because they refer to objects.\n    \n- Primitive types always hold a value, whereas non-primitive types can be `null`.\n    \n- Primitive types start with a lowercase letter (e.g., `int`), while non-primitive types start with an uppercase letter (e.g., `String`, `ArrayList`).\n    \n- Non-primitive types can be used to call methods, whereas primitive types cannot.\n    \n\n---\n\n## Type Casting\n\n### Widening Casting (Automatic)\n\n- Converts a smaller type to a larger type size.\n    \n- Order: `byte` -\u003e `short` -\u003e `char` -\u003e `int` -\u003e `long` -\u003e `float` -\u003e `double`.\n\n```java\n\nint myInt = 9;\ndouble myDouble = myInt; // Automatic casting: int to double\n```\n\n### Narrowing Casting (Manual)\n\n- Converts a larger type to a smaller size type.\n    \n- Order: `double` -\u003e `float` -\u003e `long` -\u003e `int` -\u003e `char` -\u003e `short` -\u003e `byte`.\n    \n\n```java\ndouble myDouble = 9.78d;\nint myInt = (int) myDouble; // Manual casting: double to int\n\nSystem.out.println(myDouble); // Outputs 9.78\nSystem.out.println(myInt);    // Outputs 9\n\n```\n---\n\n\n## Operators\n\n### Ternary (Conditional) Operator\n\n- A shorthand for `if-else` statements.\n    \n- Syntax: `condition ? value_if_true : value_if_false`.\n    \n\n\n```java\nint time = 20;\nString result = (time \u003c 18) ? \"Good day.\" : \"Good evening.\";\nSystem.out.println(result);\n```\n---\n\n### Modulus Operator\n\n```java\nfloat x = 5; // auto type casting will occur\nint y = 2;\nSystem.out.println(x % y); // Outputs 1\n```\n\n---\n\n## Conditional Statements and Loops\n\n### If-Else\n\n```java\n\nint myAge = 25;\nint votingAge = 18;\n\nif (myAge \u003e= votingAge) {\n  System.out.println(\"Old enough to vote!\");\n} else {\n  System.out.println(\"Not old enough to vote.\");\n}\n```\n### Switch Statement\n\n\n```java\nint day = 4;\nswitch (day) {\n  case 1:\n    System.out.println(\"Monday\");\n    break;\n  case 2:\n    System.out.println(\"Tuesday\");\n    break;\n  case 3:\n    System.out.println(\"Wednesday\");\n    break;\n  case 4:\n    System.out.println(\"Thursday\");\n    break;\n  default:\n    System.out.println(\"Friday - Sunday\");\n}\n```\n### Loops\n\n#### While Loop\n\n```java\n\nint i = 0;\nwhile (i \u003c 5) {\n  System.out.println(i);\n  i++;\n}\n\n#### Do-While Loop\n\n## do while \n\nint i = 0;\ndo {\n  System.out.println(i);\n  i++;\n} while (i \u003c 5);\n\n#### For Loop\n\n## for\n\nfor (int i = 0; i \u003c 5; i++) {\n  System.out.println(i);\n}\n\n#### Nested For Loop\n\n## nested for loop\n\nfor (int i = 1; i \u003c= 2; i++) {\n  System.out.println(\"Outer: \" + i);\n  for (int j = 1; j \u003c= 3; j++) {\n    System.out.println(\" Inner: \" + j);\n  }\n}\n```\n#### For-Each Loop\n\n```java\nString[] cars = {\"Volvo\", \"BMW\", \"Ford\", \"Mazda\"};\nfor (String car : cars) {\n  System.out.println(car);\n}\n```\n#### Break and Continue\n\n```java\n\nfor (int i = 0; i \u003c 10; i++) {\n  if (i == 4) {\n    break; // Stops the loop when i is 4\n  }\n  System.out.println(i);\n}\n\nfor (int i = 0; i \u003c 10; i++) {\n  if (i == 4) {\n    continue; // Skips the iteration when i is 4\n  }\n  System.out.println(i);\n}\n```\n---\n\n## Object-Oriented Programming (OOP)\n\n### Classes and Objects\n\n```java\n\npublic class Main {\n  int x;\n\n  public static void main(String[] args) {\n    Main myObj = new Main();\n    myObj.x = 40;\n    System.out.println(myObj.x);\n  }\n}\n```\n### Static vs. Public\n\n- **Static**: Methods/attributes belong to the class, not the object. Can be called without creating an object.\n    \n- **Public**: Methods/attributes must be called by creating an object.\n    \n- **Private**: Accessible only within the declared class.\n    \n- **Final**: Attributes/methods cannot be overridden/modified.\n    \n\n```java\n\nmyStaticMethod(); // Call the static method\nMain myObj = new Main(); // Create an object of Main\nmyObj.myPublicMethod(); // Call the public method\n```\n### Constructors\n\n```java\n\npublic class Main {\n  int x;\n\n  public Main(int num) {\n    this.x = num;\n  }\n\n  public static void main(String[] args) {\n    Main myObj = new Main(5);\n    System.out.println(myObj.x); // Outputs 5\n  }\n}\n```\n\n### Encapsulation\n\n- Hides sensitive data by making class variables private and providing public getter and setter methods.\n    \n\n```java\n\npublic class Person {\n  private String name; // Private attribute\n\n  // Getter\n  public String getName() {\n    return name;\n  }\n\n  // Setter\n  public void setName(String newName) {\n    this.name = newName;\n  }\n}\n```\n### Inheritance\n\n- Allows a class to inherit attributes and methods from another class using the `extends` keyword.\n    \n\n```java\n\nclass Vehicle {\n  protected String brand = \"Ford\";\n\n  public void honk() {\n    System.out.println(\"Tuut, tuut!\");\n  }\n}\n\nclass Car extends Vehicle {\n  public static void main(String[] args) {\n    Car myFastCar = new Car();\n    myFastCar.honk(); // Outputs \"Tuut, tuut!\"\n  }\n}\n```\n### Polymorphism\n\n- Allows methods/objects to behave differently based on the context.\n    \n\n```java\n\nclass Animal {\n  void sound() {\n    System.out.println(\"UAAAAAAAAAH\");\n  }\n}\n\nclass Dog extends Animal {\n  void sound() {\n    System.out.println(\"BARK BARK\");\n  }\n}\n\nclass Cat extends Animal {\n  void sound() {\n    System.out.println(\"Meow, meow\");\n  }\n}\n\npublic class Main {\n  public static void main(String[] args) {\n    Animal[] animals = {new Dog(), new Cat()};\n    for (Animal animal : animals) {\n      animal.sound(); // Polymorphic behavior\n    }\n  }\n}\n```\n### Abstraction\n\n- Hides certain details and shows only essential information using abstract classes/methods.\n    \n\n```java\nabstract class Animal {\n  public abstract void animalSound(); // Abstract method\n  public void sleep() {\n    System.out.println(\"Zzz\");\n  }\n}\n\nclass Pig extends Animal {\n  public void animalSound() {\n    System.out.println(\"The pig says: wee wee\");\n  }\n}\n\nclass Main {\n  public static void main(String[] args) {\n    Pig myPig = new Pig();\n    myPig.animalSound();\n    myPig.sleep();\n  }\n}\n```\n### Interfaces\n\n- Another way to achieve abstraction. Contains methods without a body.\n    \n\n```java\n\ninterface Animal {\n  public void animalSound();\n  public void run();\n}\n\nclass Pig implements Animal {\n  public void animalSound() {\n    System.out.println(\"The pig says: wee wee\");\n  }\n  public void run() {\n    System.out.println(\"Pig is running\");\n  }\n}\n\nclass Main {\n  public static void main(String[] args) {\n    Pig myPig = new Pig();\n    myPig.animalSound();\n    myPig.run();\n  }\n}\n```\n---\n\n## Arrays and ArrayLists\n\n### Arrays\n\n```java\n\nString[] cars = {\"Volvo\", \"BMW\", \"Ford\", \"Mazda\"};\nint[] myNum = {10, 20, 40, 60};\n\nfor (int num : myNum) {\n  System.out.println(num);\n}\n```\n### ArrayList\n\n- Resizable array that allows adding/removing elements.\n    \n\n```java\n\nimport java.util.ArrayList;\n\nArrayList\u003cString\u003e cars = new ArrayList\u003cString\u003e();\ncars.add(\"BMW\");\ncars.get(0);\ncars.set(0, \"OPEL\");\ncars.remove(0);\n```\n---\n\n## Exception Handling\n\n```java\n\ntry {\n  // Block of code to try\n} catch (Exception e) {\n  // Block of code to handle errors\n}\n```\n---\n\nAdditional Notes\n\n **Method Overloading**: Multiple methods can have the same name with different parameters.\n    \n- **Lambda Expressions**: Short blocks of code that take parameters and return a value.\n    \n- **Aggregation**: Represents a HAS-A relationship between classes.\n    \n- **Final Methods**: Cannot be overridden in subclasses.\n    \n- **Static Methods**: Cannot be overridden but can be hidden.\n    \n- **Private Methods**: Not inherited by subclasses and cannot be overridden.\n    \n- **Super Keyword**: Used to refer to the immediate parent class.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabberrahman%2Fjava-oop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsabberrahman%2Fjava-oop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabberrahman%2Fjava-oop/lists"}