240 likes | 265 Views
Introduction to Programming with Java. Prepared by Allaudin “Hemat”. Inheritance & Abstraction in Java. Chapter 4. Prepared by Allaudin “Hemat”. Contents. Introduction of Inheritance Classes classification Super class
E N D
Introduction to Programmingwith Java Prepared by Allaudin “Hemat”
Inheritance & Abstraction in Java Chapter 4 Prepared by Allaudin “Hemat”
Contents • Introduction of Inheritance • Classes classification • Super class • Intermediate class • Child class • Types of Inheritance • Single Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Single Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Abstract Classes and Methods • Limitations
Inheritance • 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 super-class. • 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. Prepared by Allaudin “Hemat”
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). Prepared by Allaudin “Hemat”
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. Prepared by Allaudin “Hemat”
Types of Inheritance • Single Inheritance • Multilevel Inheritance • Hierarchical Inheritance Prepared by Allaudin “Hemat”
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 : Prepared by Allaudin “Hemat”
Syntax : Multilevel Inheritance • 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. Prepared by Allaudin “Hemat”
Hierarchical Inheritance Syntax : • A structure having one parent and more child class. • Child classes must be connected with only Parent class. Prepared by Allaudin “Hemat”
The “super” keyword • It is used for three purposes: • Invoking superclass constructor – super(arguments) • Accessing superclass members – super.member • Invoking superclass methods – super.metehod(arguments) Prepared by Allaudin “Hemat”
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); } } Prepared by Allaudin “Hemat”
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 method1(int num) { //code } } class B extends A { void method1(int x) { //code } } Prepared by Allaudin “Hemat”
Dynamic binding • When over-riding is used, the method call is resolved during run-time i.e. depending on the object type, the corresponding method will be invoked. A ref; ref=new A(); ref.method1(11); //calls method of class A ref=new B(); ref.method1(29); // calss method of class B Prepared by Allaudin “Hemat”
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. Prepared by Allaudin “Hemat”
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 computePay() { System.out.println("Inside Employee computePay"); return 0.0; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } } Prepared by Allaudin “Hemat”
Abstract class Now you can try to instantiate the Employee class as shown below: public class AbstractDemo { 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 mailCheck using Employee reference--"); e.mailCheck(); } } 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 Prepared by Allaudin “Hemat”
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 myDemo { public static void main(String args[]) { teacher t=new teacher(); t.accept(); t.display(); } } Prepared by Allaudin “Hemat”
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); setSalary(salary); } public void mailCheck() { System.out.println("Within mailCheck of Salary class "); System.out.println("Mailing check to " + getName() + " with salary " + salary); } public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; } } public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } } Prepared by Allaudin “Hemat”
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 AbstractDemo { 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 mailCheck using Salary reference --"); s.mailCheck(); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } } Constructing an Employee Constructing an Employee Call mailCheck using Salary reference -- Within mailCheck of Salary class ailing check to Mohd Mohtashim with salary 3600.0 Call mailCheck using Employee reference-- Within mailCheck of Salary class ailing check to John Adams with salary 2400. Prepared by Allaudin “Hemat”
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. Prepared by Allaudin “Hemat”
Abstract methods public abstract class Employee { private String name; private String address; private int number; public abstract double computePay(); //Remainder of class definition } public class Salary extends Employee { private double salary; // Annual salary public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } //Remainder of class definition } Prepared by Allaudin “Hemat”
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. Prepared by Allaudin “Hemat”
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 Prepared by Allaudin “Hemat”