Java Assignment Answers
Java Assignment Answers
A class in Java is a blueprint from which individual objects are created. It defines the properties
Example:
class Car {
String color;
int speed;
void drive() {
System.out.println("Car is driving");
Instance variables are defined inside the class but outside any method. Methods define behaviors.
Example:
class Student {
String name;
void display() {
Example:
class Bike {
void run() {
System.out.println("Bike is running");
b.run();
class Person {
String name;
int age;
void display() {
p1.name = "Alice";
p1.age = 22;
p1.display();
5. Constructor explanation.
A constructor initializes an object.
Example:
class Student {
String name;
Student(String n) {
name = n;
void display() {
s.display();
6. Method overloading.
Method overloading allows multiple methods with same name but different parameters.
Example:
class Add {
return a + b;
return a + b;
}
7. Constructor overloading.
Example:
class Employee {
String name;
int age;
Employee() {
name = "Unknown";
age = 0;
Employee(String n, int a) {
name = n;
age = a;
8. Access specifiers.
Example:
class Sample {
void display() {
}
9. Static keyword.
Example:
class Counter {
Counter() {
count++;
System.out.println(count);
Example:
class Test {
static int x;
static {
x = 10;