-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOPs.java
129 lines (104 loc) · 2.95 KB
/
OOPs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// import java.util.*;
class Pen {
String color;
String type;
}
class Student {
String name;
int age;
// public void printInfo() {
// System.out.println(this.name);
// System.out.println(this.age);
// }
// Student(String name, int age) { -->parametrised constructor
// this.name = name;
// this.age = age;
// }
// Student() {
// System.out.println("Constructor called");
// }
// Student(Student s2) {
// this.name = s2.name;
// this.age = s2.age;
// }
// polymorphism -> creating same name of method with diff types parameter or diff
// types of return stmt.
public void printInfo(String name) {
System.out.println(name);
}
public void printInfo(int age) {
System.out.println(age);
}
public void printInfo(String name, int age) {
System.out.println(name + " " + age);
}
}
// Inheritance --> Single Level, Multi-level,Hierarchial, Hybrid
class Shape {
String color;
public void printShape() {
System.out.println("It's shape");
}
}
class Triangle extends Shape {
public void printTriangle(int b, int h) {
System.out.println("It's a triangle that have an area");
}
}
class Circle extends Shape{
public void printArea(int r){
System.out.println(3.14*r*r);
}
}
class EquilateralTriangle extends Triangle {
public void printEquiTriangle(int l, int h){
System.out.println(1/2*l*h);
}
}
// Access Modifiers - public, private, protective, default
//Abstraction
abstract class Animal{
abstract void walk();
}
class Horse extends Animal{
public void walk(){
System.out.println("Walks on 4 legs");
}
}
class Duck extends Animal{
public void walk(){
System.out.println("Walks in 2 legs");
}
}
public class OOPs {
public static void main(String args[]) {
// Pen p1 = new Pen();
// p1.color = "blue";
// p1.type = "ballpoint";
// Pen p2 = new Pen();
// p2.color = "red";
// System.out.println(p1.color);
// System.out.println(p2.color);
// Student s1 = new Student(); // --> non parameterised constructor
// s1.name = "Ram";
// s1.age = 14;
// s1.printInfo();
// Student s2 = new Student("tony", 19); //--> parameterised constructor
// s2.printInfo();
// copy constructor
// Student s2 = new Student(s1);
// s2.printInfo();
// Polymorphism
// Student s1 = new Student();
// s1.printInfo("Govind");
// s1.printInfo(18);
// s1.printInfo("Shayam",18);
// Inheritance
// Triangle t = new Triangle();
// t.color= "red";
// System.out.println(t.color);
// ---------------------
Horse horse = new Horse();
horse.walk();
}
}