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

Java_Assignment2_Full

The document outlines various Java programming concepts including Method Overloading, Static keyword, Nesting of Methods, Inheritance, Method Overriding, Final keyword, Finalization, VARARGS, Abstract Classes, Dynamic Method Dispatch, and Visibility Control. Each concept is explained with a theoretical description and accompanied by example code snippets demonstrating their usage. The document serves as an educational resource for understanding fundamental Java programming principles.

Uploaded by

nikku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views9 pages

Java_Assignment2_Full

The document outlines various Java programming concepts including Method Overloading, Static keyword, Nesting of Methods, Inheritance, Method Overriding, Final keyword, Finalization, VARARGS, Abstract Classes, Dynamic Method Dispatch, and Visibility Control. Each concept is explained with a theoretical description and accompanied by example code snippets demonstrating their usage. The document serves as an educational resource for understanding fundamental Java programming principles.

Uploaded by

nikku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Assignment 2: Java Programming Concepts

1. Method Overloading

Question: Explain Method Overloading and provide an example in Java.

Theory: Method Overloading allows multiple methods in the same class to have the same name but differe

It is achieved by changing the number of parameters or their types.

Code:

class OverloadingExample {

void display(int a) {

System.out.println("Integer: " + a);

void display(String b) {

System.out.println("String: " + b);

public static void main(String[] args) {

OverloadingExample obj = new OverloadingExample();

obj.display(10);

obj.display("Hello");

// Output:

// Integer: 10

// String: Hello

2. Static

Question: Explain the Static keyword in Java and provide an example.

Theory: The static keyword in Java is used for memory management. It can be applied to variables, method
Static members belong to the class rather than any specific instance.

Code:

class StaticExample {

static int count = 0;

StaticExample() {

count++;

static void showCount() {

System.out.println("Count: " + count);

public static void main(String[] args) {

new StaticExample();

new StaticExample();

StaticExample.showCount();

// Output:

// Count: 2

3. Nesting of Methods

Question: What is Nesting of Methods in Java? Provide an example.

Theory: Nesting of methods refers to calling one method inside another method within the same class.

Code:

class NestingExample {

void outerMethod() {

System.out.println("Outer method");

innerMethod();
}

void innerMethod() {

System.out.println("Inner method");

public static void main(String[] args) {

NestingExample obj = new NestingExample();

obj.outerMethod();

// Output:

// Outer method

// Inner method

4. Inheritance

Question: Explain Inheritance in Java with an example.

Theory: Inheritance is a mechanism where one class (child) acquires the properties and behaviors of anoth

Code:

class Parent {

void show() {

System.out.println("Parent class");

class Child extends Parent {

void display() {

System.out.println("Child class");

}
public class InheritanceExample {

public static void main(String[] args) {

Child obj = new Child();

obj.show();

obj.display();

// Output:

// Parent class

// Child class

5. Overriding

Question: What is Method Overriding in Java? Explain with an example.

Theory: Method Overriding occurs when a subclass provides a specific implementation of a method already

Code:

class Base {

void show() {

System.out.println("Base class");

class Derived extends Base {

@Override

void show() {

System.out.println("Derived class");

public class OverridingExample {


public static void main(String[] args) {

Base obj = new Derived();

obj.show();

// Output:

// Derived class

6. Final Keyword, Variable & Methods

Question: Explain the Final keyword in Java and provide an example.

Theory: The final keyword is used to restrict modification. It can be applied to variables (to make them cons

methods (to prevent overriding), and classes (to prevent inheritance).

Code:

class FinalExample {

final int value = 100;

final void show() {

System.out.println("Final method");

public static void main(String[] args) {

FinalExample obj = new FinalExample();

System.out.println("Final variable: " + obj.value);

obj.show();

// Output:

// Final variable: 100

// Final method
7. Finalization

Question: What is Finalization in Java? Explain with an example.

Theory: Finalization is a mechanism where an object's cleanup code is executed before it is garbage collec

Code:

class FinalizationExample {

protected void finalize() {

System.out.println("Object is garbage collected");

public static void main(String[] args) {

FinalizationExample obj = new FinalizationExample();

obj = null;

System.gc();

// Output:

// Object is garbage collected

8. VARARGS

Question: What is VARARGS in Java? Provide an example.

Theory: VARARGS (Variable-Length Arguments) allows passing multiple arguments of the same type to a

Code:

class VarargsExample {

static void display(int... numbers) {

for (int num : numbers) {

System.out.print(num + " ");

}
System.out.println();

public static void main(String[] args) {

display(1, 2, 3);

display(10, 20, 30, 40, 50);

// Output:

// 1 2 3

// 10 20 30 40 50

9. Abstract and Class

Question: Explain Abstract Class in Java with an example.

Theory: An abstract class is a class that cannot be instantiated and can contain abstract methods (without

Code:

abstract class AbstractExample {

abstract void show();

class ConcreteClass extends AbstractExample {

void show() {

System.out.println("Concrete implementation");

public class AbstractTest {

public static void main(String[] args) {

AbstractExample obj = new ConcreteClass();

obj.show();
}

// Output:

// Concrete implementation

10. Dynamic Method Dispatch

Question: What is Dynamic Method Dispatch? Provide an example.

Theory: Dynamic Method Dispatch is a mechanism where a method call is resolved at runtime based on th

Code:

class ParentClass {

void display() {

System.out.println("Parent display");

class ChildClass extends ParentClass {

void display() {

System.out.println("Child display");

public class DynamicDispatchExample {

public static void main(String[] args) {

ParentClass obj = new ChildClass();

obj.display();

// Output:

// Child display
11. Visibility Control

Question: Explain Visibility Control in Java.

Theory: Visibility control determines the accessibility of class members using access modifiers: private, pro

Code:

class VisibilityExample {

private int privateVar = 10;

protected int protectedVar = 20;

public int publicVar = 30;

int defaultVar = 40;

void show() {

System.out.println("Private: " + privateVar);

System.out.println("Protected: " + protectedVar);

System.out.println("Public: " + publicVar);

System.out.println("Default: " + defaultVar);

public static void main(String[] args) {

VisibilityExample obj = new VisibilityExample();

obj.show();

// Output:

// Private: 10

// Protected: 20

// Public: 30

// Default: 40

You might also like