Introduction to Programming with Java 1 Chapter 4
























- Slides: 24
Introduction to Programming with Java 1
Chapter 4 Inheritance & Abstraction in Java 2
Contents Introduction of Inheritance Classes classification Types of Inheritance Ø Super class Ø Intermediate class Ø Child class Ø Single Inheritance Ø Multilevel Inheritance Ø Hierarchical Inheritance Single Inheritance Multilevel Inheritance Hierarchical Inheritance Abstract Classes and Methods Limitations 3
Inheritance allows a class to use the properties and methods of another class. The derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as superclass. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance. Establish a link/connectivity between 2 or more classes. To establish this relation Java uses ‘extends’ keyword. Permits sharing and accessing properties from one to another class. 4
Category of classes on the basis of Inheritance Super class (base/parent/driver/inheritance/ancestor class). Intermediate class (mediating/dual class). Child class (sub/associate/derived/inherited class). 5
Conti…. . Ø Super Class § § Ø Top located class Service provider (its properties accessed by all its lower level class). Intermediate Class § Middle located class § Having Dual policy (obtain properties of upper level class and transmit properties to lower level class). Ø Child § § Class Bottom located class Much benefitted class Much loaded class Properties of child class as well as class and parent class can be accessed by only the object of child class. 6
Types of Inheritance Ø Single Inheritance ØMultilevel Inheritance ØHierarchical Inheritance 7
Single Inheritance A structure having one and only one parent as well as child class. Child class is authorized to access the property of Parent class. Syntax : 8
Multilevel Inheritance Syntax : Standard structure of Single Inheritance having one Parent, one or more intermediate and one child classes. Child class as well as intermediate class may access the properties of upper level classes. 9
Hierarchical Inheritance Syntax : Ø A structure having one parent and more child class. Ø Child classes must be connected with only Parent class. 10
The “super” keyword It is used for three purposes: 1. Invoking superclass constructor – super(arguments) 2. Accessing superclass members – super. member 3. Invoking superclass methods – super. metehod(arguments) 11
The “super” keyword class A { protected int num; A(int num) { this. num=num; } } class B extends A { int num; B(int a, int b) { super(a); //shoulb be the first line in the subclass constructor this. num=b; } void display() { System. out. println("In A, num="+super. num); System. out. println("In B, num="+num); } } 12
Overriding methods Redefining superclass methods in a subclass is called overriding. The signature of the subclass method should be the same as the superclass method. class A { void method 1(int num) { //code } } class B extends A { void method 1(int x) { //code } } 13
Dynamic binding When over-riding is used, the method call is resolved during runtime i. e. depending on the object type, the corresponding method will be invoked. A ref; ref=new A(); ref. method 1(11); //calls method of class A ref=new B(); ref. method 1(29); // calss method of class B 14
Abstract class A class which contains the abstract keyword in its declaration is known as abstract class. Ø Abstract classes may or may not contain abstract methods i. e. , methods with out body ( public void get(); ) Ø But, if a class have at least one abstract method, then the class must be declared abstract. Ø If a class is declared abstract it cannot be instantiated. Ø To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it. Ø If you inherit an abstract class you have to provide implementations to all the abstract methods in it. 15
Abstract class public abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System. out. println("Constructing an Employee"); this. name = name; this. address = address; this. number = number; } public double compute. Pay() { System. out. println("Inside Employee compute. Pay"); return 0. 0; } public void mail. Check() { System. out. println("Mailing a check to " + this. name + " " + this. address); } } 16
Abstract class Now you can try to instantiate the Employee class as shown below: public class Abstract. Demo { public static void main(String [] args) { /* Following is not allowed and would raise error */ Employee e = new Employee("George W. ", "Houston, TX", 43); System. out. println("n Call mail. Check using Employee reference--"); e. mail. Check(); } } When you compile the above class, it gives you the following error: Employee. java: 46: Employee is abstract; cannot be instantiated Employee e = new Employee("George W. ", "Houston, TX", 43); 1 error 17
Abstract class package bcsthird; abstract class student { int sid; String sname; float marks; void accept() { System. out. println("This is accept method"); } abstract void display(); abstract void delete(); } class teacher extends student { void display() { System. out. println("This is display abstract method"); } void delete() { } } public class my. Demo { public static void main(String args[]) { teacher t=new teacher(); t. accept(); t. display(); } } 18
Inheriting the abstract class We can inherit the properties of Employee class just like concrete class as shown below: public class Salary extends Employee { private double salary; //Annual salary public Salary(String name, String address, int number, double salary) { super(name, address, number); set. Salary(salary); } public void mail. Check() { System. out. println("Within mail. Check of Salary class "); System. out. println("Mailing check to " + get. Name() + " with salary " + salary); } public void set. Salary(double new. Salary) { if(new. Salary >= 0. 0) { salary = new. Salary; } } public double compute. Pay() { System. out. println("Computing salary pay for " + get. Name()); return salary/52; } } 19
Inheriting the abstract class Here, you cannot instantiate the Employee class, but you can instantiate the Salary Class, and using this instance you can access the all the three fields and all methods of Employee class as shown below. public class Abstract. Demo { public static void main(String [] args) { Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600. 00); Employee e = new Salary("John Adams", "Boston, MA", 2, 2400. 00); System. out. println("Call mail. Check using Salary reference --"); s. mail. Check(); System. out. println("n Call mail. Check using Employee reference--"); e. mail. Check(); } } Constructing an Employee Call mail. Check using Salary reference -Within mail. Check of Salary class ailing check to Mohd Mohtashim with salary 3600. 0 Call mail. Check using Employee reference-Within mail. Check of Salary class ailing check to John Adams with salary 2400. 20
Abstract methods If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract. Ø abstract keyword is used to declare the method as abstract. Ø You have to place the abstract keyword before the method name in the method declaration. Ø An abstract method contains a method signature, but no method body. Ø Instead of curly braces an abstract method will have a semi colon ( ; ) at the end. 21
Abstract methods public abstract class Employee { private String name; private String address; private int number; public abstract double compute. Pay(); } //Remainder of class definition public class Salary extends Employee { private double salary; // Annual salary public double compute. Pay() { System. out. println("Computing salary pay for " + get. Name()); return salary/52; } } //Remainder of class definition 22
Limitations of Inheritance Link is established into single direction(Fig) Java does not support Multiple inheritance as well as Hybrid inheritance. The extends keyword permits to connect a class with only one class. 23
Example Create an abstract class person. Derive two classes Employee and Worker from it. Use proper method to accept and display the details for the same. The fields of Employee are Emp_no, Emp_name, and address. Similar for worker are name and working hours 24