Menu


Notice: Undefined index: alg in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 82

Notice: Undefined index: url2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 84

Notice: Undefined index: url3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 85

Notice: Undefined index: url4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 86

Notice: Undefined index: opurl2 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 89

Notice: Undefined index: opurl3 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 90

Notice: Undefined index: opurl4 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 91

Notice: Undefined index: opurl5 in /home/u681245571/domains/studyglance.in/public_html/labprograms/r22javadisplay.php on line 92

Java Programming [ Lab Programs ]


Aim:

Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance, Polymorphism and Abstraction]

Source Code:

OopPrinciplesDemo.java


/* Encapsulation: 

   The fields of the class are private and accessed through getter and setter methods.*/
class Person {
    // private fields
    private String name;
    private int age;
    // constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    // getter and setter methods
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    /* Abstraction: 
       The displayInfo() method provides a simple interface to interact with the object.*/
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

/* Inheritance: 
   Employee is a subclass of Person, inheriting its properties and methods.*/
class Employee extends Person {
    // private field
    private double salary;
    // constructor
    public Employee(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }
    // getter and setter methods
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }

    /* Polymorphism:
       Overriding the displayInfo() method to provide a specific implementation for Employee.*/
    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Salary: " + salary);
    }
}

public class OopPrinciplesDemo {
    public static void main(String[] args) {
        // Demonstrating encapsulation and abstraction
        Person person = new Person("Madhu", 30);
        System.out.println("Person Info:");
        person.displayInfo();
        System.out.println("====================");

        // Demonstrating inheritance and polymorphism
        Employee employee = new Employee("Naveen", 26, 50000);
        System.out.println("Employee Info:");
        employee.displayInfo();
    }
}

Output:

image

Related Content :

Java Programming Lab Programs

1) Use eclipse or Netbean platform and acquaint with the various menus, create a test project, add a test class and run it see how you can use auto suggestions, auto fill. Try code formatter and code refactoring like renaming variables, methods and classes. Try debug step by step with a small program of about 10 to 15 lines which contains at least one if else condition and a for loop. View Solution

2) Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance, Polymorphism and Abstraction] View Solution

3) Write a Java program to handle checked and unchecked exceptions. Also, demonstrate the usage of custom exceptions in real time scenario. View Solution

4) Write a Java program on Random Access File class to perform different read and write operations. View Solution

5) Write a Java program to demonstrate the working of different collection classes. [Use package structure to store multiple classes]. View Solution

6) Write a program to synchronize the threads acting on the same object. [Consider the example of any reservations like railway, bus, movie ticket booking, etc.] View Solution

7) Write a program to perform CRUD operations on the student table in a database using JDBC. View Solution

8) Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible exceptions like divided by zero. View Solution

9) Write a Java program that handles all mouse events and shows the event name at the center of the window when a mouse event is fired. [Use Adapter classes] View Solution