Untitled Document
Untitled Document
Untitled Document
```java
//HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello");
}
}
```
**Output:**
```
Hello
```
```java
// AbstractClass.java
abstract class Shape {
abstract double getArea();
}
@Override
double getArea() {
return length * width;
}
}
Circle(double radius) {
this.radius = radius;
}
@Override
double getArea() {
return Math.PI * radius * radius;
}
}
**Output:**
```
Area: 50.0
Area: 28.274333882308138
```
```java
// InheritanceExample.java
class Animal {
String name;
Animal(String name) {
this.name = name;
}
@Override
public void speak() {
System.out.println("Dog barks...");
}
}
class Cat extends Animal {
Cat(String name) {
super(name);
}
@Override
public void speak() {
System.out.println("Cat meows...");
}
}
animal.speak();
dog.speak();
cat.speak();
}
}
```
**Output:**
```
Animal says...
Dog barks...
Cat meows...
```
**4. Java Program to Find the Fibonacci Series Using Recursive Functions**
```java
// Fibonacci.java
public class Fibonacci {
public static int fib(int n) {
if (n <= 1) {
return n;
}
**Output:**
```
0 1 1 2 3 5 8 13 21 34
```
```java
// MethodOverloading.java
class Calculator {
int sum(int a, int b) {
return a + b;
}
**Output:**
```
30
27.8
```
```java
// InnerClassExample.java
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
innerClass.display();
}
}
```
**Output:**
```
x = 10
y=5
```
```java
// ConstructorOverloading.java
class Student {
int rollNumber;
String name;
Student() {
// Default constructor
}
Student(int rollNumber) {
this.rollNumber = rollNumber;
}
**Output:**
```
0 null
10 null
20 John
```
```java
// PolymorphismExample.java
class Animal {
public void speak() {
System.out.println("Animal says...");
}
}
**Output:**
```
Animal says...
Dog barks...
Cat meows...
```
```java
// EncapsulationAndOverridingExample.java
class Animal {
private String name;
**Output:**
```
Animal's name: Animal
Animal says...
Dog's name: Dog
Dog barks...
```
```java
// ArrayListExample.java
import java.util.ArrayList;
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println(numbers);
**Output:**