0% found this document useful (0 votes)
24 views9 pages

Module1 10 Marks

The document explains key concepts in Java, including objects, classes, constructors, method overloading, inheritance, and interfaces. It provides examples of how to create and use classes and objects, as well as the differences between static and instance methods. Additionally, it discusses how interfaces facilitate multiple inheritance in Java and includes code snippets demonstrating these concepts.

Uploaded by

ayusingggg
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)
24 views9 pages

Module1 10 Marks

The document explains key concepts in Java, including objects, classes, constructors, method overloading, inheritance, and interfaces. It provides examples of how to create and use classes and objects, as well as the differences between static and instance methods. Additionally, it discusses how interfaces facilitate multiple inheritance in Java and includes code snippets demonstrating these concepts.

Uploaded by

ayusingggg
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
You are on page 1/ 9

Q1) What is an object in Java? Explain how objects are created and used with an example.

What is a class in Java? Write a simple Java class with attributes and methods.

Ans - An object is an instance of a class. It represents a real-world entity and holds both state
(attributes or variables) and behavior (methods or functions). Objects are created based on class
definitions and allow programmers to work with real-world modeling in an organized way.

Example - // Defining a class


class Car {
String brand;
int speed;
// Constructor
Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
// Method to display car details
void display() {
System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of Car class
Car myCar = new Car("Toyota", 120);

// Calling a method using the object


myCar.display();
}
}
A class in Java is a blueprint for creating objects. It defines attributes (variables) and behaviors
(methods) that its objects will have.
// Defining a Person class
class Person {
// Attributes
String name;
int age;

// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}

// Method to display details


void introduce() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of Person class
Person person1 = new Person("Alice", 25);

// Calling a method
person1.introduce();
}
}
Q2) What is a constructor in Java? Explain the different types of constructors with an example.
What is the difference between a static method and an instance method? Provide examples.
Ans – A constructor in Java is a special method used to initialize objects when they are created. It has
the same name as the class and does not have a return type.
Types of Constructors in Java
1. Default Constructor (No parameters)
o Automatically provided if no constructor is defined.
o Initializes objects with default values.
2. Parameterized Constructor (With parameters)
o Used to initialize objects with specific values.
3. Copy Constructor (Creates a copy of another object)
• Used to duplicate an existing object.
Example- // Defining a class with different constructors
class Student {
String name;
int age;
// Default Constructor
Student() {
this.name = "Unknown";
this.age = 0;
}
// Parameterized Constructor
Student(String name, int age) {
this.name = name;
this.age = age;
}
// Copy Constructor
Student(Student other) {
this.name = other.name;
this.age = other.age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Using Default Constructor
Student s1 = new Student();
s1.display();

// Using Parameterized Constructor


Student s2 = new Student("Alice", 22);
s2.display();

// Using Copy Constructor


Student s3 = new Student(s2);
s3.display();
}
}
Static vs. Instance Methods in Java
Static methods belong to the class and can be called without creating an object.
Instance methods require an object to be invoked.
Key Differences:
Feature Static Method Instance Method
Belongs To Class Object
Invocation ClassName.methodName() objectName.methodName()
Can Access Instance Variables? No Yes
Can Be Overridden? No Yes

Example - class MathOperations {


int num;
// Instance method (requires an object)
void square() {
System.out.println("Square: " + (num * num));
}
// Static method (does not require an object)
static int cube(int n) {
return n * n * n;
}
}
public class Main {
public static void main(String[] args) {
// Calling a static method
System.out.println("Cube: " + MathOperations.cube(3));
// Calling an instance method
MathOperations obj = new MathOperations();
obj.num = 4;
obj.square();
}
}
Q3) What is method overloading? Write a Java program demonstrating method overloading.
Explain the concept of inheritance in Java. Write a Java program demonstrating inheritance.
Ans - Method overloading allows multiple methods in the same class to share the same
name but have different parameters (type, number, or order). It improves code readability and
flexibility
Example of Method Overloading
class MathOperations {
// Method with one parameter
int multiply(int a) {
return a * a;
}
// Method with two parameters
int multiply(int a, int b) {
return a * b;
}
// Method with three parameters
double multiply(double a, double b, double c) {
return a * b * c;
}
}
public class Main {
public static void main(String[] args) {
MathOperations obj = new MathOperations();
System.out.println("Multiply 1 number: " + obj.multiply(5));
System.out.println("Multiply 2 numbers: " + obj.multiply(4, 3));
System.out.println("Multiply 3 numbers: " + obj.multiply(2.5, 3.5, 4.0));
}
}
Inheritance in Java
Inheritance is a core concept of Object-Oriented Programming (OOP) where a child class
inherits properties and behaviors from a parent class. It promotes code reusability and
establishes a hierarchy.
Types of Inheritance in Java
1. Single Inheritance – One child inherits from one parent class.
2. Multilevel Inheritance – A child inherits from a parent, which itself is a child of
another class.
3. Hierarchical Inheritance – Multiple classes inherit from the same parent. (Java does
not support multiple inheritance through classes, but it is possible via interfaces.)

