Computer Class As Computation
Computer Class As Computation
S PUBLIC SCHOOL
2nd Block, Rajajinagar, Bangalore-560010
2. Wrapper Class
Definition: A wrapper class in Java provides a way to use primitive
data types (like int, char, etc.) as objects. Each primitive type has a
corresponding wrapper class, such as Integer for int, Double for
double, and so on.
Common Wrapper Classes:
Integer for int
Character for char
Boolean for boolean
Example:
java
Copy code
public class WrapperClassExample {
public static void main(String[] args) {
int num = 5;
Integer wrappedNum = Integer.valueOf(num); // Boxing
System.out.println(wrappedNum);
}
}
In this example, Integer is a wrapper class that wraps the primitive
int into an object.
3. Autoboxing
Definition: Autoboxing is the automatic conversion of primitive
data types into their corresponding wrapper class objects. This is
useful in cases where an object is required, such as in collections
(e.g., ArrayList or HashMap).
Example:
java
Copy code
public class AutoboxingExample {
public static void main(String[] args) {
int num = 10;
Integer wrappedNum = num; // Autoboxing: automatic conversion
from int to Integer
System.out.println(wrappedNum);
}
}
Here, num is automatically converted from int to Integer.
4. Unboxing
Definition: Unboxing is the reverse of autoboxing, where an object
of a wrapper class is automatically converted into its corresponding
primitive type.
Example:
java
Copy code
public class UnboxingExample {
public static void main(String[] args) {
Integer wrappedNum = 20;
int num = wrappedNum; // Unboxing: automatic conversion from
Integer to int
System.out.println(num);
}
}
In this example, wrappedNum (an Integer object) is automatically
unboxed into an int.
6. Encapsulation
Definition: Encapsulation is the principle of wrapping data
(variables) and code (methods) together as a single unit, usually by
making variables private and providing public getter and setter
methods for access.
Benefits: It hides the internal state of an object and only exposes a
controlled interface.
Example:
java
Copy code
public class EncapsulationExample {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
EncapsulationExample obj = new EncapsulationExample();
obj.setName("John");
System.out.println(obj.getName()); // Output: John
}
}
The class EncapsulationExample encapsulates the name variable by
providing public methods to access it.
8. Inheritance
Definition: Inheritance allows a class to acquire properties and
behavior (methods) from another class. The class that is inherited is
called the superclass or parent class, and the class that inherits is
called the subclass or child class.
Syntax: class SubClass extends SuperClass
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
}
}
In this example, Dog inherits the eat() method from Animal.
9. Java Packages
Definition: A package in Java is a namespace that organizes a set of
related classes and interfaces. Java provides built-in packages such
as java.util and java.io. You can also create your own packages.
Purpose: To prevent naming conflicts and make it easier to locate
and use classes.
Example:
java
Copy code
package mypackage; // Declare a package
public class MyClass {
public void display() {
System.out.println("Hello from the package!");
}
}