1.
Command Line Arguments
Print command line arguments passed to the program.
java
public class CommandLineArgs {
public static void main(String[] args) {
for (String arg : args) {
[Link](arg);
}
}
}
Run: java CommandLineArgs Hello World
Output:
Hello
World
2. String Operations
Perform basic string operations like concatenation, substring, and length.
java
public class StringOperations {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
[Link](str1 + " " + str2); // Concatenation
[Link]([Link](1, 3)); // Substring
[Link]([Link]()); // Length
}
}
Output:
Hello World
el
5
3. Method Overloading
Demonstrate method overloading by defining multiple methods with the
same name but different parameters.
java
public class MethodOverloading {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
[Link](add(5, 10)); // Output: 15
[Link](add(5.5, 10.5)); // Output: 16.0
}
}
4. Method Overriding
Demonstrate method overriding by redefining a method in a subclass.
java
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
}
}
public class MethodOverriding {
public static void main(String[] args) {
Animal dog = new Dog();
[Link](); // Output: Dog barks
}
}
5. Static Keyword
Demonstrate the use of the static keyword for variables and methods.
java
public class StaticExample {
static int count = 0;
StaticExample() {
count++;
}
static void displayCount() {
[Link]("Count: " + count);
}
public static void main(String[] args) {
new StaticExample();
new StaticExample();
[Link](); // Output: Count: 2
}
}
6. Variable-Length Arguments
Demonstrate the use of variable-length arguments (varargs).
java
public class VarargsExample {
static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
public static void main(String[] args) {
[Link](sum(1, 2, 3)); // Output: 6
[Link](sum(10, 20, 30, 40)); // Output: 100
}
}
7. Default Constructor
Demonstrate the use of a default constructor.
java
class Student {
String name;
Student() {
name = "Unknown";
}
void display() {
[Link]("Name: " + name);
}
}
public class DefaultConstructor {
public static void main(String[] args) {
Student student = new Student();
[Link](); // Output: Name: Unknown
}
}
8. Parameterized Constructor
Demonstrate the use of a parameterized constructor.
java
class Student {
String name;
Student(String name) {
[Link] = name;
}
void display() {
[Link]("Name: " + name);
}
}
public class ParameterizedConstructor {
public static void main(String[] args) {
Student student = new Student("Alice");
[Link](); // Output: Name: Alice
}
}
9. this Keyword
Demonstrate the use of the this keyword to refer to the current object.
java
class Student {
String name;
Student(String name) {
[Link] = name; // 'this' refers to the current object
}
void display() {
[Link]("Name: " + [Link]);
}
}
public class ThisKeyword {
public static void main(String[] args) {
Student student = new Student("Bob");
[Link](); // Output: Name: Bob
}
}
10. super Keyword
Demonstrate the use of the super keyword to call the parent class
constructor.
java
class Animal {
Animal() {
[Link]("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the parent class constructor
[Link]("Dog constructor");
}
}
public class SuperKeyword {
public static void main(String[] args) {
new Dog();
}
}
Output:
Animal constructor
Dog constructor
11. Access Modifiers
Demonstrate the use of access modifiers (public, private, protected,
default).
java
class AccessExample {
public int publicVar = 1;
private int privateVar = 2;
protected int protectedVar = 3;
int defaultVar = 4;
void display() {
[Link](privateVar); // Accessible within the class
}
}
public class AccessModifiers {
public static void main(String[] args) {
AccessExample obj = new AccessExample();
[Link]([Link]); // Accessible
// [Link]([Link]); // Not accessible
[Link]([Link]); // Accessible
[Link]([Link]); // Accessible
}
}
12. Inheritance
Demonstrate inheritance by creating a subclass.
java
class Vehicle {
void run() {
[Link]("Vehicle is running");
}
}
class Car extends Vehicle {
void accelerate() {
[Link]("Car is accelerating");
}
}
public class Inheritance {
public static void main(String[] args) {
Car car = new Car();
[Link](); // Inherited method
[Link](); // Subclass method
}
}
13. Abstract Class
Demonstrate the use of an abstract class.
java
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing a circle");
}
}
public class AbstractClass {
public static void main(String[] args) {
Shape shape = new Circle();
[Link](); // Output: Drawing a circle
}
}
14. final Keyword
Demonstrate the use of the final keyword for variables, methods, and
classes.
java
final class FinalClass {
final int finalVar = 10;
final void display() {
[Link]("Final method");
}
}
public class FinalKeyword {
public static void main(String[] args) {
FinalClass obj = new FinalClass();
[Link]([Link]); // Output: 10
[Link](); // Output: Final method
}
}
15. Polymorphism
Demonstrate runtime polymorphism using method overriding.
java
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
}
}
public class Polymorphism {
public static void main(String[] args) {
Animal animal = new Dog(); // Upcasting
[Link](); // Output: Dog barks
}
}
16. Interface
Demonstrate the use of an interface.
java
interface Drawable {
void draw();
}
class Circle implements Drawable {
@Override
public void draw() {
[Link]("Drawing a circle");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Drawable drawable = new Circle();
[Link](); // Output: Drawing a circle
}
}
17. Packages
Demonstrate the use of packages.
java
// File: com/example/[Link]
package [Link];
public class MyClass {
public void display() {
[Link]("Hello from MyClass");
}
}
// File: [Link]
import [Link];
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link](); // Output: Hello from MyClass
}
}
18. Multiple Inheritance Using Interfaces
Demonstrate multiple inheritance using interfaces.
java
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
@Override
public void methodA() {
[Link]("Method A");
}
@Override
public void methodB() {
[Link]("Method B");
}
}
public class MultipleInheritance {
public static void main(String[] args) {
C obj = new C();
[Link](); // Output: Method A
[Link](); // Output: Method B
}
}
19. Abstract Method in Interface
Demonstrate abstract methods in an interface.
java
interface Drawable {
void draw();
}
class Circle implements Drawable {
@Override
public void draw() {
[Link]("Drawing a circle");
}
}
public class AbstractMethodInterface {
public static void main(String[] args) {
Drawable drawable = new Circle();
[Link](); // Output: Drawing a circle
}
}
20. Static Method in Interface
Demonstrate static methods in an interface.
java
interface MathOperations {
static int add(int a, int b) {
return a + b;
}
}
public class StaticMethodInterface {
public static void main(String[] args) {
[Link]([Link](5, 10)); // Output: 15
}
}
21. Default Method in Interface
Demonstrate default methods in an interface.
java
interface Vehicle {
default void start() {
[Link]("Vehicle started");
}
}
class Car implements Vehicle {
// Uses the default method
}
public class DefaultMethodInterface {
public static void main(String[] args) {
Car car = new Car();
[Link](); // Output: Vehicle started
}
}
22. Final Variable in Interface
Demonstrate final variables in an interface.
java
interface Constants {
double PI = 3.14; // By default, public, static, and final
}
public class FinalVariableInterface {
public static void main(String[] args) {
[Link]("Value of PI: " + [Link]); // Output: Value
of PI: 3.14
}
}