-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOPConcepts.java
More file actions
161 lines (135 loc) · 2.92 KB
/
Copy pathOOPConcepts.java
File metadata and controls
161 lines (135 loc) · 2.92 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package java1.oop;
/**
* Java 1.0 Object-Oriented Programming Concepts Demonstrates classes, objects, inheritance, and polymorphism
*/
public class OOPConcepts
{
public static void main(String[] args)
{
System.out.println("=== Java 1.0 OOP Concepts ===\n");
System.out.println("1. Classes and Objects");
System.out.println("---------------------");
Person person = new Person("John", 30);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("\n2. Inheritance");
System.out.println("-------------");
Dog dog = new Dog("Buddy");
dog.makeSound();
dog.displayInfo();
System.out.println("\n3. Polymorphism");
System.out.println("--------------");
Animal animal1 = new Dog("Max");
Animal animal2 = new Cat("Whiskers");
animal1.makeSound(); // Polymorphic call
animal2.makeSound(); // Polymorphic call
System.out.println("\n4. Encapsulation");
System.out.println("---------------");
BankAccount account = new BankAccount(1000);
System.out.println("Initial balance: " + account.getBalance());
account.deposit(500);
System.out.println("After deposit: " + account.getBalance());
account.withdraw(200);
System.out.println("After withdrawal: " + account.getBalance());
System.out.println("\nKey OOP Principles:");
System.out.println("- Encapsulation: Data hiding with private fields");
System.out.println("- Inheritance: Extending classes");
System.out.println("- Polymorphism: Method overriding");
System.out.println("- Abstraction: Abstract classes and interfaces");
}
}
// Example: Encapsulation
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
// Example: Inheritance
class Animal
{
protected String name;
public Animal(String name)
{
this.name = name;
}
public void makeSound()
{
System.out.println("Animal makes sound");
}
public void displayInfo()
{
System.out.println("Animal: " + name);
}
}
class Dog extends Animal
{
public Dog(String name)
{
super(name);
}
@Override
public void makeSound()
{
System.out.println(name + " barks: Woof! Woof!");
}
}
class Cat extends Animal
{
public Cat(String name)
{
super(name);
}
@Override
public void makeSound()
{
System.out.println(name + " meows: Meow! Meow!");
}
}
// Example: Encapsulation
class BankAccount
{
private double balance;
public BankAccount(double initialBalance)
{
this.balance = initialBalance;
}
public double getBalance()
{
return balance;
}
public void deposit(double amount)
{
if (amount > 0)
{
balance += amount;
}
}
public void withdraw(double amount)
{
if (amount > 0 && amount <= balance)
{
balance -= amount;
}
}
}