Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java_Homework_Answers

The document provides answers to common Java homework questions, covering fundamental concepts such as classes, objects, constructors, methods, inheritance, method overriding, abstract classes, and interfaces. It includes definitions, syntax examples, and distinctions between related concepts. The content is structured in a question-and-answer format, making it easy to understand key Java programming principles.

Uploaded by

kangirene9705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_Homework_Answers

The document provides answers to common Java homework questions, covering fundamental concepts such as classes, objects, constructors, methods, inheritance, method overriding, abstract classes, and interfaces. It includes definitions, syntax examples, and distinctions between related concepts. The content is structured in a question-and-answer format, making it easy to understand key Java programming principles.

Uploaded by

kangirene9705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Homework Answers

1. What is a class in Java?


A class in Java is a blueprint for creating objects. It defines fields (variables) and methods
(functions) that objects of that class will have.

2. What is an object in Java?


An object is an instance of a class. It has a state (attributes) and behavior (methods).

3. How will you create an object?


An object is created using the `new` keyword followed by a call to a class constructor.

4. What is the syntax for object creation?


ClassName obj = new ClassName();
Example:
Car myCar = new Car();

5. What is a constructor?
A constructor is a special method in a class that is automatically called when an object is
created. It initializes the object.

6. What is the syntax of a constructor?


class ClassName {
ClassName() {
// Constructor body
}
}

7. What are the types of constructors?


1. Default Constructor (No parameters)
2. Parameterized Constructor (Takes parameters)
3. Copy Constructor (Copies values from another object)

8. What is a method?
A method is a block of code that performs a specific task and is defined inside a class.

9. What is the syntax of a method?


returnType methodName(parameters) {
// Method body
}
10. What is the difference between a pre-defined method and a user-defined
method?
Pre-defined methods are built into Java (e.g., `Math.sqrt()`).
User-defined methods are created by the programmer.

11. What is method overloading?


Method overloading allows multiple methods to have the same name but different
parameters (different type or number).

12. Example program for method overloading:


class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}

public class Main {


public static void main(String[] args) {
MathOperations obj = new MathOperations();
System.out.println(obj.add(5, 10));
System.out.println(obj.add(5.5, 2.2));
}
}

13. What is constructor overloading?


Constructor overloading means defining multiple constructors with different parameters.

14. Example program for constructor overloading:


class Person {
String name;
int age;

Person() {
name = "Unknown";
age = 0;
}

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

public class Main {


public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person("Alice", 25);
}
}

15. Differences between methods and constructors:


| Feature | Methods | Constructors |
|---------------|---------|-------------|
| Name | Any name | Same as class name |
| Return Type | Has a return type | No return type |
| Called When | Explicitly called | Automatically called when an object is created |
| Purpose | Perform actions | Initialize objects |

16. What is inheritance?


Inheritance is a mechanism in Java where one class acquires the properties and behaviors of
another class.

17. What are the types of inheritance?


1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance (via interfaces)

18. Syntax for different types of inheritance:


// Single Inheritance
class Parent {}
class Child extends Parent {}

// Multilevel Inheritance
class Grandparent {}
class Parent extends Grandparent {}
class Child extends Parent {}

// Hierarchical Inheritance
class Parent {}
class Child1 extends Parent {}
class Child2 extends Parent {}
19. What is method overriding? Give an example.
Method overriding allows a subclass to provide a specific implementation of a method that
is already defined in its superclass.

Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
d.makeSound(); // Output: Dog barks
}
}

20. What is an abstract class? What is an abstract method?


An abstract class is a class that cannot be instantiated. It can have abstract and non-abstract
methods.
An abstract method has no body and must be implemented by subclasses.

21. Syntax for an abstract class and an abstract method:


abstract class Animal {
abstract void makeSound();
}

22. Example program of an abstract class and how it is used:


abstract class Animal {
abstract void makeSound();
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound();
}
}

23. What is an interface?


An interface is a collection of abstract methods that a class can implement. It provides full
abstraction.

24. Example program using an interface:


interface Animal {
void makeSound();
}

class Dog implements Animal {


public void makeSound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
d.makeSound();
}
}

You might also like