UNIT V Inheritance Packages and Interfaces Inheritance basics
UNIT V Inheritance, Packages and Interfaces • Inheritance basics • Using Super • Creating Multilevel hierarchy • Constructors in derived class • Method overriding • Dynamic method dispatch • Using Abstract classes • Using final with inheritance • Object class PVG's College of Engineering, Nashik
Terminology Inheritance is a fundamental Object Oriented concept The derivation of one class from another class is called Inheritance A class that is inherited is called a super class. The class that does the inheriting is called as subclass. A class can be defined as a "subclass" of another class. The subclass inherits all data attributes , methods, associations of its superclass: The subclass can: Add new functionality Use inherited functionality Override inherited functionality subclass: PVG's College of Engineering, Nashik Person - name: String - dob: Date Employee - employee. ID: int - salary: int - start. Date: Date
What really happens? When an object is created using new, the system must allocate enough memory to hold all its instance variables. This includes any inherited instance variables In this example, we can say that an Employee "is a kind of" Person. An Employee object inherits all of the attributes, methods and associations of Person - name: String - dob: Date Person name = "John Smith" dob = Jan 13, 1954 Employee name = "Sally Halls" dob = Mar 15, 1968 employee. ID = 37518 salary = 65000 start. Date = Dec 15, 2000 is a kind of Employee - employee. ID: int - salary: int - start. Date: Date PVG's College of Engineering, Nashik
• A subclass inherits all instance variables and methods from its super class and also has its own variables and methods. • One can inherit the class using keyword extends. Class subclass-name extends super class-name { // body of class. } PVG's College of Engineering, Nashik
Inheritance in Java Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object public class Person { private String name; private Date dob; [. . . ] Person - name: String - dob: Date public class Employee extends Person { private int employe. ID; private int salary; private Date start. Date; [. . . ] Employee an. Employee = new Employee(); PVG's College of Engineering, Nashik Employee - employee. ID: int - salary: int - start. Date: Date
Inheritance Example class Bank. Account { private double my. Bal; public Bank. Account() { my. Bal = 0; } public double get. Balance() { return my. Bal; } } class Checking. Account extends Bank. Account { private double my. Interest; public Checking. Account(double interest) { } public double get. Interest() { return my. Interest; } public void apply. Interest() { } } Checking. Account objects have my. Bal and my. Interest fields, and get. Balance(), get. Interest(), and apply. Interest() methods PVG's College of Engineering, Nashik
Remember • In java, a class has only one super class. • Java does not support Multiple Inheritance. • One can create a hierarchy of inheritance in which a subclass becomes a super class of another subclass. • However, no class can be a super class of itself. PVG's College of Engineering, Nashik
class A //superclass { int num 1; //member of superclass int num 2; //member of superclass void set. Val(int no 1, int no 2) //method of superclass { num 1 = no 1; num 2 = no 2; } } class B extends A //subclass B { int multi; //member of subclass void mul() //method of subclass { multi = num 1*num 2; //accessing member of superclass from subclass } } class inhe 2 { public static void main(String args[]) { B subob = new B(); subob. set. Val(5, 6); //calling superclass method throgh subclass object subob. mul(); System. out. println("Multiplication is " + subob. multi); } PVG's College of Engineering, Nashik }
Using Super Keyword • super keyword is used to call a superclass constructor and to call or access super class members(instance variables or methods). syntax of super : super(args); // call parent’s constructor super. field. Name // access parent’s field super. method. Name(args); // or method • When a subclass calls super() it is calling the constructor of its immediate superclass. • super() must always be the first statement executed inside a subclass constructor. Þsuper. member • Here member can be either method or an instance variables. • This second form of super is most applicable to situation in which member names of a subclass hide member of superclass due to same name. PVG's College of Engineering, Nashik
class A 1 { public int i; A 1() { i=5; } } class B 1 extends A 1 { int i; B 1(int a, int b) { super(); //calling super class constructor //now we will change value of superclass variable i super. i=a; //accessing superclass member from subclass i=b; } void show() { System. out. println("i in superclass = " + super. i ); System. out. println("i in subclass = " + i ); } } public class Usesuper { public static void main(String[] args) { B 1 b = new B 1(10, 12); b. show(); } } PVG's College of Engineering, Nashik
Output : i in superclass = 10 i in subclass = 12 The instance variable i in B 1 hides the i in A, super allows access to the i defined in the superclass. super can also be used to call methods that are hidden by a subclass. PVG's College of Engineering, Nashik
Final keyword final keyword can be use with variables, methods and class. Þ final variables. When we want to declare constant variable in java we use final keyword. Syntax : final variable name = value; => final method Syntax final methodname(arg) When we put final keyword before method than it becomes final method. To prevent overriding of method final keyword is used, means final method cant be override. => final class A class that can not be sub classed is called a final class. This is archived in java using the keyword final as follow. Syntax : final class_name {. . . } Any attempt to inherit this class will cause an error and compiler will not allow it. PVG's College of Engineering, Nashik
Inheritance Hierarchy Each Java class has one (and only one) superclass. C++ allows for multiple inheritance Inheritance creates a class hierarchy Classes higher in the hierarchy are more general and more abstract Classes lower in the hierarchy are more specific and concrete Class There is no limit to the number of subclasses a class can have There is no limit to the depth of the class tree. Class PVG's College of Engineering, Nashik Class
class student { int rollno; String name; student(int r, String n) { rollno = r; name = n; } void dispdatas() { System. out. println("Rollno = " + rollno); System. out. println("Name = " + name); } } void dispdatam() { dispdatas(); // call dispdatap of student class System. out. println("Total = " + total); } } class percentage extends marks { int per; percentage(int r, String n, int t, int p) { super(r, n, t); //call super class(marks) constructor per = p; } void dispdatap() { dispdatam(); // call dispdatap of marks class System. out. println("Percentage = " + per); } } class marks extends student class Multi_Inhe { { public static void main(String args[]) int total; { marks(int r, String n, int t) percentage stu = new percentage(1912, "SAM", 350, { 50); //call constructor percentage stu. dispdatap(); // call dispdatap of percentage class super(r, n); //call super class (student) } constructor } of Engineering, Nashik PVG's College total = t;
Output : Rollno = 1912 Name = SAM Total = 350 Percentage = 50 • It is common that a class is derived from another derived class. • The class student serves as a base class for the derived class marks, which in turn serves as a base class for the derived class percentage. • The class marks is known as intermediated base class since it provides a link for the inheritance between student and percentage. • The chain is known as inheritance path. • When this type of situation occurs, each subclass inherits all of the features found in all of its super classes. In this case, percentage inherits all aspects of marks and student. • To understand the flow of program read all comments of program. PVG's College of Engineering, Nashik
Constructors and Initialization Classes use constructors to initialize instance variables When a subclass object is created, its constructor is called. It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors so that the instance variables defined in the superclass are properly initialized Superclass constructors can be called using the "super" keyword in a manner similar to "this" It must be the first line of code in the constructor If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass. PVG's College of Engineering, Nashik
Constructors - Example public class Bank. Account { private String owners. Name; private int account. Number; private float balance; public Bank. Account(int an. Account. Number, String a. Name) { account. Number = an. Account. Number; owners. Name = a. Name; } [. . . ] } public class Overdraft. Account extends Bank. Account { private float overdraft. Limit; public Overdraft. Account(int an. Account. Number, String a. Name, float a. Limit) { super(an. Account. Number, a. Name); overdraft. Limit = a. Limit; } } PVG's College of Engineering, Nashik
- Slides: 17