0% found this document useful (0 votes)
8 views3 pages

Java Class Fundamentals and Concepts

This document provides an overview of Java class fundamentals, including class creation, object instantiation, method overloading, and constructors. It also covers concepts such as passing objects, access control, static and final keywords, the 'this' keyword, garbage collection, and wrapper classes. Examples are provided to illustrate each concept effectively.

Uploaded by

prachilad.co24d1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Class Fundamentals and Concepts

This document provides an overview of Java class fundamentals, including class creation, object instantiation, method overloading, and constructors. It also covers concepts such as passing objects, access control, static and final keywords, the 'this' keyword, garbage collection, and wrapper classes. Examples are provided to illustrate each concept effectively.

Uploaded by

prachilad.co24d1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming Notes - Part 2: Class

Fundamentals

General Form of a Class


class ClassName {
dataType variableName;
returnType methodName(parameters) {
// body
}
}

Creating Class and Object


class Student {
String name;
int age;

void display() {
[Link]("Name: " + name + ", Age: " + age);
}
}

class Test {
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Alice";
[Link] = 20;
[Link]();
}
}

Overloading Methods
class MathOps {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}

Constructor
class Student {
String name;
int age;

Student(String n, int a) {
name = n;
age = a;
}
}

Constructor Overloading
class Person {
String name;
int age;

Person() {
name = "Unknown";
age = 0;
}
Person(String n, int a) {
name = n;
age = a;
}
}

Passing and Returning Objects


class Box {
int length;
Box(int l) { length = l; }

boolean compare(Box b) {
return [Link] == [Link];
}

Box bigger(Box b) {
if ([Link] > [Link])
return this;
else
return b;
}
}

Assigning Object References


Student s1 = new Student("John", 21);
Student s2 = s1;
[Link] = "David";
[Link](); // Prints: David - 21

Access Control
class Student {
private int rollNo;
public String name;

public void setRoll(int r) { rollNo = r; }


public int getRoll() { return rollNo; }
}

static Keyword
class Counter {
static int count = 0;
Counter() { count++; }
}

final Keyword
final class A {}
// class B extends A {} // Error: cannot inherit final class

this Keyword
class Student {
String name;
Student(String name) {
[Link] = name;
}
}

Garbage Collection and finalize()


class Test {
protected void finalize() {
[Link]("Object destroyed");
}
public static void main(String[] args) {
Test t = new Test();
t = null;
[Link]();
}
}

Wrapper Classes
int a = 10;
Integer obj = [Link](a); // Boxing
int b = [Link](); // Unboxing

Common questions

Powered by AI

The `final` keyword in Java is beneficial when you want to prevent inheritance of a class, thereby ensuring that the class's implementation remains unchanged and its behavior predictable when used. Making a class final can lead to optimizations by the Java compiler because it knows about invariability in certain aspects of the class structure and methods. For instance, marking a class as final, as shown in the example with class A, prevents other classes from altering its behavior through inheritance .

The `this` keyword in Java refers to the current instance of a class, and it is crucial when you need to disambiguate between class attributes and parameters with the same names. In the Student class, it's necessary for assigning a constructor parameter to the instance variable name, distinguishing local variables from instance variables. This keyword avoids shadowing and increases code readability and maintainability by clearly indicating when a member of the current object is being accessed or modified .

Method overloading in Java allows multiple methods to have the same name with different parameter lists within a class. This enhances flexibility by enabling polymorphic-like behavior where a method can operate with different types of inputs or different numbers of inputs while maintaining a single conceptual action. For example, the MathOps class demonstrates overloading through the add methods that accept different types (int and double), allowing for operations on various numeric data types without requiring method name changes .

The static keyword in Java signifies that a particular member belongs to the class rather than instances of the class. For resource management, this means that static fields are common across all instances—they are shared. For example, in the Counter class, the static variable count increments whenever a new instance is created, regardless of how many instances exist. This allows for efficient use of memory for shared properties and simplifies tracking global states across all instances within a class .

Garbage collection in Java is crucial for automatically managing memory. It relieves developers from manually deallocating objects that are no longer in use, helping avoid memory leaks. The finalize method, though deprecated in newer Java versions, provides a hook for cleanup actions before an object is garbage collected. As in the Test example, invoking System.gc() suggests that the garbage collector should engage, which may call finalize allowing resource deallocation or logging. However, reliance on finalize is discouraged due to unpredictability, as garbage collection time is not guaranteed .

Method overriding allows a subclass to provide a specific implementation for a method already defined in a super class, thereby supporting runtime polymorphism. Method overloading, on the other hand, is within the same class, involving methods with the same name but different parameters. Overriding must follow rules like matching the method name, return type, and parameter list exactly; the overridden method cannot be less accessible than the overridden method in the parent class; and travel without raising exceptions incompatible with those in the superclass. Overloading provides compile-time polymorphism and optimizes code readability and reusability, while overriding focuses on achieving specialized behaviors in subclasses .

Constructors initialize new objects and set necessary default values or configurations. The ability to overload constructors in Java enhances this by permitting multiple ways to instantiate an object. For example, the Person class shows constructor overloading where one constructor sets default values, while another takes parameters to initialize object fields. This provides flexibility and convenience in object creation, allowing clients of a class to create objects in diverse states depending on the context and requirements .

Encapsulation in Java uses access control (public, private, protected, and package-private) to hide implementation details of a class from the outside world and restrict access to its components. For example, in the Student class, private access to rollNo ensures that direct manipulation is prevented, protecting data integrity. Methods like setRoll and getRoll are provided as the interface for other classes to interact with this data, maintaining a controlled and predictable interaction pattern .

Assigning object references in Java means that variables in reference point to the same memory location. For instance, when s2 is assigned to s1 (Student s2 = s1;), both variables reference the same Student object. Changes made through either reference are reflected in the same memory space. This simplifies memory management but requires caution for object mutation, as unintended side effects can occur if objects are modified in ways that other parts of an application do not expect .

Wrapper classes in Java allow conversion between primitive types (int, char, etc.) and their corresponding objects. This process, known as boxing (converting primitives to objects) and unboxing (converting objects back to primitives), enhances object manipulation. By creating wrapper objects, you can utilize classes like Integer or Character in collections that require objects, such as ArrayLists. Furthermore, this abstraction aligns Java's object-oriented nature with operations that involve primitives, enhancing flexibility in handling numeric operations within the language paradigm .

You might also like