Concept Programs - Docx 1
Concept Programs - Docx 1
1.SINGLE INHERITANCE
class Animal {
String name;
2. METHOD OVERRIDING
class Vehicle {
public void move() {
System.out.println("The vehicle is moving.");
}
}
3. SUPER KEYWORD
class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}
4. CONSTRUCTOR CHAINING
class Animal {
public Animal() {
System.out.println("Animal constructor");
}
}
OUTPUT :
Animal constructor
Dog constructor
5. ACCESS MODIFIERS
class Animal {
private String name; // Only accessible within Animal
OUTPUT :
/tmp/Ip8n0AJNSo/InheritanceDemo.java:18: error: name has private
access in Animal
System.out.println(name + " (" + breed + ") is barking.");
^
ERROR!
/tmp/Ip8n0AJNSo/InheritanceDemo.java:26: error: name has private
access in Animal
dog.name = "Fido"; // Now accessible due to being in the same
package
^
2 errors
interface AnimalInterface {
public void makeSound(); // Abstract method
}
8. HIERARCHICAL INHERITANCE
class Vehicle {
public void move() {
System.out.println("The vehicle is moving.");
}
}
OUTPUT :
The vehicle is moving.
The car is driving.
The vehicle is moving.
The truck is hauling cargo.
9. MULTIPLE INHERITANCE
class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}
OUTPUT:
The animal is eating.
The mammal is giving birth.
The dog is barking.
interface Movable {
public void move();
}
class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}
class Calculator {
public int add(int a, int b) {
return a + b;
}
OUTPUT:
8
13.0
2. RUNTIME POLYMORPHISM
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
3. INTERFACE POLYMORPHISM
interface Drawable {
void draw();
}
OUTPUT:
Drawing a square
Drawing a triangle
OUTPUT :
Car is driving
Bike is cycling
CONCEPT 3 : ENCAPSULATION
class Account {
private int balance; // Encapsulated data member
}
class Account {
private int balance;
class Account {
private int balance;
class Account {
private int balance;
OUTPUT :
Current balance: 100
class Address {
private String street;
private String city;
private String state;
private String zipcode;
class Student {
private String name;
private int age;
OUTPUT :
Name: Alice
Age: 20
class Product {
private int id;
private String name;
private double price;
CONCEPT 4 : ABSTRACTION
1.BASIC ABSTRACTION
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
@Override
public double getArea() {
return sideLength * sideLength;
}
}
OUTPUT :
Circle Area: 78.53981633974483
Square Area: 16.0
2. INTERFACES FOR BEHAVIOR ABSTRACTION
interface Drawable {
void draw();
}
@Override
public void draw() {
System.out.println("Drawing a rectangle with width: " + width + ",
height: " + height);
}
}
@Override
public void draw() {
System.out.println("Drawing a triangle with base: " + base + ",
height: " + height);
}
}
rectangle.draw();
triangle.draw();
}
}
OUTPUT:
Drawing a rectangle with width: 5.0, height: 3.0
Drawing a triangle with base: 4.0, height: 6.0