0% found this document useful (0 votes)
6 views4 pages

Import Java

The document contains a Java program that defines an interface 'Displayable' and a base class 'Person' with a child class 'Student' that inherits from it. The main class creates a list of 'Student' objects, sorts them by ID using a lambda expression, and displays their details. It also includes a simple exception handling example for division by zero.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Import Java

The document contains a Java program that defines an interface 'Displayable' and a base class 'Person' with a child class 'Student' that inherits from it. The main class creates a list of 'Student' objects, sorts them by ID using a lambda expression, and displays their details. It also includes a simple exception handling example for division by zero.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

// ✅ INTERFACE

interface Displayable {

void display();

// ✅ BASE CLASS

class Person implements Displayable {

private int id;

private String name;

// ✅ CONSTRUCTOR

Person(int id, String name) {

[Link] = id;

[Link] = name;

// ✅ GETTERS

public int getId() {

return id;

public String getName() {

return name;

// ✅ SETTERS

public void setId(int id) {

[Link] = id;

}
public void setName(String name) {

[Link] = name;

// ✅ INTERFACE METHOD

public void display() {

[Link]("ID: " + id + " Name: " + name);

// ✅ CHILD CLASS (INHERITANCE)

class Student extends Person {

private double marks;

// constructor using super

Student(int id, String name, double marks) {

super(id, name);

[Link] = marks;

// getter

public double getMarks() {

return marks;

// setter

public void setMarks(double marks) {

[Link] = marks;

}
// overriding display

public void display() {

[Link]();

[Link]("Marks: " + marks);

// ✅ MAIN CLASS

public class Main {

public static void main(String[] args) {

// ✅ COLLECTION

ArrayList<Student> list = new ArrayList<>();

[Link](new Student(3, "Amit", 75));

[Link](new Student(1, "Rohit", 85));

[Link](new Student(2, "Neha", 65));

// ✅ LAMBDA (sorting by id)

[Link]((s1, s2) -> [Link]() - [Link]());

// display

for (Student s : list) {

[Link]();

// ✅ SIMPLE EXCEPTION

try {

int x = 10 / 0;

} catch (Exception e) {

[Link]("Exception handled");
}

You might also like