0% found this document useful (0 votes)
5 views

Concept Programs - Docx 1

Uploaded by

harishramaraops
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)
5 views

Concept Programs - Docx 1

Uploaded by

harishramaraops
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/ 29

CONCEPT 1 : INHERITANCE

1.SINGLE INHERITANCE
class Animal {
String name;

public void eat() {


System.out.println(name + " is eating.");
}
}

class Dog extends Animal {


public void bark() {
System.out.println(name + " is barking.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
}
}
OUTPUT :
Buddy is eating.
Buddy is barking.

2. METHOD OVERRIDING

class Vehicle {
public void move() {
System.out.println("The vehicle is moving.");
}
}

class Car extends Vehicle {


@Override // Optional annotation for clarity
public void move() {
System.out.println("The car is driving.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.move(); // Calls Vehicle's move()

Car car = new Car();


car.move(); // Calls Car's overridden move()
}
}
OUTPUT :
The vehicle is moving.
The car is driving.

3. SUPER KEYWORD

class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}

class Circle extends Shape {


@Override
public void draw() {
super.draw(); // Call the superclass's draw()
System.out.println("Drawing a circle.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
}
}
OUTPUT :
Drawing a shape.
Drawing a circle.

4. CONSTRUCTOR CHAINING

class Animal {
public Animal() {
System.out.println("Animal constructor");
}
}

class Dog extends Animal {


public Dog() {
super(); // Call the superclass constructor implicitly
System.out.println("Dog constructor");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Dog dog = new Dog();
}
}

OUTPUT :
Animal constructor
Dog constructor
5. ACCESS MODIFIERS

class Animal {
private String name; // Only accessible within Animal

public void eat() { // Accessible from anywhere


System.out.println(name + " is eating.");
}

// Getter method for private name field (encapsulation)


public String getName() {
return name;
}
}

class Dog extends Animal {


protected String breed; // Accessible within Dog and subclasses

public void bark() {


System.out.println(name + " (" + breed + ") is barking.");
// Access inherited name using getName() for better encapsulation
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Fido"; // Now accessible due to being in the same
package
dog.breed = "Golden Retriever";
dog.eat(); // Public access
System.out.println("Dog's breed (using getter): " + dog.getName());
// Access using getName()
}
}

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

6. INHERITANCE & POLYMORPHISM

interface AnimalInterface {
public void makeSound(); // Abstract method
}

class Cat implements AnimalInterface {


@Override
public void makeSound() {
System.out.println("The cat meows.");
}
}

class Cow implements AnimalInterface {


@Override
public void makeSound() {
System.out.println("The cow moos.");
}
}

public class InheritanceDemo {


public static void makeAnimalSound(AnimalInterface animal) {
animal.makeSound(); // Polymorphic call, actual behavior
determined at runtime
}

public static void main(String[] args) {


Cat cat = new Cat();
Cow cow = new Cow();

makeAnimalSound(cat); // Prints "The cat meows."


makeAnimalSound(cow); // Prints "The cow moos."
}
}
OUTPUT :
The cat meows.
The cow moos.

7. INHERITANCE AND ABSTRACTION

abstract class Animal {


public abstract void makeSound(); // Abstract method, subclasses
must implement
public String getName() {
return "Animal"; // Default implementation
}
}

class Cat extends Animal {


@Override
public void makeSound() {
System.out.println("The cat meows.");
}
}
public class Main {
public static void main(String[] args) {
// Create an Animal object (e.g., a Cat)
Animal animal = new Cat();
animal.makeSound(); // This will print "The cat meows."
}
}
OUTPUT :
The cat meows.

8. HIERARCHICAL INHERITANCE

class Vehicle {
public void move() {
System.out.println("The vehicle is moving.");
}
}

class Car extends Vehicle {


public void drive() {
System.out.println("The car is driving.");
}
}

class Truck extends Vehicle {


public void haulCargo() {
System.out.println("The truck is hauling cargo.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Car car = new Car();
Truck truck = new Truck();
car.move(); // Inherited from Vehicle
car.drive(); // Specific to Car

truck.move(); // Inherited from Vehicle


truck.haulCargo(); // Specific to Truck
}
}

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.");
}
}

class Mammal extends Animal {


public void giveBirth() {
System.out.println("The mammal is giving birth.");
}
}
class Dog extends Mammal {
public void bark() {
System.out.println("The dog is barking.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.giveBirth(); // Inherited from Mammal
dog.bark(); // Specific to Dog
}
}

OUTPUT:
The animal is eating.
The mammal is giving birth.
The dog is barking.

10. HYBRID INHERITANCE WITH INTERFACE

interface Movable {
public void move();
}

class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}

class Vehicle implements Movable {


@Override
public void move() {
System.out.println("The vehicle is moving.");
}
}

class FlyingAnimal extends Animal implements Movable {


@Override
public void move() {
System.out.println("The flying animal is flying.");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
Vehicle car = new Vehicle();
FlyingAnimal bird = new FlyingAnimal();

car.move(); // Implements Movable interface


bird.eat(); // Inherited from Animal
bird.move(); // Overrides Animal's move() (polymorphism)
}
}
OUTPUT:
The vehicle is moving.
The animal is eating.
The flying animal is flying.
CONCEPT 2 : POLYMORPHISM

1.COMPILE TIME POLYMORPHISM

class Calculator {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {


return a + b;
}
}

public class Main {


public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 3)); // Calls int add(int, int)
System.out.println(calc.add(5.2, 7.8)); // Calls double add(double,
double)
}
}

