Complete Example of Inheritance and Interfaces
package untitled146;
import javax.swing.*;
public class Shepherd_workstudy
{
private String name;
public Shepherd_workstudy()
{
}
public static void main(String[] args)
{
new Shepherd_workstudy();
Student stu = new Student();
Employee work = new Employee();
stu.get_age();
int s_age=stu.return_age();
work.get_age();
int w_age=work.return_age();
JOptionPane.showMessageDialog(null," "+w_age +" "+s_age);
College_Student col = new College_Student();
col.get_Name();
String na=col.return_college_name();
JOptionPane.showMessageDialog(null,"name of College "+na);
String g=work.interface_constant;
JOptionPane.showMessageDialog(null," "+g);
// cast col object to be of Student type
Student nn= (Student) col;
// col. look at method list
// nn. look at method list
College_Student cc =(College_Student) stu;
this.
System.exit(0);
}
}
**********************************************************
package untitled146;
interface Int1
{
final String interface_constant="Shared by student/employee";
public abstract void get_age();
public abstract int return_age();
}
**********************************************************
package untitled146;
import javax.swing.*;
public class Employee implements Int1
{
private int employee_age;
public Employee()
{
}
public void get_age()
{
String s = JOptionPane.showInputDialog("enter age ");
employee_age=Integer.parseInt(s);
}
public int return_age()
{
return employee_age;
}
}
**********************************************************package untitled146;
import javax.swing.*;
public class Student implements Int1
{
private int student_age;
public Student()
{
}
public void get_age()
{
String s = JOptionPane.showInputDialog("enter age ");
student_age=Integer.parseInt(s);
}
public int return_age()
{
return student_age;
}
}
*******************************************************
package untitled146;
import javax.swing.*;
public class College_Student extends Student
{
private String Name_of_college;
public College_Student()
{
}
public void get_Name()
{
Name_of_college = JOptionPane.showInputDialog("enter name of college ");
}
public String return_college_name()
{
return Name_of_college;
}
}