Inheritance
The process by which one class acquires the properties and functionalities of another class. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it.
Inheritance is a mechanism of defining a new class based on an existing class.
Inheritance enables reuse of code. Inheritance also provides scope for refinement of the existing class. Inheritance helps in specialization
The existing (or original) class is called the base class or super class or parent class. The new class which inherits from the base class is called the derived class or sub class or child class.
Inheritance implements the “Is-A” or “Kind Of/ Has-A” relationship.
Note : The biggest advantage of Inheritance is that, code in base class need not be rewritten in the derived class.
The member variables and methods of the base class can be used in the derived class as well.
Inheritance Example
Consider below two classes –
Class Teacher:
class Teacher {
private String name;
private double salary;
private String subject;
public Teacher (String tname) {
name = tname;
}
public String getName() {
return name;
}
private double getSalary() {
return salary;
}
private String getSubject() {
return subject;
}
}
Class: OfficeStaff
class OfficeStaff{
private String name;
private double salary;
private String dept;
public OfficeStaff (String sname) {
name = sname;
}
public String getName() {
return name;
}
private double getSalary() {
return salary;
}
private String getDept () {
return dept;
}
}
Points:
1) Both the classes share few common properties and methods. Thus repetition of code.
2) Creating a class which contains the common methods and properties.
3) The classes Teacher and OfficeStaff can inherit the all the common properties and methods from below Employee class
class Employee{
private String name;
private double salary;
public Employee(String ename){
name=ename;
}
public String getName(){
return name;
}
private double getSalary(){
return salary;
}
}
4) Add individual methods and properties to it Once we have created a super class that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses
5) Any similar classes like Engineer, Principal can be generated as subclasses from the Employee class.
6) The parent class is termed super class and the inherited class is the sub class
7) A sub class is the specialized version of a super class – It inherits all of the instance variables and methods defined by the super class and adds its own, unique elements.
8) Although a sub class includes all of the members of its super class it can not access those members of the super class that have been declared as private.
9) A reference variable of a super class can be assigned to a reference to any sub class derived from that super class
i.e. Employee emp = new Teacher();
Note: Multi-level inheritance is allowed in Java but not multiple inheritance
Types of Inheritance
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class inheriting from more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
Note 1: Multiple Inheritance is very rarely used in software projects. Using Multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple in
The process by which one class acquires the properties and functionalities of another class. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it.
Inheritance is a mechanism of defining a new class based on an existing class.
Inheritance enables reuse of code. Inheritance also provides scope for refinement of the existing class. Inheritance helps in specialization
The existing (or original) class is called the base class or super class or parent class. The new class which inherits from the base class is called the derived class or sub class or child class.
Inheritance implements the “Is-A” or “Kind Of/ Has-A” relationship.
Note : The biggest advantage of Inheritance is that, code in base class need not be rewritten in the derived class.
The member variables and methods of the base class can be used in the derived class as well.
Inheritance Example
Consider below two classes –
Class Teacher:
class Teacher {
private String name;
private double salary;
private String subject;
public Teacher (String tname) {
name = tname;
}
public String getName() {
return name;
}
private double getSalary() {
return salary;
}
private String getSubject() {
return subject;
}
}
Class: OfficeStaff
class OfficeStaff{
private String name;
private double salary;
private String dept;
public OfficeStaff (String sname) {
name = sname;
}
public String getName() {
return name;
}
private double getSalary() {
return salary;
}
private String getDept () {
return dept;
}
}
Points:
1) Both the classes share few common properties and methods. Thus repetition of code.
2) Creating a class which contains the common methods and properties.
3) The classes Teacher and OfficeStaff can inherit the all the common properties and methods from below Employee class
class Employee{
private String name;
private double salary;
public Employee(String ename){
name=ename;
}
public String getName(){
return name;
}
private double getSalary(){
return salary;
}
}
4) Add individual methods and properties to it Once we have created a super class that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses
5) Any similar classes like Engineer, Principal can be generated as subclasses from the Employee class.
6) The parent class is termed super class and the inherited class is the sub class
7) A sub class is the specialized version of a super class – It inherits all of the instance variables and methods defined by the super class and adds its own, unique elements.
8) Although a sub class includes all of the members of its super class it can not access those members of the super class that have been declared as private.
9) A reference variable of a super class can be assigned to a reference to any sub class derived from that super class
i.e. Employee emp = new Teacher();
Note: Multi-level inheritance is allowed in Java but not multiple inheritance
Types of Inheritance
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class inheriting from more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
Note 1: Multiple Inheritance is very rarely used in software projects. Using Multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple in
No comments:
Post a Comment