Inheritance:
Inheritance is a process where one class acquires the properties (variable and methods) of another class.
Importance:
Code:
Inheritance is a process where one class acquires the properties (variable and methods) of another class.
Importance:
- For code reusability.
- For method overloading.
- Implement parent-child relationship.
Code:
- // Base class
- public class StudentI {
- int id;
- String name;
- String dept;
- public void dispInfo() {
- System.out.println("Id : "+id);
- System.out.println("Name : "+name);
- System.out.println("Department : "+dept);
- }
- }
- // Derived class
- public class TeacherI extends StudentI { //inherit class studenti
- int age;
- public void disp() {
- dispInfo();
- System.out.println("Age : "+age);
- }
- }
- // Main class
- public class InheritanceDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- StudentI s1=new StudentI();
- s1.id=1001;
- s1.name="Sujan Hasan";
- s1.dept="CSE";
- s1.dispInfo();
- System.out.println();
- StudentI s2=new StudentI();
- s2.id=1002;
- s2.name="Shakil Sorker";
- s2.dept="CSE";
- s2.dispInfo();
- System.out.println();
- TeacherI t1=new TeacherI();
- t1.id=7001;
- t1.name="Alamgir Hossain";
- t1.dept="CSE";
- t1.age=30;
- t1.disp();
- System.out.println();
- TeacherI t2=new TeacherI();
- t2.id=7003;
- t2.name="Shamol Pal";
- t2.dept="CSE";
- t2.age=37;
- t2.disp();
- System.out.println();
- }
- }
0 Comments:
Post a Comment