OOP - Principles
OOP - Principles
OOP - Principles
1. Name: The constructor must have the same name as the class it is in.
2. No Return Type: Constructors do not have a return type, not even void.
3. Purpose: The primary purpose of a constructor is to initialize the newly created
object.
4. Types of Constructors:
• Default Constructor: A constructor that takes no arguments. If no constructor is
explicitly defined, Java provides a default constructor.
• Parameterized Constructor: A constructor that takes one or more arguments,
allowing for more flexible initialization of objects.
// Default constructor
public Dog() {
this.name = "Unknown";
this.age = 0;
}
// Parameterized constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
Constructor Overloading
Just like other methods, constructors can be overloaded. This means you can have
more than one constructor in a class with different parameter lists.
Constructor Chaining
In Java, you can call one constructor from another constructor in the same class
using this(). This is called constructor chaining.
Variables
Variables in Java are used to store data that can be used and manipulated within a
program. They act as containers for storing data values. Each variable in Java has a
specific type, which determines the kind of data it can hold.
1. Local Variables:
• Declared with the static keyword within a class, outside any method, constructor, or
block.
• Shared among all instances of the class.
• Default initialized if not explicitly initialized.
Variables must be declared with a type and can be initialized at the time of
declaration or later in the code.
// Instance variable
int instanceVariable;
Variable Types
1. Primitive Types: These include int, char, boolean, byte, short, long, float, and
double.
2. int number = 5;
3. char letter = 'A';
4. boolean flag = true;
Set Method
In Java, set methods, also known as "setter" methods, are part of the class design
used to modify the value of private fields in a class. They provide a controlled way to
set the value of an instance variable, allowing for encapsulation and data hiding. This
practice adheres to the principles of object-oriented programming.
1. Naming Convention: Typically, setter methods are named using the prefix set
followed by the name of the field with the first letter capitalized.
2. Access Modifier: They are usually public, allowing external code to modify the value
of private fields.
3. Parameter: They take a parameter that represents the new value to be assigned to
the field.
4. No Return Value: Set methods typically have a void return type, as their primary
purpose is to set a value.
// Getter methods
public String getName() {
return name;
}
1. Encapsulation: By using private fields and public setter methods, you encapsulate
the internal representation of the object, which allows you to control how the data is
modified.
2. Validation: You can add validation logic within the setter method to ensure that only
valid data is assigned to the fields.
3. Flexibility: If the internal implementation changes, the setter methods can provide a
consistent interface for modifying the object's state without affecting the external
code that uses the class.
Here's another example where the setter method includes validation logic:
Get Method
In Java, get methods, also known as "getter" methods, are used to retrieve the value
of private fields in a class. They provide a controlled way to access the value of an
instance variable, supporting encapsulation and data hiding.
1. Naming Convention: Typically, getter methods are named using the prefix get
followed by the name of the field with the first letter capitalized.
2. Access Modifier: They are usually public, allowing external code to read the value of
private fields.
3. Return Value: They return the value of the field they are associated with.
4. No Parameters: Getter methods typically do not take any parameters.
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Public getter method for name
public String getName() {
return name;
}
1. Encapsulation: By using private fields and public getter methods, you encapsulate
the internal representation of the object, which allows you to control how the data is
accessed.
2. Read-Only Access: Getter methods provide read-only access to the fields,
preventing unauthorized modification.
3. Flexibility: If the internal implementation changes, the getter methods can provide a
consistent interface for accessing the object's state without affecting the external
code that uses the class.
// Setter method
public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Invalid balance. Balance cannot be negative.");
}
}
// Getter method
public double getBalance() {
return balance;
}
toString
In Java, the toString method is used to provide a string representation of an object. It
is defined in the Object class, which is the superclass of all classes in Java. By
default, the toString method returns a string that consists of the class name followed
by the @ symbol and the object's hashcode in hexadecimal form. However, it is
common practice to override this method to provide a more meaningful string
representation of an object.
The default implementation in the Object class looks something like this:
Here’s another example with a different class to show how you can override the
toString method:
Method Overloading
Method overloading in Java is a feature that allows a class to have more than one
method with the same name, but with different parameter lists. It is a form of
polymorphism (specifically, compile-time polymorphism) that enables methods to
perform similar but slightly varied operations based on different inputs.
1. Method Signature: Overloaded methods must have different parameter lists (different
types, number, or order of parameters). The return type can be different, but it alone
is not sufficient to overload a method.
2. Compile-Time Polymorphism: The decision about which method to invoke is made at
compile-time based on the method signature.
3. Ease of Use: Method overloading increases the readability and reusability of code by
allowing methods with the same name to handle different data types or numbers of
arguments.
• Different Parameter List: Methods must have different parameter lists (different
types, number, or order of parameters).
• Return Type: The return type can be different, but it alone does not differentiate
overloaded methods.
• Access Modifiers: Methods can have different access modifiers, though this is not a
requirement for overloading.
• Static Methods: Static methods can also be overloaded in the same way as instance
methods.