OUTPUT:
8
13.0
2. RUNTIME POLYMORPHISM

class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}

class Dog extends Animal {


@Override // Explicitly indicate overriding
public void makeSound() {
System.out.println("Woof!");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Animal(); // Can refer to any subclass at
runtime
animal.makeSound(); // Outputs: Generic animal sound

Animal dogAnimal = new Dog(); // Upcasting (polymorphism)


dogAnimal.makeSound(); // Outputs: Woof! (runtime
polymorphism)
}
}
OUTPUT:
Generic animal sound
Woof!

3. INTERFACE POLYMORPHISM

interface Drawable {
void draw();
}

class Square implements Drawable {


@Override
public void draw() {
System.out.println("Drawing a square");
}
}

class Triangle implements Drawable {


@Override
public void draw() {
System.out.println("Drawing a triangle");
}
}

public class Main {


public static void drawShape(Drawable shape) {
shape.draw(); // Polymorphism via interface
}
public static void main(String[] args) {
drawShape(new Square()); // Outputs: Drawing a square
drawShape(new Triangle()); // Outputs: Drawing a triangle
}
}

OUTPUT:
Drawing a square
Drawing a triangle

4. ABSTRACT CLASS AND POLYMORPHISM

abstract class Vehicle {


public abstract void move(); // Abstract method forces subclasses to
implement
}

class Car extends Vehicle {


@Override
public void move() {
System.out.println("Car is driving");
}
}

class Bike extends Vehicle {


@Override
public void move() {
System.out.println("Bike is cycling");
}
}

public class Main {


public static void moveVehicle(Vehicle vehicle) {
vehicle.move(); // Polymorphism via abstract class
}

public static void main(String[] args) {


moveVehicle(new Car()); // Outputs: Car is driving
moveVehicle(new Bike()); // Outputs: Bike is cycling
}
}

OUTPUT :
Car is driving
Bike is cycling
CONCEPT 3 : ENCAPSULATION

1.DECLARING PRIVATE VARIABLES

class Account {
private int balance; // Encapsulated data member
}

2. ACCESSING PRIVATE VARIABLES WITH GETTER


METHODS

class Account {
private int balance;

public int getBalance() {


return balance;
}
}

3. MODIFYING PRIVATE VARIABLES WITH SETTER


METHODS (VALIDATION EXAMPLE)

class Account {
private int balance;

public void setBalance(int amount) {


if (amount >= 0) {
balance = amount;
} else {
System.out.println("Invalid balance: Cannot be negative.");
}
}
}

4. USING GETTERS AND SETTERS IN A SIMPLE PROGRAM

