Introduction to Programming with Java

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
PolymorphismtMyn1 Polymorphism Polymorphism enables you to write programs that process objects that share the same base class (either directly or indirectly)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Inheritance-Basics.
Inheritance and Polymorphism
Inheritance in Java.
Advanced Programming in Java
Week 8 Lecture -3 Inheritance and Polymorphism
Lecture 14 - Abstract Classes
Modern Programming Tools And Techniques-I Inheritance
Abstract Classes.
Inheritance Chapter 5.
Inheritance Basics Programming with Inheritance
More inheritance, Abstract Classes and Interfaces
Review of Object-Oriented Concepts in JAVA
Java Programming Language
Chapter 9: Polymorphism and Inheritance
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Programming, Second Edition
Java Inheritance.
Review of Object-Oriented Concepts in JAVA
Chapter 10: Method Overriding and method Overloading
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Inheritance and Polymorphism
Presentation transcript:

Introduction to Programming with Java Prepared by Allaudin “Hemat” 1

Inheritance & Abstraction in Java Chapter 4 Inheritance & Abstraction in Java Prepared by Allaudin “Hemat” 2

Contents Introduction of Inheritance Classes classification Super class Intermediate class Child class Types of Inheritance Single Inheritance Multilevel Inheritance Hierarchical Inheritance Abstract Classes and Methods Limitations 3

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 Intermediate Class Child 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”

Multilevel Inheritance Hierarchical Inheritance 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”

Multilevel Inheritance 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) 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 Prepared by Allaudin “Hemat” 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 Prepared by Allaudin “Hemat” 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 Call mailCheck using Salary reference -- Within mailCheck of Salary class ailing check to Mohd Mohtashim with salary 3600.0 Call mailCheck using Employee reference-- 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 Prepared by Allaudin “Hemat” 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”