Example of Inheritance
// Parent class
class Animal {
String name;
void makeSound() {
System.out.println(name + " makes a sound.");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println(name + " barks!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Buddy";

myDog.makeSound(); // Inherited method


myDog.bark(); // Child class method
}
}
Q4) What is an interface in Java? How is it different from an abstract class?

Explain how interfaces help in achieving multiple inheritance in Java.


Ans - An interface in Java is a collection of abstract methods that define a contract for classes to
implement. Unlike regular classes, interfaces cannot have instance variables or concrete methods
(until Java 8, when default methods were introduced). They are used to achieve abstraction and
enforce a common behavior across multiple classes.
Example - // Defining an interface
interface Animal {
void makeSound(); // Abstract method
}
// Implementing the interface in a class
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks!");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Calls the implemented method
}
}
Difference Between an Interface and an Abstract Class
Feature Interface Abstract Class
Only abstract methods (before Java
Method Type Both abstract & concrete methods
8)
Instance Variables No instance variables Can have instance variables
Can have protected, private
Access Modifiers Methods are public by default
methods
Constructor No constructors Can have constructors
Multiple Doesn't support multiple
Supports multiple inheritance
Inheritance inheritance

Java does not support multiple inheritance with classes. However, interfaces allow a class to
implement multiple behaviors without conflicts. A single class can implement multiple
interfaces and inherit their method definitions.
Example of Multiple Inheritance Using Interfaces
interface Animal {
void makeSound();
}
interface Movable {
void move();
}
// Class implementing multiple interfaces
class Dog implements Animal, Movable {
public void makeSound() {
System.out.println("Dog barks!");
}
public void move() {
System.out.println("Dog runs!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
myDog.move();
}
}
Q5) How can a method return an object in Java? Provide an example.
What is the difference between static methods and instance methods in Java? Give an
example.
Ans - In Java, a method can return an object by specifying the class type as the return type.
This is useful when you want to create an object inside a method and return it for further use.
Example: Returning an Object from a Method
class Car {
String brand;
int speed;
// Constructor
Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
// Method that returns an object
static Car createCar() {
return new Car("Toyota", 120);
}
void display() {
System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
// Calling the method and receiving an object
Car myCar = Car.createCar();
// Using the returned object
myCar.display();
}
}
Static Methods vs. Instance Methods in Java
Static Method:
• Belongs to the class, not an instance.
• Can be called using the class name.
• Cannot access instance variables or methods.
Instance Method:
• Requires an object to be invoked.
• Can access instance variables.
• Can be overridden in subclasses.
Example: Static vs. Instance Methods
class MathOperations {
int num;
// Instance method (requires an object)
void square() {
System.out.println("Square: " + (num * num));
}
// Static method (does not require an object)
static int cube(int n) {
return n * n * n;
}
}
public class Main {
public static void main(String[] args) {
// Calling a static method
System.out.println("Cube: " + MathOperations.cube(3));
// Calling an instance method
MathOperations obj = new MathOperations();
obj.num = 4;
obj.square();
}
}
Q6) Define a class Student with attributes (name, ID, grade). Create an object and print
details.
Ans - // Defining the Student class
class Student {
// Attributes
String name;
int ID;
char grade;
// Constructor
Student(String name, int ID, char grade) {
this.name = name;
this.ID = ID;
this.grade = grade;
}
// Method to display student details
void displayDetails() {
System.out.println("Student Name: " + name);
System.out.println("Student ID: " + ID);
System.out.println("Grade: " + grade);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of Student class
Student student1 = new Student("Alice", 101, 'A');

// Calling the method to print details


student1.displayDetails();
}
}

• The Student class has three attributes: name, ID, and grade.
• A constructor initializes these attributes when an object is created.
• The displayDetails() method prints student information.
• In the Main class, an object student1 is created and the method is called to print
details.

You might also like