# Java program to print student details using "single-level" inheritance
In this section, You will learn how to print student details using single inheritance in java, with Scanner and without Scanner class.
1.) With "Scanner" class
Let's try to create a simple example :
➤ Example : student.java;
import java.util.Scanner;
class StudentDetails {
int roll_no;
String name, cl;
//creating a function to take student details
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll Number: ");
roll_no = sc.nextInt(); //=> 22\n == buffer
sc.nextLine(); // => \n
System.out.print("Enter Name: ");
name = sc.nextLine();
System.out.print("Enter class: ");
cl = sc.nextLine();
}
}
class Student extends StudentDetails {
//method to display student details
void display() {
System.out.println("/******* Student details printed ********/");
System.out.println("Roll Number is: "+roll_no);
System.out.println("Your name is: "+name);
System.out.println("Your class is: "+cl);
}
public static void main(String args[]) {
Student obj = new Student();
obj.input();
obj.display();
}
}
Output :
Enter Roll Number: 22
Enter Name: Coder Website
Enter class: 12th
/******* Student details printed ********/
Roll Number is: 22
Your name is: Coder Website
Your class is: 12th
2.) Without "Scanner" class
Let's try to create a simple example :
➤ Example 2: student.java;
class StudentDetails {
int roll_no;
String name, cl;
//creating a function to take student details
void input(int roll, String nam, String cl_name) {
roll_no = roll;
name = nam;
cl = cl_name;
}
}
class Student extends StudentDetails {
//method to display student details
void display() {
System.out.println("\n/******* Student details printed ********/");
System.out.println("Roll Number is: "+roll_no);
System.out.println("Your name is: "+name);
System.out.println("Your class is: "+cl);
}
public static void main(String args[]) {
Student std1 = new Student();
std1.input(20, "Ankaj Gupta", "12th");
std1.display();
Student std2 = new Student();
std2.input(10, "Anmol kumar", "10th");
std2.display();
}
}
Output :
/******* Student details printed ********/
Roll Number is: 20
Your name is: Ankaj Gupta
Your class is: 12th
/******* Student details printed ********/
Roll Number is: 10
Your name is: Anmol kumar
Your class is: 10th
Comments