Java
Java
• 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:
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() {
// Child class
@Override
void sound() {
System.out.println("Dog barks");
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);
car1.model = "Toyota";
car1.year = 2020;
Method Overloading: Allows a class to have more than one method with the same name but different
parameter lists.
java
Copy code
class Calculator {
// Method Overloading
return a + b;
}
double add(double a, double b) {
return a + b;
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.
java
Copy code
class Student {
String name;
int age;
// Constructor 1 (default)
Student() {
name = "Unknown";
age = 0;
}
// Constructor 2 (parameterized)
this.name = name;
this.age = age;
void display() {
4. Programs
java
Copy code
import java.util.Scanner;
public class Main {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
java
Copy code
import java.util.Scanner;
if (num % 2 == 0) {
} else {
}
}
Factorial Program:
java
Copy code
import java.util.Scanner;
int factorial = 1;
factorial *= i;
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
Example:
java
Copy code
class Animal {
String name;
Animal(String name) {
void display() {
String breed;
@Override
void display() {
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() {
// Child class
@Override
void sound() {
System.out.println("Dog barks");
• 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() {
car1.model = "Toyota";
car1.year = 2020;
• Method Overloading: Allows a class to have more than one method with the same name but
different parameter lists.
Copy code
class Calculator {
// Method Overloading
return a + b;
return a + b;
• 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.
java
Copy code
class Student {
String name;
int age;
// Constructor 1 (default)
Student() {
name = "Unknown";
age = 0;
// Constructor 2 (parameterized)
this.name = name;
this.age = age;
void display() {
4. Programs
Copy code
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
java
Copy code
import java.util.Scanner;
if (num % 2 == 0) {
Factorial Program:
java
Copy code
import java.util.Scanner;
int factorial = 1;
factorial *= i;
• 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
Example:
java
Copy code
class Animal {
String name;
Animal(String name) {
void display() {
String breed;
@Override
void display() {
7. Garbage Collector
• 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() {
}
// Child class
@Override
void sound() {
System.out.println("Dog barks");
• 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() {
car1.model = "Toyota";
car1.year = 2020;
• Method Overloading: Allows a class to have more than one method with the same name but
different parameter lists.
java
Copy code
class Calculator {
// Method Overloading
return a + b;
}
return a + b;
• 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.
java
Copy code
class Student {
String name;
int age;
// Constructor 1 (default)
Student() {
name = "Unknown";
age = 0;
}
// Constructor 2 (parameterized)
this.name = name;
this.age = age;
void display() {
4. Programs
java
Copy code
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
java
Copy code
import java.util.Scanner;
if (num % 2 == 0) {
} else {
Factorial Program:
java
Copy code
import java.util.Scanner;
int factorial = 1;
factorial *= i;
• Object-Oriented Programming (OOP) focuses on objects and encapsulating related data and
behaviors. Key concepts in OOP are:
o Encapsulation
o Inheritance
o Polymorphism
o Abstraction
Example:
java
Copy code
class Animal {
String name;
Animal(String name) {
void display() {
String breed;
@Override
void display() {
7. Garbage Collector
• In Java, the Garbage Collector is a part of the JVM that identifies and removes objects that are
no longer used to reclaim memory.