0% found this document useful (0 votes)
10 views

Java

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

Java

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

1.

Concept of Inheritance and Method Overriding

• Inheritance is a mechanism in which one class (child class) acquires the properties and behaviors
(methods) of another class (parent class). This allows for reusability of code and a hierarchical
relationship between classes.

• Method Overriding occurs when a child class provides a specific implementation of a method
that is already defined in its parent class. This is done to change or extend the behavior of the
method in the child class.

Example:

java1. Concept of Inheritance and Method Overriding

Inheritance is a mechanism in which one class (child class) acquires the properties and behaviors
(methods) of another class (parent class). This allows for reusability of code and a hierarchical
relationship between classes.

Method Overriding occurs when a child class provides a specific implementation of a method that is
already defined in its parent class. This is done to change or extend the behavior of the method in the
child class.

Example:

java

Copy code

// Parent class

class Animal {

void sound() {

System.out.println("Animal makes a sound");

// Child class

class Dog extends Animal {

// Overriding the sound method of Animal class

@Override
void sound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal();

Animal myDog = new Dog(); // Polymorphism

myAnimal.sound(); // Output: Animal makes a sound

myDog.sound(); // Output: Dog barks (Overridden method)

2. Object and Class

Class: A blueprint for creating objects. It defines properties (fields/attributes) and behaviors (methods).

Object: An instance of a class. Objects hold the state (values of fields) and can use the behaviors defined
by the class.

Example:

java

Copy code

// Class definition

class Car {

String model;

int year;

// Method

void display() {
System.out.println("Model: " + model + ", Year: " + year);

public class Main {

public static void main(String[] args) {

// Creating objects of class Car

Car car1 = new Car();

car1.model = "Toyota";

car1.year = 2020;

car1.display(); // Output: Model: Toyota, Year: 2020

3. Method, Method Overloading, Constructor, and Constructor Overloading

Method: A function defined inside a class. It performs a task or operation.

Method Overloading: Allows a class to have more than one method with the same name but different
parameter lists.

Example: Method Overloading

java

Copy code

class Calculator {

// Method Overloading

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) {

Calculator calc = new Calculator();

System.out.println(calc.add(2, 3)); // Output: 5 (int version)

System.out.println(calc.add(2.5, 3.5)); // Output: 6.0 (double version)

Constructor: A special method that is called when an object is instantiated. It initializes the object.

Constructor Overloading: Multiple constructors in the same class with different parameter lists.

Example: Constructor and Constructor Overloading

java

Copy code

class Student {

String name;

int age;

// Constructor 1 (default)

Student() {

name = "Unknown";

age = 0;
}

// Constructor 2 (parameterized)

Student(String name, int age) {

this.name = name;

this.age = age;

void display() {

System.out.println("Name: " + name + ", Age: " + age);

public class Main {

public static void main(String[] args) {

Student s1 = new Student(); // Calls default constructor

Student s2 = new Student("John", 20); // Calls parameterized constructor

s1.display(); // Output: Name: Unknown, Age: 0

s2.display(); // Output: Name: John, Age: 20

4. Programs

Largest of Three Integers:

java

Copy code

import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter three integers:");

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

int largest = (a > b && a > c) ? a : (b > c ? b : c);

System.out.println("The largest number is: " + largest);

Even or Odd Program:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = sc.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is even.");

} else {

System.out.println(num + " is odd.");

}
}

Factorial Program:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

System.out.println("Factorial of " + num + " is " + factorial);

5. Procedural Language vs Object-Oriented Concepts

Procedural Programming focuses on procedures (functions). It follows a top-down approach, where a


program is divided into smaller parts called procedures or functions.

Object-Oriented Programming (OOP) focuses on objects and encapsulating related data and behaviors.
Key concepts in OOP are:
Class and Object

Encapsulation

Inheritance

Polymorphism

Abstraction

6. Use of this and super Keywords

this refers to the current instance of the class.

super is used to refer to the parent class's methods or constructors.

Example:

java

Copy code

class Animal {

String name;

Animal(String name) {

this.name = name; // this refers to the current object

void display() {

System.out.println("Animal Name: " + name);

class Dog extends Animal {

String breed;

Dog(String name, String breed) {

super(name); // Calling the parent class constructor


this.breed = breed; // this refers to the current class

@Override

void display() {

super.display(); // Calls the display method of parent class

System.out.println("Breed: " + breed);

public class Main {

public static void main(String[] args) {

Dog dog = new Dog("Max", "Labrador");

dog.display(); // Output: Animal Name: Max, Breed: Labrador

7. Garbage Collector

Garbage Collection is the process of automatically freeing up memory by destroying unused objects that
are no longer referenced in the program.

In Java, the Garbage Collector is a part of the JVM that identifies and removes objects that are no longer
used to reclaim memory. 1. Concept of Inheritance and Method Overriding

• Inheritance is a mechanism in which one class (child class) acquires the properties and behaviors
(methods) of another class (parent class). This allows for reusability of code and a hierarchical
relationship between classes.

• Method Overriding occurs when a child class provides a specific implementation of a method
that is already defined in its parent class. This is done to change or extend the behavior of the
method in the child class.

Example:

java

Copy code

// Parent class
class Animal {

void sound() {

System.out.println("Animal makes a sound");

// Child class

class Dog extends Animal {

// Overriding the sound method of Animal class

@Override

void sound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal();

Animal myDog = new Dog(); // Polymorphism

myAnimal.sound(); // Output: Animal makes a sound

myDog.sound(); // Output: Dog barks (Overridden method)

2. Object and Class

• Class: A blueprint for creating objects. It defines properties (fields/attributes) and behaviors
(methods).

• Object: An instance of a class. Objects hold the state (values of fields) and can use the behaviors
defined by the class.
Example:

java

Copy code

// Class definition

class Car {

String model;

int year;

// Method

void display() {

System.out.println("Model: " + model + ", Year: " + year);

public class Main {

public static void main(String[] args) {

// Creating objects of class Car

Car car1 = new Car();

car1.model = "Toyota";

car1.year = 2020;

car1.display(); // Output: Model: Toyota, Year: 2020

3. Method, Method Overloading, Constructor, and Constructor Overloading

• Method: A function defined inside a class. It performs a task or operation.

• Method Overloading: Allows a class to have more than one method with the same name but
different parameter lists.

Example: Method Overloading


java

Copy code

class Calculator {

// Method Overloading

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) {

Calculator calc = new Calculator();

System.out.println(calc.add(2, 3)); // Output: 5 (int version)

System.out.println(calc.add(2.5, 3.5)); // Output: 6.0 (double version)

• Constructor: A special method that is called when an object is instantiated. It initializes the
object.

• Constructor Overloading: Multiple constructors in the same class with different parameter lists.

Example: Constructor and Constructor Overloading

java

Copy code

class Student {

String name;

int age;
// Constructor 1 (default)

Student() {

name = "Unknown";

age = 0;

// Constructor 2 (parameterized)

Student(String name, int age) {

this.name = name;

this.age = age;

void display() {

System.out.println("Name: " + name + ", Age: " + age);

public class Main {

public static void main(String[] args) {

Student s1 = new Student(); // Calls default constructor

Student s2 = new Student("John", 20); // Calls parameterized constructor

s1.display(); // Output: Name: Unknown, Age: 0

s2.display(); // Output: Name: John, Age: 20

4. Programs

Largest of Three Integers:


java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter three integers:");

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

int largest = (a > b && a > c) ? a : (b > c ? b : c);

System.out.println("The largest number is: " + largest);

Even or Odd Program:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = sc.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is even.");


} else {

System.out.println(num + " is odd.");

Factorial Program:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

System.out.println("Factorial of " + num + " is " + factorial);

5. Procedural Language vs Object-Oriented Concepts

• Procedural Programming focuses on procedures (functions). It follows a top-down approach,


where a program is divided into smaller parts called procedures or functions.

• Object-Oriented Programming (OOP) focuses on objects and encapsulating related data and
behaviors. Key concepts in OOP are:
o Class and Object

o Encapsulation

o Inheritance

o Polymorphism

o Abstraction

6. Use of this and super Keywords

• this refers to the current instance of the class.

• super is used to refer to the parent class's methods or constructors.

Example:

java

Copy code

class Animal {

String name;

Animal(String name) {

this.name = name; // this refers to the current object

void display() {

System.out.println("Animal Name: " + name);

class Dog extends Animal {

String breed;

Dog(String name, String breed) {

super(name); // Calling the parent class constructor

this.breed = breed; // this refers to the current class


}

@Override

void display() {

super.display(); // Calls the display method of parent class

System.out.println("Breed: " + breed);

public class Main {

public static void main(String[] args) {

Dog dog = new Dog("Max", "Labrador");

dog.display(); // Output: Animal Name: Max, Breed: Labrador

7. Garbage Collector

• Garbage Collection is the process of automatically freeing up memory by destroying unused


objects that are no longer referenced in the program.

• In Java, the Garbage Collector is a part of the JVM that identifies and removes objects that are
no longer used to reclaim memory.

Copy code

// Parent class

class Animal {

void sound() {

System.out.println("Animal makes a sound");

}
// Child class

class Dog extends Animal {

// Overriding the sound method of Animal class

@Override

void sound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Animal();

Animal myDog = new Dog(); // Polymorphism

myAnimal.sound(); // Output: Animal makes a sound

myDog.sound(); // Output: Dog barks (Overridden method)

2. Object and Class

• Class: A blueprint for creating objects. It defines properties (fields/attributes) and behaviors
(methods).

• Object: An instance of a class. Objects hold the state (values of fields) and can use the behaviors
defined by the class.

Example:

java

Copy code

// Class definition

class Car {

String model;
int year;

// Method

void display() {

System.out.println("Model: " + model + ", Year: " + year);

public class Main {

public static void main(String[] args) {

// Creating objects of class Car

Car car1 = new Car();

car1.model = "Toyota";

car1.year = 2020;

car1.display(); // Output: Model: Toyota, Year: 2020

3. Method, Method Overloading, Constructor, and Constructor Overloading

• Method: A function defined inside a class. It performs a task or operation.

• Method Overloading: Allows a class to have more than one method with the same name but
different parameter lists.

Example: Method Overloading

java

Copy code

class Calculator {

// Method Overloading

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) {

Calculator calc = new Calculator();

System.out.println(calc.add(2, 3)); // Output: 5 (int version)

System.out.println(calc.add(2.5, 3.5)); // Output: 6.0 (double version)

• Constructor: A special method that is called when an object is instantiated. It initializes the
object.

• Constructor Overloading: Multiple constructors in the same class with different parameter lists.

Example: Constructor and Constructor Overloading

java

Copy code

class Student {

String name;

int age;

// Constructor 1 (default)

Student() {

name = "Unknown";

age = 0;

}
// Constructor 2 (parameterized)

Student(String name, int age) {

this.name = name;

this.age = age;

void display() {

System.out.println("Name: " + name + ", Age: " + age);

public class Main {

public static void main(String[] args) {

Student s1 = new Student(); // Calls default constructor

Student s2 = new Student("John", 20); // Calls parameterized constructor

s1.display(); // Output: Name: Unknown, Age: 0

s2.display(); // Output: Name: John, Age: 20

4. Programs

Largest of Three Integers:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter three integers:");

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

int largest = (a > b && a > c) ? a : (b > c ? b : c);

System.out.println("The largest number is: " + largest);

Even or Odd Program:

java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = sc.nextInt();

if (num % 2 == 0) {

System.out.println(num + " is even.");

} else {

System.out.println(num + " is odd.");

Factorial Program:
java

Copy code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

System.out.println("Factorial of " + num + " is " + factorial);

5. Procedural Language vs Object-Oriented Concepts

• Procedural Programming focuses on procedures (functions). It follows a top-down approach,


where a program is divided into smaller parts called procedures or functions.

• Object-Oriented Programming (OOP) focuses on objects and encapsulating related data and
behaviors. Key concepts in OOP are:

o Class and Object

o Encapsulation

o Inheritance

o Polymorphism

o Abstraction

6. Use of this and super Keywords


• this refers to the current instance of the class.

• super is used to refer to the parent class's methods or constructors.

Example:

java

Copy code

class Animal {

String name;

Animal(String name) {

this.name = name; // this refers to the current object

void display() {

System.out.println("Animal Name: " + name);

class Dog extends Animal {

String breed;

Dog(String name, String breed) {

super(name); // Calling the parent class constructor

this.breed = breed; // this refers to the current class

@Override

void display() {

super.display(); // Calls the display method of parent class

System.out.println("Breed: " + breed);


}

public class Main {

public static void main(String[] args) {

Dog dog = new Dog("Max", "Labrador");

dog.display(); // Output: Animal Name: Max, Breed: Labrador

7. Garbage Collector

• Garbage Collection is the process of automatically freeing up memory by destroying unused


objects that are no longer referenced in the program.

• In Java, the Garbage Collector is a part of the JVM that identifies and removes objects that are
no longer used to reclaim memory.

You might also like