For working professionals
For fresh graduates
More
Step by Step Java Tutorial Con…
1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
Now Reading
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects and classes. It is designed to model real-world entities and relationships using objects. OOP is highly beneficial for creating modular, reusable, and maintainable code, and the core principles that govern it are Encapsulation, Inheritance, Polymorphism, and Abstraction. Understanding these principles is crucial for grasping the fundamentals of OOP.
Let’s explore each principle in depth.
1. Encapsulation
Encapsulation is the process of bundling data (variables) and the methods (functions) that operate on that data into a single unit, typically a class. The goal of encapsulation is to protect the internal state of an object from outside interference and misuse, allowing changes to be made only through well-defined interfaces.
Encapsulation ensures that an object's internal workings are hidden, and only the necessary information is exposed through public methods. This concept is vital for creating robust and secure code since it restricts direct access to some of the object’s components, providing controlled interaction.
#Example:
```java
public class Car {
private String model;
private int speed;
public Car(String model, int speed) {
this.model = model;
this.speed = speed;
}
// Getter and setter methods to control access
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed >= 0) {
this.speed = speed;
}
}
}
```
In this example, the `Car` class encapsulates the `model` and `speed` fields, controlling access through getter and setter methods. This prevents external code from modifying the car's speed or model in an unintended way.
2. Inheritance
Inheritance is the mechanism by which one class can acquire the properties (fields) and behaviors (methods) of another class. It allows for code reuse and the extension of existing classes. The class that inherits from another is called the subclass or child class, and the class from which it inherits is known as the superclass or parent class.
This principle helps in reducing redundancy, making the code more manageable and scalable. Inheritance also facilitates the hierarchical classification of classes and enables the subclass to modify or enhance the inherited methods.
#Example:
```java
public class Vehicle {
protected int wheels;
protected String fuelType;
public void move() {
System.out.println("The vehicle is moving");
}
}
public class Car extends Vehicle {
private String model;
public Car(String model) {
this.model = model;
this.wheels = 4; // Inherited property
this.fuelType = "Gasoline"; // Inherited property
}
// Overriding the move method
@Override
public void move() {
System.out.println("The car is moving");
}
}
```
In this example, `Car` inherits from the `Vehicle` class and gains access to its fields (`wheels` and `fuelType`) and methods. It can also override the `move` method to provide more specific functionality.
3. Polymorphism
Polymorphism, meaning “many shapes,” allows objects of different classes to be treated as objects of a common superclass. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding). This principle allows for flexibility and the ability to define one interface and have multiple implementations.
- Method Overloading: Multiple methods with the same name but different parameters in the same class.
- Method Overriding: A subclass provides a specific implementation of a method that is already defined in its superclass.
Polymorphism is key to making systems more modular, extensible, and easier to maintain.
#Example (Method Overriding):
Java
public class Animal {
public void sound() {
System.out.println("This is a generic animal sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}
In this example, both `Dog` and `Cat` classes override the `sound` method of the `Animal` class, providing their own specific implementation. Polymorphism allows these objects to be treated as instances of `Animal`, despite behaving differently.
4. Abstraction
Abstraction focuses on hiding the complexity of a system by exposing only the necessary parts of an object, making it simpler for users to interact with it. It allows programmers to focus on *what* an object does rather than *how* it does it.
In OOP, abstraction is achieved using abstract classes and interfaces. An abstract class cannot be instantiated on its own and may contain abstract methods (methods without a body), while an interface defines a contract for what a class should implement without dictating how the methods are executed.
Example:
```java
public abstract class Shape {
public abstract double calculateArea();
}
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
In this example, `Shape` is an abstract class that defines an abstract method `calculateArea`. The `Circle` class provides a concrete implementation of the method. This abstraction allows other developers to use `Shape` without needing to know the internal details of how the area is calculated.
By following these principles, OOP provides several benefits:
- Code Reusability: Through inheritance, classes can reuse code from parent classes, reducing redundancy.
- Scalability: Polymorphism and abstraction allow the system to grow in complexity while maintaining manageable code.
- Maintainability: Encapsulation makes code easier to debug and maintain by keeping data and functions closely tied.
- Security: Data hiding through encapsulation provides a layer of protection from external access.
The principles of Object-Oriented Programming—encapsulation, inheritance, polymorphism, and abstraction—form the foundation of OOP and are essential for designing efficient, maintainable, and scalable software. Understanding and applying these concepts allows developers to write cleaner, more modular code that reflects real-world entities and relationships. Whether you're a beginner or an experienced developer, mastering these principles will enhance your ability to build robust software systems.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.