Explain how to declare a Class in Java. Also explain the process of Creating Objects in Java. Write a short-note on Principles of object-oriented language. Explain Class Variable a... Explain how to declare a Class in Java. Also explain the process of Creating Objects in Java. Write a short-note on Principles of object-oriented language. Explain Class Variable and Methods in detail. Explain Widening conversion and narrowing conversion in brief. Explain how to Pass the arrays to methods and return arrays from method with example. Explain how to declare an Interface. Give an Example of it. Explain the method overriding with example. Explain the process of creation, saving, compiling and executing packages in Java. Write a program to use the String Methods of String Class. Explain the conversion of primitive data type to wrapper objects and wrapper objects to primitive types. Write a short-note on java.lang.Objects class. How to set the Thread Priority? Explain in brief. Explain the categories of Exception using diagram. Write a short-note on Thread States. Write a short-note on multiple catch exception.
Understand the Problem
The document contains exam questions related to Object Oriented Concepts with Java, including topics such as class declaration, object creation, object-oriented principles, array methods, interface declaration, string methods, thread priority, and exception categories.
Answer
In Java, a class is declared with the 'class' keyword, and objects are created with 'new'. OOP principles include encapsulation, inheritance, polymorphism, and abstraction. Widening and narrowing involve size conversions. Arrays can be passed and returned by reference. Interfaces define abstract methods implemented by classes.
Creating a class in Java involves using the 'class' keyword followed by the class name. For example:
class Car {
String color;
int year;
void display() {
System.out.println("Color: " + color + ", Year: " + year);
}
}
To create an object, use the 'new' keyword:
Car myCar = new Car();
myCar.color = "Red";
myCar.year = 2020;
myCar.display();
Principles of Object-Oriented Language
- Encapsulation: Wrapping data and methods into a single unit (class).
- Inheritance: A class can inherit properties and methods from another.
- Polymorphism: Ability to take many forms; methods may work differently based on inputs.
- Abstraction: Hiding complex implementation details using simple interfaces.
Class Variables and Methods
- Class Variable: Declared inside the class but outside any method, and typically marked static.
- Method: A block of code executing a function, can include parameters.
Widening and Narrowing Conversions
- Widening: Converting a smaller primitive type to a larger one (e.g., int to long).
- Narrowing: Converting a larger type to a smaller one, requires explicit casting (e.g., double to int).
Pass and Return Arrays
- Passing: Arrays are passed by reference. Example:
void printArray(int[] arr) {
for (int i : arr) {
System.out.println(i);
}
}
- Returning:
int[] getArray() {
return new int[]{1, 2, 3};
}
Declaring an Interface
An interface is a reference type similar to a class, containing abstract methods:
interface Vehicle {
void move();
}
class Bike implements Vehicle {
public void move() {
System.out.println("Bike is moving");
}
}
Method Overriding
Occurs when a subclass has a method with the same name and parameters as a method in the parent class:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Packages
- Create: Organize similar types of classes and interfaces.
- Save: In a directory matching the package name.
- Compile: Use 'javac -d' to set the destination for class files.
- Execute: Use 'java' with the package path.
String Methods Example
public class StringsExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.length());
System.out.println(str.toUpperCase());
}
}
Primitive to Wrapper and Vice Versa
- Primitive to Wrapper: Using autoboxing, e.g., Integer intObj = 5;
-
Wrapper to Primitive: Using methods like
intValue()
, e.g., int num = intObj;
java.lang.Objects
Provides methods for object manipulation (e.g., equals
, hashCode
, toString
).
Thread Priority
Adjust using setPriority(Thread.MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY);
Exception Categories
Exceptions in Java can be checked (checked at compile-time) and unchecked (occur at runtime). Include subclasses like IOException, RuntimeException.
Thread States
- New: Just created.
- Runnable: Ready or running.
- Blocked: Waiting to acquire a lock.
- Waiting: Threads wait to be notified.
- Timed_Waiting: Waits for a specific period.
- Terminated: Completed execution.
Multiple Catch
Using multiple catch blocks allows handling different types of exceptions:
try {
// code
} catch (ArithmeticException e) {
// handle error
} catch (Exception e) {
// handle error
}
Answer for screen readers
Creating a class in Java involves using the 'class' keyword followed by the class name. For example:
class Car {
String color;
int year;
void display() {
System.out.println("Color: " + color + ", Year: " + year);
}
}
To create an object, use the 'new' keyword:
Car myCar = new Car();
myCar.color = "Red";
myCar.year = 2020;
myCar.display();
Principles of Object-Oriented Language
- Encapsulation: Wrapping data and methods into a single unit (class).
- Inheritance: A class can inherit properties and methods from another.
- Polymorphism: Ability to take many forms; methods may work differently based on inputs.
- Abstraction: Hiding complex implementation details using simple interfaces.
Class Variables and Methods
- Class Variable: Declared inside the class but outside any method, and typically marked static.
- Method: A block of code executing a function, can include parameters.
Widening and Narrowing Conversions
- Widening: Converting a smaller primitive type to a larger one (e.g., int to long).
- Narrowing: Converting a larger type to a smaller one, requires explicit casting (e.g., double to int).
Pass and Return Arrays
- Passing: Arrays are passed by reference. Example:
void printArray(int[] arr) {
for (int i : arr) {
System.out.println(i);
}
}
- Returning:
int[] getArray() {
return new int[]{1, 2, 3};
}
Declaring an Interface
An interface is a reference type similar to a class, containing abstract methods:
interface Vehicle {
void move();
}
class Bike implements Vehicle {
public void move() {
System.out.println("Bike is moving");
}
}
Method Overriding
Occurs when a subclass has a method with the same name and parameters as a method in the parent class:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Packages
- Create: Organize similar types of classes and interfaces.
- Save: In a directory matching the package name.
- Compile: Use 'javac -d' to set the destination for class files.
- Execute: Use 'java' with the package path.
String Methods Example
public class StringsExample {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.length());
System.out.println(str.toUpperCase());
}
}
Primitive to Wrapper and Vice Versa
- Primitive to Wrapper: Using autoboxing, e.g., Integer intObj = 5;
-
Wrapper to Primitive: Using methods like
intValue()
, e.g., int num = intObj;
java.lang.Objects
Provides methods for object manipulation (e.g., equals
, hashCode
, toString
).
Thread Priority
Adjust using setPriority(Thread.MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY);
Exception Categories
Exceptions in Java can be checked (checked at compile-time) and unchecked (occur at runtime). Include subclasses like IOException, RuntimeException.
Thread States
- New: Just created.
- Runnable: Ready or running.
- Blocked: Waiting to acquire a lock.
- Waiting: Threads wait to be notified.
- Timed_Waiting: Waits for a specific period.
- Terminated: Completed execution.
Multiple Catch
Using multiple catch blocks allows handling different types of exceptions:
try {
// code
} catch (ArithmeticException e) {
// handle error
} catch (Exception e) {
// handle error
}
More Information
Understanding the foundations of Java classes, objects, and conversions is crucial for effective Java programming. These concepts form the basis for developing applications using the Java programming language.
Tips
Remember to match method signatures exactly when overriding, and be aware of array passing by reference, which can lead to unintentional changes in the array contents.
Sources
- Classes and Objects in Java - GeeksforGeeks - geeksforgeeks.org
- Java - Classes and Objects - TutorialsPoint - tutorialspoint.com
- Chapter 5. Conversions and Contexts - docs.oracle.com