Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Java Programs All 25

The document contains various Java programming examples illustrating concepts such as static methods, final methods and variables, static blocks, inheritance, constructor chaining, and encapsulation. Each example demonstrates specific functionalities, including calculating the area of a circle, managing employee data, and implementing a bank account system. The code snippets highlight key object-oriented programming principles and usage in Java.
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)
8 views9 pages

Java Programs All 25

The document contains various Java programming examples illustrating concepts such as static methods, final methods and variables, static blocks, inheritance, constructor chaining, and encapsulation. Each example demonstrates specific functionalities, including calculating the area of a circle, managing employee data, and implementing a bank account system. The code snippets highlight key object-oriented programming principles and usage in Java.
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

1.

Static method to calculate area of a circle

public class CircleArea {


public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}

public static void main(String[] args) {


System.out.println("Area: " + calculateArea(5));
}
}

2. Final method in superclass and attempt to override

class Parent {
public final void display() {
System.out.println("Final method in Parent");
}
}

class Child extends Parent {


// Uncommenting below will cause a compile-time error
// public void display() {
// System.out.println("Trying to override");
// }
}

3. Final variable initialized in constructor

public class FinalVariable {


final int value;

public FinalVariable(int val) {


value = val;
}

public void show() {


System.out.println("Value: " + value);
}

public static void main(String[] args) {


FinalVariable obj = new FinalVariable(10);
obj.show();
}
}

4. Static block for class initialization

public class StaticBlockExample {


static {
System.out.println("Static block called before main");
}

public static void main(String[] args) {


System.out.println("Main method executed");
}
}

5. Static variable shared among instances

public class StaticVariableDemo {


static int count = 0;

public StaticVariableDemo() {
count++;
}

public static void main(String[] args) {


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

6. Employee and Manager inheritance

class Employee {
String name;
double salary;

Employee(String name, double salary) {


this.name = name;
this.salary = salary;
}
}

class Manager extends Employee {


double bonus;

Manager(String name, double salary, double bonus) {


super(name, salary);
this.bonus = bonus;
}

public void display() {


System.out.println("Name: " + name + ", Total Salary: " + (salary + bonus));
}
}

7. Constructor chaining with super()

class A {
A() {
System.out.println("Constructor A");
}
}

class B extends A {
B() {
super();
System.out.println("Constructor B");
}

public static void main(String[] args) {


B obj = new B();
}
}
1. Static method to calculate area of a circle

public class CircleArea {


public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}

public static void main(String[] args) {


System.out.println("Area: " + calculateArea(5));
}
}

2. Final method in superclass and attempt to override

class Parent {
public final void display() {
System.out.println("Final method in Parent");
}
}

class Child extends Parent {


// Uncommenting below will cause a compile-time error
// public void display() {
// System.out.println("Trying to override");
// }
}

3. Final variable initialized in constructor

public class FinalVariable {


final int value;

public FinalVariable(int val) {


value = val;
}

public void show() {


System.out.println("Value: " + value);
}

public static void main(String[] args) {


FinalVariable obj = new FinalVariable(10);
obj.show();
}
}

4. Static block for class initialization

public class StaticBlockExample {


static {
System.out.println("Static block called before main");
}

public static void main(String[] args) {


System.out.println("Main method executed");
}
}

5. Static variable shared among instances

public class StaticVariableDemo {


static int count = 0;

public StaticVariableDemo() {
count++;
}

public static void main(String[] args) {


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

6. Employee and Manager inheritance

class Employee {
String name;
double salary;

Employee(String name, double salary) {


this.name = name;
this.salary = salary;
}
}

class Manager extends Employee {


double bonus;

Manager(String name, double salary, double bonus) {


super(name, salary);
this.bonus = bonus;
}

public void display() {


System.out.println("Name: " + name + ", Total Salary: " + (salary + bonus));
}
}

7. Constructor chaining with super()

class A {
A() {
System.out.println("Constructor A");
}
}

class B extends A {
B() {
super();
System.out.println("Constructor B");
}

public static void main(String[] args) {


B obj = new B();
}
}
class A {
void greet() {
System.out.println("Hello from A");
}
}

class B extends A {}
class C extends B {}

public class Test {


public static void main(String[] args) {
C obj = new C();
obj.greet();
}
}

11. BankAccount encapsulation

public class BankAccount {


private int accountNumber;
private double balance;

public int getAccountNumber() {


return accountNumber;
}

public void setAccountNumber(int number) {


accountNumber = number;
}

public double getBalance() {


return balance;
}

public void setBalance(double amount) {


balance = amount;
}
}

12. Student with encapsulation

public class Student {


private int rollNumber;
private String name;
private String grade;

public void setData(int r, String n, String g) {


rollNumber = r;
name = n;
grade = g;
}

public void display() {


System.out.println("Roll: " + rollNumber + ", Name: " + name + ", Grade: " +
grade);

You might also like