class Account {
private int balance;

public void setBalance(int amount) {


if (amount >= 0) {
balance = amount;
} else {
System.out.println("Invalid balance: Cannot be negative.");
}
}

public int getBalance() {


return balance;
}
}

public class Main {


public static void main(String[] args) {
Account account = new Account();
account.setBalance(100); // Setting balance using setter

int currentBalance = account.getBalance(); // Retrieving balance using


getter
System.out.println("Current balance: " + currentBalance);
}
}

OUTPUT :
Current balance: 100

5. READ-ONLY CLASS (USING ONLY GETTERS)

class Address {
private String street;
private String city;
private String state;
private String zipcode;

public Address(String street, String city, String state, String zipcode) {


this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}

public String getStreet() {


return street;
}

public String getCity() {


return city;
}

public String getState() {


return state;
}

public String getZipcode() {


return zipcode;
}
}

public class Main {


public static void main(String[] args) {
// Create an Address object
Address address = new Address("123 Main St", "Anytown", "CA",
"12345");

// Print the address information using string concatenation


System.out.println("Street: " + address.getStreet());
System.out.println("City: " + address.getCity());
System.out.println("State: " + address.getState());
System.out.println("Zipcode: " + address.getZipcode());
}
}
OUTPUT:
Street: 123 Main St
City: Anytown
State: CA
Zipcode: 12345

6. CONSTRUCTOR WITH ENCAPSULATION

class Student {
private String name;
private int age;

public Student(String name, int age) {


if (age >= 0) { // Validation in constructor
this.name = name;
this.age = age;
} else {
System.out.println("Invalid age: Cannot be negative.");
}
}

public String getName() {


return name;
}

public int getAge() {


return age;
}
}
public class Main {
public static void main(String[] args) {
// Create a Student object
Student student1 = new Student("Alice", 20);

// Print the student information


System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
}
}

OUTPUT :
Name: Alice
Age: 20

7. FULL-FLEDGED PROGRAM WITH ENCAPSULATION

class Product {
private int id;
private String name;
private double price;

public Product(int id, String name, double price) {


this.id = id;
this.name = name;
this.price = price;
}

public int getId() {


return id;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}

// Additional methods for product management (e.g., discount


calculation)
}

public class ProductStore {


public static void main(String[] args) {
Product product1 = new Product(101, "T-Shirt", 24.99);
Product product2 = new Product(102, "Jeans", 49.95);

System.out.println("Product 1: " + product1.getName() + " (ID: " +


product1.getId() + ", Price: $" + product1.getPrice() + ")");
System.out.println("Product 2: " + product2.getName() + " (ID: " +
product2.getId() + ", Price: $" + product2.getPrice() + ")");
}
}
OUTPUT:
Product 1: T-Shirt (ID: 101, Price: $24.99)
Product 2: Jeans (ID: 102, Price: $49.95)

CONCEPT 4 : ABSTRACTION

1.BASIC ABSTRACTION

abstract class Shape {


public abstract double getArea();
}

class Circle extends Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

class Square extends Shape {


private double sideLength;

public Square(double sideLength) {


this.sideLength = sideLength;
}

@Override
public double getArea() {
return sideLength * sideLength;
}
}

public class Main {


public static void main(String[] args) {
Shape circle = new Circle(5);
Shape square = new Square(4);

System.out.println("Circle Area: " + circle.getArea());


System.out.println("Square Area: " + square.getArea());
}
}

OUTPUT :
Circle Area: 78.53981633974483
Square Area: 16.0
2. INTERFACES FOR BEHAVIOR ABSTRACTION

interface Drawable {
void draw();
}

class Rectangle implements Drawable {


private double width, height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

@Override
public void draw() {
System.out.println("Drawing a rectangle with width: " + width + ",
height: " + height);
}
}

class Triangle implements Drawable {


private double base, height;

public Triangle(double base, double height) {


this.base = base;
this.height = height;
}

@Override
public void draw() {
System.out.println("Drawing a triangle with base: " + base + ",
height: " + height);
}
}

public class Main {


public static void main(String[] args) {
Drawable rectangle = new Rectangle(5, 3);
Drawable triangle = new Triangle(4, 6);

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

You might also like