diff --git a/18_OOPS/Access_Modifier.java b/18_OOPS/Access_Modifier.java new file mode 100644 index 0000000..173b292 --- /dev/null +++ b/18_OOPS/Access_Modifier.java @@ -0,0 +1,23 @@ +/** + * Lecture 2 + * Access Modifier lecture + * Bank is a class + */ +public class Access_Modifier { + public static void main(String[] args) { + // creating object of BankAccount + BankAccount myAcc = new BankAccount(); + myAcc.username = "Saksham"; + myAcc.setPassword("abcd"); + } +} + +class BankAccount { + public String username; + private String password; + + // setter function to set password + public void setPassword(String pwd) { + password = pwd; + } +} \ No newline at end of file diff --git a/18_OOPS/Constructor.java b/18_OOPS/Constructor.java new file mode 100644 index 0000000..8d2f0bd --- /dev/null +++ b/18_OOPS/Constructor.java @@ -0,0 +1,25 @@ +/** + * Lecture 5 + * Constructor in Java + * Java has its own default constructor + * It invokes automatically + */ + +public class Constructor { + public static void main(String[] args) { + Student s1 = new Student("Shradha"); + System.out.println(s1.name); + } + +} + +class Student { + String name; + int rollNo; + + Student(String name) { + name = this.name; + System.out.println("Constructor is called..."); + } + +} \ No newline at end of file diff --git a/18_OOPS/CopyConstructor.java b/18_OOPS/CopyConstructor.java new file mode 100644 index 0000000..6f69f7e --- /dev/null +++ b/18_OOPS/CopyConstructor.java @@ -0,0 +1,61 @@ +/** + * Lecture 8 + * Copy Constrcutors + */ +public class CopyConstructor { + public static void main(String[] args) { + Student s1 = new Student(); + s1.name = "shradha"; + s1.rollNo = 456; + s1.pswd = "abcd"; + + s1.marks[0] = 100; + s1.marks[1] = 90; + s1.marks[2] = 80; + + Student s2 = new Student(s1); // copy + s1.pswd = "xyz"; + s1.marks[2] = 100; + for (int i = 0; i < 3; i++) { + System.out.println(s2.marks[i]); + } + + } +} + +class Student { + String name; + int rollNo; + String pswd; + int marks[]; + + class Address { + String city; + } + + Student(Student s1) { + marks = new int[3]; + this.name = s1.name; + this.rollNo = s1.rollNo; + this.marks = s1.marks; + } + + // Non-Parameterised Constructors + Student() { + marks = new int[3]; + System.out.println("Constructor is called..."); + } + + // Parametrised Constructor + Student(String name) { + marks = new int[3]; + name = this.name; + } + + // Parametrised Constructor + Student(int rollNo) { + marks = new int[3]; + rollNo = this.rollNo; + } + +} \ No newline at end of file diff --git a/18_OOPS/Destructors.java b/18_OOPS/Destructors.java new file mode 100644 index 0000000..21ae5ec --- /dev/null +++ b/18_OOPS/Destructors.java @@ -0,0 +1,8 @@ +// lecture 9 +// Destructor + +public class Destructors { + public static void main(String[] args) { + // No Code Only theory + } +} diff --git a/18_OOPS/Encapsulation.java b/18_OOPS/Encapsulation.java new file mode 100644 index 0000000..a4b1a38 --- /dev/null +++ b/18_OOPS/Encapsulation.java @@ -0,0 +1,12 @@ +/** + * Lecture 4 + * Encapsulation + * One of the pillars of OOPS + */ + +public class Encapsulation { + public static void main(String[] args) { + // Only theory lecture + } + +} diff --git a/18_OOPS/Getters_Setters.java b/18_OOPS/Getters_Setters.java new file mode 100644 index 0000000..418c984 --- /dev/null +++ b/18_OOPS/Getters_Setters.java @@ -0,0 +1,37 @@ +/** + * Leccture 3 + * Getters and setters + * this - refrence to same object + */ +public class Getters_Setters { + public static void main(String[] args) { + // creating object of Pen + Pen p1 = new Pen(); // created a pen called p1 as an object + p1.setColor("Blue"); + System.out.println(p1.getColor()); + p1.setTip(5); + System.out.println(p1.getTip()); + p1.setColor("yellow"); + } +} + +class Pen { + private String color; + private int tip; + + String getColor() { + return this.color; + } + + int getTip() { + return this.tip; + } + + void setColor(String newColor) { + color = newColor; + } + + void setTip(int Tip) { + tip = this.tip; + } +} \ No newline at end of file diff --git a/18_OOPS/Hierarchial.java b/18_OOPS/Hierarchial.java new file mode 100644 index 0000000..f312648 --- /dev/null +++ b/18_OOPS/Hierarchial.java @@ -0,0 +1,51 @@ +/** + * Lecture 13 + * Hierarchial inheritance + */ +public class Hierarchial { + public static void main(String[] args) { + Mammal m1 = new Mammal(); + Fish f1 = new Fish(); + Bird b1 = new Bird(); + m1.breathe(); + f1.breathe(); + b1.breathe(); + } +} + +// Base Class +class Animal { + String color; + + void eat() { + System.out.println("eating"); + } + + void breathe() { + System.out.println("breathing"); + } +} + +class Mammal extends Animal { + int legs; + + void walks() { + System.out.println("walks"); + } +} + +class Fish extends Animal { + void swim() { + System.out.println("swimming"); + } +} + +class Bird extends Animal { + void fly() { + System.out.println("flying"); + } +} + +class Dog extends Mammal { + String breed; +} \ No newline at end of file diff --git a/18_OOPS/Inhertitance.java b/18_OOPS/Inhertitance.java new file mode 100644 index 0000000..af62f5e --- /dev/null +++ b/18_OOPS/Inhertitance.java @@ -0,0 +1,36 @@ +/** + * Lecture 10 + * Inheritance + * One of the pillars of OOPS + * + */ +public class Inhertitance { + public static void main(String[] args) { + Fish shark = new Fish(); + shark.eat(); + shark.breathe(); + shark.swims(); + } +} + +// Base Class +class Animal { + String color; + + void eat() { + System.out.println("eating"); + } + + void breathe() { + System.out.println("breathing"); + } +} + +// Derived Class +class Fish extends Animal { + int fins; + + void swims() { + System.out.println("swimming"); + } +} \ No newline at end of file diff --git a/18_OOPS/MultiLevel.java b/18_OOPS/MultiLevel.java new file mode 100644 index 0000000..d8a73f2 --- /dev/null +++ b/18_OOPS/MultiLevel.java @@ -0,0 +1,35 @@ +import javax.annotation.processing.SupportedOptions; + +/** + * Lecture 12 + * MultiLevel Inheritance + */ +public class MultiLevel { + public static void main(String[] args) { + Dog dobby = new Dog(); + dobby.eat(); + dobby.legs = 4; + System.out.println(dobby.legs); + } +} + +// Base Class +class Animal { + String color; + + void eat() { + System.out.println("eating"); + } + + void breathe() { + System.out.println("breathing"); + } +} + +class Mammal extends Animal { + int legs; +} + +class Dog extends Mammal { + String breed; +} diff --git a/18_OOPS/Oops.java b/18_OOPS/Oops.java new file mode 100644 index 0000000..b7e3dd7 --- /dev/null +++ b/18_OOPS/Oops.java @@ -0,0 +1,41 @@ +/** + * Leacture 1 - Classes and Objects + * Classes name starts with Capital letters as a conventions + * new keyword - allocates memory in heap + * public- its a access modifier + */ +public class Oops { + public static void main(String[] args) { + // creating objects + Pen p1 = new Pen(); // created a pen called p1 as an object + p1.setColor("Blue"); + System.out.println(p1.color); + p1.setTip(5); + System.out.println(p1.tip); + p1.color = "yellow"; + } +} + +// only one class can be puclic and it should be on top of all +class Pen { + String color; + int tip; + + void setColor(String newColor) { + color = newColor; + } + + void setTip(int newTip) { + tip = newTip; + } +} + +class Student { + String name; + int age; + float percentage; // cgpa + + void calcPercentage(int phy, int chem, int mth) { + percentage = (phy + chem + mth) / 3; + } +} \ No newline at end of file diff --git a/18_OOPS/TypeOfConstructor.java b/18_OOPS/TypeOfConstructor.java new file mode 100644 index 0000000..5941937 --- /dev/null +++ b/18_OOPS/TypeOfConstructor.java @@ -0,0 +1,35 @@ +/** + * Lecture 6 + * Types of Constructor + * + */ + +public class TypeOfConstructor { + public static void main(String[] args) { + Student s1 = new Student(); + Student s2 = new Student("Shradha"); + Student s3 = new Student(35); + // Student s3 = new Student("aman",35); wrong + } +} + +class Student { + String name; + int rollNo; + + // Non-Parameterised Constructors + Student() { + System.out.println("Constructor is called..."); + } + + // Parametrised Constructor + Student(String name) { + name = this.name; + } + + // Parametrised Constructor + Student(int rollNo) { + rollNo = this.rollNo; + } + +} \ No newline at end of file diff --git a/18_OOPS/TypesOfInheritance.java b/18_OOPS/TypesOfInheritance.java new file mode 100644 index 0000000..27e01c6 --- /dev/null +++ b/18_OOPS/TypesOfInheritance.java @@ -0,0 +1,11 @@ +/** + * lecture 11 + * Types of Inheritance + * + * + */ +public class TypesOfInheritance { + public static void main(String[] args) { + // No code lecture + } +}