Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding OOPS #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 18_OOPS/Access_Modifier.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions 18_OOPS/Constructor.java
Original file line number Diff line number Diff line change
@@ -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...");
}

}
61 changes: 61 additions & 0 deletions 18_OOPS/CopyConstructor.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
8 changes: 8 additions & 0 deletions 18_OOPS/Destructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// lecture 9
// Destructor

public class Destructors {
public static void main(String[] args) {
// No Code Only theory
}
}
12 changes: 12 additions & 0 deletions 18_OOPS/Encapsulation.java
Original file line number Diff line number Diff line change
@@ -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
}

}
37 changes: 37 additions & 0 deletions 18_OOPS/Getters_Setters.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions 18_OOPS/Hierarchial.java
Original file line number Diff line number Diff line change
@@ -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;
}
36 changes: 36 additions & 0 deletions 18_OOPS/Inhertitance.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
35 changes: 35 additions & 0 deletions 18_OOPS/MultiLevel.java
Original file line number Diff line number Diff line change
@@ -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;
}
41 changes: 41 additions & 0 deletions 18_OOPS/Oops.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
35 changes: 35 additions & 0 deletions 18_OOPS/TypeOfConstructor.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
11 changes: 11 additions & 0 deletions 18_OOPS/TypesOfInheritance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* lecture 11
* Types of Inheritance
*
*
*/
public class TypesOfInheritance {
public static void main(String[] args) {
// No code lecture
}
}