https://github.com/pratikrameshmajage/java
📚 Learn and Master OOP Concepts in Java! This repository is a comprehensive collection of Object-Oriented Programming (OOP) examples, projects, and practice problems written in Java. Perfect for students, developers, and enthusiasts looking to sharpen their Java skills. 🖥️💡
https://github.com/pratikrameshmajage/java
dependency-injection lang-java static super this
Last synced: 9 months ago
JSON representation
📚 Learn and Master OOP Concepts in Java! This repository is a comprehensive collection of Object-Oriented Programming (OOP) examples, projects, and practice problems written in Java. Perfect for students, developers, and enthusiasts looking to sharpen their Java skills. 🖥️💡
- Host: GitHub
- URL: https://github.com/pratikrameshmajage/java
- Owner: PratikRameshMajage
- Created: 2024-10-22T07:20:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-15T18:03:35.000Z (11 months ago)
- Last Synced: 2025-03-27T08:45:20.953Z (10 months ago)
- Topics: dependency-injection, lang-java, static, super, this
- Language: Java
- Homepage:
- Size: 10.2 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# `Java Notes:`
# `Classes & Objects:`
- **OOP: Object Oriented Programming:** Style of Writing Code.
- **Objects:** Entities in Real World.
- **Casses:** Group of these Entities | Collection of Real World Objects | BluePrint of on object | has Attributes & Methods | ex: Pen
- **Attributes:** Properties | ex: color, thickness
- **Methods:** Functions Which is use in the Class | Behaviours | ex: setColor(), setTip()
- **Java Code Writing Convention: Traditional Java Developers Follow this Convention**
- Always Make Classes After Public Class.
- File_Name & Public_Class_Name Should be Same.
- **Class** name Should be Start With **Capital Letter**.
- **Methords** name Should be Start with **Small Letter** - **CamelCase**.
# `Code:`
```java
public class OOP{
// Compiler Always Start Execution From Main Method:
// Public: Access Specifier.
// Static: Without Creating Object to Use Main Method.
// Void: Return DataType.
public static void main(String args[]){
// Pen: Class.
// P1: Reference Variable.
// new: Memory Allocation.
// Pen(): Pen Class Default Constructor.
// ; : Treminate the Line.
// Created a Pen Object called p1.
// Stack:
// Heap: Object Created in Heap Memory (alocates Memory) by using New Keyword.
Pen p1 = new Pen();
p1.setColor("BLue");
// p1.color = "Green"; We can Also Write in this Way.
System.out.println(p1.color);
p1.setTip(10);
System.out.println(p1.tip);
}
}
// Pen Class
class Pen{
// Properties and Methods:
String color = "Red";
int tip = 5;
void setColor(String newColor){
color = newColor;
}
void setTip(int newTip){
tip = newTip;
}
}
// Student Class
class Student{
String name;
int age;
float percentage;
void calcPercentage(int phy, int che, int math){
percentage = (phy + che + math)/3;
}
}
```
# `Access Modifiers:`
|Access Specifier|Within Class|Within Package|Outside Package BySubClass Only|OutSide Package|
|----------------|------------|--------------|-------------------------------|---------------|
|- **Private:** |Y |NO |NO |NO |
|- **Default:** |Y |Y |NO |NO |
|- **Protected:**|Y |Y |Y |NO |
|- **Public:** |Y |Y |Y |Y |
# `Getter & Setter:`
- Get: To Return the Value.
- Set: To Modify the Value.
- This Keyword is Used to refer to the Current Object.
```java
public class OOP{
public static void main(String args[]) {
Pen p1 = new Pen();
p1.setColor("Red");
p1.setTip(10);
System.out.println(p1.getColor());
System.out.println(p1.getTip());
}
}
class Pen {
private String color;
private int tip;
String getColor(){
return this.color;
}
int getTip(){
return this.tip;
}
void setColor(String newColor){
// you can use this when instance variable and local variable names are same
// color: Instance Variable.
// newColor: Local Variable.
this.color = newColor;
}
void setTip(int newTip){
this.tip = newTip;
}
}
```
# `Encapsulation:`
- **Four Pillers of OOP**.
- **Encpsulation:** Encapsulation is defined as the **wrapper up of Data & Methods ( Properties|Variables & Functions) under a Single Unit. It also Implements Data Hiding (Useless | Sensitive -> Private|Protected|Default ).**
- **Abstraction:**
- **Inheritance:**
- **Polimorphisum:**
# `Constructors:`
- Constructor is a **Special Method** which is **invoked by Automatically at the time of Object Creation.**
- Constructor have the Same name as Class Structure.
- Constructor Don't have Return Type.(Not even void)
- Constructors are Only called Once.
- Memory Allocation happens when Constructor is Called.
- **Types Of Constructor:**
- **Non-Parametrized:**
- **Parametrized:**
- Constructor Overloading - Polymorphisum
- **Copy Constructor:**
- Shallw (On a Surface Changes) Copy: Refrence Copy - Changes Reflect
- Deep (Totally Deep Inside Changes) Copy: New Copy - Changes Not Reflect
```java
public class OOP{
public static void main(String args[]) {
Student s1 = new Student();
Student s2 = new Student("Pratik",22,"12345");
s2.marks[0] = 90;
s2.marks[1] = 95;
s2.marks[2] = 99;
// Copy Constructor:
Student s3 = new Student(s2);
// Beacouse of Array are Referece Variables.
s3.password = "67890";
s2.marks[2] = 12;
for(int i=0; i<3;i++){
System.out.println(s3.marks[i]);
}
}
}
class Student{
String name;
int rollno;
String password;
int marks[];
// Default Constructor | Non-Parametrized Constructor:
Student(){
System.out.println("Student Constructor Called...");
}
// Parametrized Constructor:
Student(String name, int rollno, String password){
this.name = name;
this.rollno = rollno;
this.password = password;
marks = new int[3];
}
// Copy Constructor: Shallw Copy Constructor
// Student(Student s2){
// this.name = s2.name;
// this.rollno = s2.rollno;
// this.marks = s2.marks;
// }
// Deep Copy Constructor:
Student(Student s2){
marks = new int[3];
this.name = s2.name;
this.rollno = s2.rollno;
this.marks = s2.marks;
for(int i =0; i<3; i++){
this.marks[i] = s2.marks[i];
}
}
}
```
# `Destructors:`
- Garbage Collector:
# `Inheritance:`
- Inheritance is when Properties and Methods of **base | Parent | Super** class are passed on **Derived | Child | Sub** Class.
- In Java Multiple Inheritance is Not Exist | Not Possible by Classes. But We Can Achive by using Interface.
- Multiple Base Class - Single Derived Class
## `Single Level Inheritance:`
- Single Base Class to Single Child Class (1 on 1):
- 
```java
// Inheritance
// Singlr Level Inheritance
public class OOP{
public static void main(String[] args) {
Fish shark = new Fish();
shark.eat();
}
}
// BaseCLass
class Animal{
String color;
void eat(){
System.out.println("Eats");
}
void breath(){
System.out.println("Breathes");
}
}
// DerivedClass
class Fish extends Animal{
int fins;
void swim(){
System.out.println("Swims in Water");
}
}
```
## `Multiple Inheritance:`
- Single Base class to Single Child Class Again Inherit by Another Single Child Classes (1 on 1 on 1...):
- 
```java
```java
public class OOP{
public static void main(String[] args) {
Dog dobby = new Dog();
dobby.eat();
dobby.legs = 4;
System.out.println(dobby.legs);
}
}
class Animal{
String color;
void eat(){
System.out.println("Eats");
}
void breathe(){
System.out.println("Breaths");
}
}
class Mammal extends Animal{
int legs;
}
class Dog extends Mammal{
String breed;
}
```
## `Hierarchial Inheritance:`
- Single Base class to Many Child Class (1 on MANY):
- 
```java
public class OOP{
public static void main(String[] args) {
Mammal
}
}
class Animal{
String color;
void eat(){
System.out.println("Eats");
}
void Breath(){
System.out.println("Breathes");
}
}
class Mammal extends Animal{
void walk(){
System.out.println("Walks");
}
}
class Fish extends Animal{
void swim(){
System.out.println("Swims");
}
}
class Bird extends Animal{
void fly(){
System.out.println("Fly");
}
}
```
## `Hybrid Inheritance:`
- Single Base class to Many Child Class Again Inherit by Another Many Child Classes (1 on MANY on Many) Combination of all inheritance:
- 
```java
// Hybrid Inheritance:
public class OOP{
public static void main(String[] args) {
Dog dobby = new Dog();
dobby.eat();
dobby.breath();
dobby.walk();
dobby.bark();
}
}
class Animal{
String color;
void eat(){
System.out.println("Eats");
}
void breath(){
System.out.println("Breathes");
}
}
class Mammal extends Animal{
void walk(){
System.out.println("Walks");
}
}
class Fish extends Animal{
void swim(){
System.out.println("Swims");
}
}
class Bird extends Animal{
void fly(){
System.out.println("Fly");
}
}
class Dog extends Mammal{
void bark(){
System.out.println("Barking");
}
}
```
# `Polymorphism:`
- Poly: Many
- Morph: Forms
- **Compile Time Polymorphism** (Static)
- Methord Overloading: Multiple Functions with the Same Name but Different Parameters.
- **Run Time Polymorphism** (Dynamic)
- Method Overriding: Parent and Child Classes both Contain the Same functions with a Different Defination.
## `Method Overloading:`
- Multiple Functions with the Same Name but Different Parameters.
```java
// Polymorphism:
// Compile Time Polymorphism:
// Method Overloading:
public class OOP{
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.sum(2,3));
// System.out.println(calc.sum(2.2,3.3)); By Default it takes as a Double thts why we have to Type Cast.
System.out.println(calc.sum((float)2.2,(float)3.3));
System.out.println(calc.sum(2,3,4));
}
}
class Calculator{
int sum(int num1, int num2){
return num1+num2;
}
float sum(float num1, float num2){
return num1+ num2;
}
int sum(int num1, int num2, int num3){
return num1+num2+num3;
}
}
```
## `Method Overloading:`
- Parent and Child Classes both Contain the Same functions with a Different Defination.
```java
// Polymorphism:
// Run Time Polymorphism:
// Method Overriding:
public class OOP{
public static void main(String[] args) {
Deer deer = new Deer();
deer.eat();
Animal animal = new Animal();
animal.eat();
}
}
class Animal{
void eat(){
System.out.println("Eats");
}
}
class Deer extends Animal{
void eat(){
System.out.println("Eats Grass");
}
}
```
# `Packages:`
- Packages is a Groupof Similar Types of Classes, Interfaces and Sub-Packages.
- InBuild Packages: java.util.*;
- UserDefined Packages: package myPackage;
# `Abstraction:`
- Hiding all the Unnecessary details and showing only the important parts to the user. Idea & Implimentation
- Abstract Classes
- Interfaces
- Connot create create an instance | Object of Abstract Class.
- Can have Abstract/Non-Abstract Class.
- Can have Constructor.
```java
// Abstract:
public class OOP{
public static void main(String args[]){
Horse hourse = new Horse();
hourse.eat();
hourse.walk();
Peacock peacock = new Peacock();
peacock.eat();
peacock.walk();
Mustang myHorse = new Mustang();
// Animal - Horse - Mustang.
}
}
abstract class Animal{
String color;
Animal(){
color= "White";
System.out.println("Animal Constructor Called.");
}
void eat(){
System.out.println("Animal Eats");
}
abstract void walk(); //Gives Idea and You have to be this Methoed when you extends.
}
class Horse extends Animal{
void changColor(){
color = "Black";
}
Horse(){
System.out.println("Hourse Constructor Called.");
}
void walk(){
System.out.println("Hourse walks on 4 legs.");
}
}
class Mustang extends Horse{
Mustang(){
System.out.println("Mustang Constructor Called.");
}
}
class Peacock extends Animal{
void changColor(){
color = "Purple";
}
void walk(){
System.out.println("Peacock walks on 2 legs.");
}
}
// Animal Constructor Called.
// Hourse Constructor Called.
// Animal Eats
// Hourse walks on 4 legs.
// Animal Constructor Called.
// Animal Eats
// Peacock walks on 2 legs.
// Animal Constructor Called.
// Hourse Constructor Called.
// Mustang Constructor Called.
```
# `Interface:`
- Interface is Blueprint of a class.
- Interface(Blueprint of Class) - Class(Blueprint of Object) - Object
- Use Interface Keyword
- All Methods are Public, Abstract & Without Imlimentation.
- Used to achieve total Abstraction.
- Varible in the Interface are Finaly, Public and Static.
- Class - Extends
- Interface - Impliment
- Total Abstraction (Interfaces)
- Multiple Inheritance (Implimentation)
- 
```java
// Interface: Blueprint of Class: Achive Multiple Inheritance:
public class OOP{
public static void main(String args[]) {
Queen queen = new Queen();
queen.moves();
}
}
interface ChessPlayer{
void moves();
}
class Queen implements ChessPlayer{
public void moves(){
System.out.println("Queen: Up, Down, Left, Right, Diagonal, (in all 4 Directions)");
}
}
class Rook implements ChessPlayer{
public void moves(){
System.out.println("Rook: Up, Down, Left, Right");
}
}
class King implements ChessPlayer{
public void moves(){
System.out.println("King: Up, Down, Left, Right, Diagonal, (by 1 Step)");
}
}
// Multiple Inheritance:
interface Herbivors{
void eat();
}
interface Carnivore{
void eat();
}
class Bear implements Herbivors, Carnivore{
public void eat(){
System.out.println("Eat Grass And Eat Meat.");
}
}
```
# `Static Keyword::`
- Static Keyword in Java is used to **Share the Same Variable Or Method of a given Class**
- Properties: Variables
- Methods: Funtions
- Blocks: {--Club 2-3 Lines of Code--} (Black of Code)
- Nested Classes: Similar as Nested Loops
```java
// Static:
public class OOP{
public static void main(String args[]) {
Student student1 = new Student();
student1.schoolName = "SubhedarWada";
Student student2 = new Student();
System.out.println(student2.schoolName);
System.out.println(student2.returnPercentage(12,12,12));
}
}
class Student{
String name;
int rollno;
// Properties:
static String schoolName;
// Methods:
static int returnPercentage(int math, int phy, int chem){
return(math+phy+chem)/3;
}
void setName(String name){
this.name = name;
}
String getName(){
return this.name;
}
}
```
# `Super Keyword::`
- Super keywordis used to **refer Immediate Parent Class Object.**
- To Access Parent's Properties.
- To Access Parent's Functions.
- To Access Parent's Constructor.
```java
// Super:
public class OOP{
public static void main(String args[]) {
Horse horse = new Horse();
System.out.println(horse.color);
}
}
class Animal{
String color;
Animal(){
System.out.println("Animal Constructor Callaed");
}
}
class Horse extends Animal{
Horse(){
super.color = "Brown";
System.out.println("Horse Constructor Called");
}
}
```
- Advanced: Constructive Chaining You can Learn.
- End OOP's
Java -> Android, Web, Enterprise Market | Applications | Complex Web Applications | Mobile Applications | Emmbeded Softwares
Language -> Syntax Cahange
Basic -> Core (OOP) -> JDBC(Java Database Connectivity) -> Servelet -> Jsp -> Hybernate Framework -> Spring -> Spring Boot
Java as Language:
- Basic: Syntax, Cnstruct, loop, Condition, logic.
- Core:- Object, Inheritance, Polymorphism, Encapsulation, Abstraction.
- Advanced:- Files, Database, Multithreading.
Java as Technology: Servelet, Spring Framework, Hibernate Framework
IDE: Integrated Devlopment Environment -> Type - Compile - Run.
In Java Every Line make Scence.
Prespective: Core Java: Java Prespective | Web: Java EE (Enterprise Editor)
Create:
New Java Project
New Package | Class
String Should be in Double Coatation: ("")
Semicolan: Terminates the line: ();
Block of Code: Class{}, Methods(){}
Source Code: Compile: Byte Code: Java Virtual Machine
Variables: Container, Memory Name, To Store Data,
Primitive Data Types & Refrence Data Types:
Premitive Data Types:
|Data Type|Keyword|Size(bit)|Size(Byte)|
|---------|-------|---------|----------|
|CHarecter|Char |16 |2 |
|Byte |Byte |8 |1 |
|Short |Short |16 |2 |
|Integer |Int |32 |4 |
|Float |FLoat |32 |4 |
|Long |Long |64 |8 |
|Double |Double |64 |8 |
|Boolean |Boolean|8 |1 |
- Allowed in Variable: $ _
- Not Allowed: Start with Number
- Decimal Numbers: By Default Double
- To Declare Float: Mention 5.5f;
- To Long Number: Mention 50000000000l;
- Char ch = 'C';
- American Standard Code For Information Interchange:
- Implicit Convertion: Mean Java Do By Default.
Double d1 = 5;
- Explicit Conversion: Type Casting: Changes ForceFully Done By Us.
int k = (int)5.6
Byte - Char - Short - int - long - Float - Double
- Naming Convantion in Java: Specific Standards:
Look Good, More Efficient, Readable
- In Java We Follow Camel-Casing Rule:
- Variable Name: sname, stockprice
- Constant: PI, DENSITY, MAX_PRICE
- Method: Verb : actionPerformed(), run(), print(), write()
- Class Name: Noun : String, Integer, Student, Worker, Engineer, Person, Computer, HashMap
- Constructor: Car(), Run(), Swim()
- Interface Name: Adjective - able : Runable, Serializable, Remote, Readable
- Multiline Comment: /*-----*/
- Single Line Comment: //
- Operators:
- Arithmatic Operators:
- Bitwise Operators:
- Relational Operators:
- Logical Operators: