Inheritance In Java Language
Inheritance :
Creating new class from existing class such that the features of existing class are available to the new class is called inheritance Already existing class is called super class & produced class is called sub class Using inheritance while creating sub classes a programmer can reuse the super class code without rewriting it
Syntax
//Syntax:
class subclass_name extends superclass_name
eg: class Child extends Parent
Program 1 : Write a program to create a Person class which contains general details of a person and create a sub class Employ which contains company details of a person Reuse the general details of the person in its sub class Inheritance Example
CODE/PROGRAM/EXAMPLE
class Person
{
String name;
String permanentAddress;
int age;
void set_PermanentDetails (String name, String permanentAddress, int age) {
thisname = name;
thispermanentAddress = permanentAddress;
thisage = age;
}
void get_PermanentDetails () {
System.out.println ("Name : " + name);
System.out.println ("Permanent Address : " + permanentAddress);
System.out.println ("Age :" + age);
}
}
class Employ extends Person {
int id;
String companyName;
String companyAddress;
Employ (int id, String name, String permanentAddress, int age, String companyName, String companyAddress) {
thisid = id;
set_PermanentDetails (name, permanentAddress, age);
thiscompanyName = companyName; thiscompanyAddress = companyAddress;
}
void get_EmployDetails () {
System.out.println ("Employ Id : " + id);
get_PermanentDetails ();
System.out.println ("Company Name : "+ companyName);
System.out.println ("Company Address : "+companyAddress);
}
}
class InherDemo {
public static void main (String args []) {
Employ e1 = new Employ (101, "alex", "US", 21, "Software- CI", "20-CSI");
e1get_EmployDetails ();
}
}
Program 2 : Write a program to illustrate the order of calling of default constructor in super and sub class Default constructors in super and sub class
CODE/PROGRAM/EXAMPLE
class One
{
One ( ) //super class default constructor {
System.out.println ("Super class default constructor called");
}
}
class Two extends One {
Two ( ) //sub class default constructor
{
System.out.println ("Sub class default constructor called");
}
}
class Const {
public static void main (String args[]) {
Two t=new Two ( ); //create sub class object
}
}
Super class default constructor is available to sub class by default
- First super class default constructor is executed then sub class default constructor is executed
- Super class parameterized constructor is not automatically available to subclass super is the key word that refers to super class
The keyword ‘super’ :
- super can be used to refer super class variables as : supervariable
- super can be used to refer super class methods as : supermethod ()
- super can be used to refer super class constructor as : super (values)
Program 3 : Write a program to access the super class method, super class parameterized constructor and super class instance variable by using super keyword from sub class super refers to super class- constructors, instance variables and methods
CODE/PROGRAM/EXAMPLE
class A
{
int x;
A (int x) {
thisx = x;
}
void show( ) {
System.out.println("super class method: x = "+x);
}
}
class B extends A {
int y;
B (int a,int b)
{
super(a); // (or) x=a;
y=b;
}
void show( ) {
supershow ();
System.out.println ("y = "+y);
System.out.println (“ super x = “ + superx);
}
}
class SuperUse {
public static void main(String args[]) {
B ob = new B (10, 24);
obshow ( );
}
}
NOTE : Super key word is used in sub class only The statement calling super class constructor should be the first one in sub class constructor