Defining Classes in Java, Constructors, and Methods
Java classes, constructors, and methods are the core
components of Java's object-oriented programming (OOP) paradigm. Let’s explore
each in detail.
1. Defining
Classes in Java
A class is a blueprint or template from which
objects are created. It defines the attributes (fields) and behaviors (methods)
that an object of that class can have.
Syntax:
class ClassName {
// Fields (Attributes)
DataType variableName;
// Methods (Behaviors)
ReturnType methodName(Parameters) {
// Method body
}
}
Example:
public class
Student {
// Fields (Attributes)
String name;
int age;
// Method (Behavior)
public void display() {
System.out.println("Name: " +
name);
System.out.println("Age: " +
age);
}
}
2. Constructors
A constructor is a special method used to initialize
objects. It has the same name as the class and does not have a return type.
Characteristics:
- Automatically called when an object is created.
- Can be overloaded (multiple constructors with different parameters).
- Default constructor is provided if no constructor is defined.
Types of
Constructors:
- Default Constructor (No-Argument Constructor)
public Student()
{
name = "Unknown";
age = 0;
}
- Parameterized Constructor
public
Student(String name, int age) {
this.name = name; // The 'this' keyword differentiates instance
variables from parameters.
this.age = age;
}
- Copy Constructor (For creating a new object as a copy of an existing
object)
public
Student(Student s) {
this.name = s.name;
this.age = s.age;
}
Example: Using
Constructors
public class
Student {
String name;
int age;
// Default constructor
public Student() {
name = "Unknown";
age = 0;
}
// Parameterized constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Copy constructor
public Student(Student s) {
this.name = s.name;
this.age = s.age;
}
public void display() {
System.out.println("Name: " +
name);
System.out.println("Age: " +
age);
}
public static void main(String[] args) {
// Using default constructor
Student student1 = new Student();
student1.display();
// Using parameterized constructor
Student student2 = new
Student("Krishna", 25);
student2.display();
// Using copy constructor
Student student3 = new
Student(student2);
student3.display();
}
}
Output:
Name: Unknown
Age: 0
Name: Krishna
Age: 25
Name: Krishna
Age: 25
3. Methods
Methods define the behaviors or functionalities of a
class. They contain code that performs a specific task.
Syntax:
AccessSpecifier
ReturnType methodName(ParameterList) {
// Method body
}
Access
Specifiers:
- public: Accessible from anywhere.
- private: Accessible only within the class.
- protected: Accessible within the package and subclasses.
- default (no specifier): Accessible
within the package.
Return Type:
- void if the method doesn't return a value.
- Any data type if the method returns a value.
Method
Declaration and Calling Example:
public class
Calculator {
// Method with return type
public int add(int a, int b) {
return a + b;
}
// Method without return type (void)
public void displayMessage(String message)
{
System.out.println(message);
}
public static void main(String[] args) {
Calculator calc = new Calculator();
int sum = calc.add(10, 20); // Calling a method with return type
System.out.println("Sum: " +
sum);
calc.displayMessage("Hello,
Java!"); // Calling a void method
}
}
Output:
Sum: 30
Hello, Java!
4. Method
Overloading
Method overloading allows a class to have more than
one method with the same name but different parameters (type, number, or both).
Example of
Overloading:
public class
Display {
// Overloaded methods
public void show(int num) {
System.out.println("Number: "
+ num);
}
public void show(String text) {
System.out.println("Text: " +
text);
}
public void show(int num, String text) {
System.out.println("Number: "
+ num + ", Text: " + text);
}
public static void main(String[] args) {
Display obj = new Display();
obj.show(100);
obj.show("Hello");
obj.show(100, "Hello");
}
}
Output:
Number: 100
Text: Hello
Number: 100,
Text: Hello
5. ‘this’ Keyword
- Refers to the current instance of the class.
- Helps to differentiate between instance variables and parameters with
the same name.
- Can be used to invoke another constructor within the same class.
No comments:
Post a Comment