CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
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 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance-Basics.
Inheritance and Polymorphism
UNIT-2.
Inheritance in Java.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Interface.
Extending Classes.
INHERITANCE.
METHOD OVERRIDING in JAVA
Java – Inheritance.
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Inheritance.
Index Understanding static Final Inheritance Super
Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

CSI 3125, Preliminaries, page 1 Inheritance

CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. A class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

CSI 3125, Preliminaries, page 3 Inheritance To inherit a class, by using the extends keyword class inh { public static void main(String args[]) { B obj =new B(); obj.show_a(10); obj.show_b(20); } class A { int a; void show_a(int x) { a=x;} } class B extends A { int b; void show_b(int x) { b=x; int c=a+b; System.out.println("sum = "+c);} }

CSI 3125, Preliminaries, page 4 Member Access and Inheritance Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.

CSI 3125, Preliminaries, page 5 Super Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. super has two general forms. The first calls the superclass’ constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass.

CSI 3125, Preliminaries, page 6 Using super to Call Superclass Constructors A subclass can call a constructor method defined by its superclass by use of the following form of super: super(parameter-list);

CSI 3125, Preliminaries, page 7 Using super to Call Superclass Constructors class inh { public static void main(String args[]) { B obj =new B(1,2); obj.show_b(); } class A { int a; A(int x){a=x;} } class B extends A { int b; B(int x,int y) { super(x);// invoke supper class constructor method b=y; } void show_b() { int c=a+b; System.out.println("sum = "+c);} }

CSI 3125, Preliminaries, page 8 A Second Use for super The second form of super acts somewhat like this, This usage has the following form: super.member Here, member can be either a method or an instance variable. This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.

CSI 3125, Preliminaries, page 9 A Second Use for super class inh { public static void main(String args[]) { B obj =new B(1,2); obj.show_b(); } class A{ int a; } class B extends A{ int a; B(int x,int y) { super.a=x; a=y; } void show_b(){ int c=super.a+a; System.out.println("sum = "+c);} }

CSI 3125, Preliminaries, page 10 Multilevel Hierarchy Given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A. When this type of situation occurs, each subclass inherits all of the traits found in all of its super classes

CSI 3125, Preliminaries, page 11 Multilevel Hierarchy class multi { public static void main(String[] args) { C obj=new C(); } class A { A(){System.out.println("Constructor of Class A");} } class B extends A {B(){System.out.println("Constructor of Class B");} } class C extends B {C(){System.out.println("Constructor of Class C");} }

CSI 3125, Preliminaries, page 12 Constructors in Multilevel Hierarchy class multi { public static void main(String[] args) { C obj=new C(1,2,3); obj.add(); } class A { int a; A(int x){a=x;} } class B extends A {int b; B(int x,int y){super(x); b=y;} } class C extends B {int c; C(int x,int y,int z){super(x,y);c=z;} void add() { System.out.println("sum= "+(a+b+c)); }

CSI 3125, Preliminaries, page 13 Method Overriding In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the that method defined by the subclass. The version of the method defined by the superclass will be hidden.

CSI 3125, Preliminaries, page 14 Method Overriding class overrid { public static void main(String[] args) { B obj=new B(); obj.show(); } class A { void show() {System.out.println("Method in Super class");} } class B extends A {void show() {System.out.println("Method in Sub class");} } Out put will be Method in sub class That is, the version of show( ) inside B overrides the version declared in A.

CSI 3125, Preliminaries, page 15 Method Overriding To access the superclass version of an overridden function, by using super. The superclass version of show( ) is invoked within the subclass’ version.

CSI 3125, Preliminaries, page 16 Method Overriding class overrid { public static void main(String[] args) { B obj=new B(); obj.show(); } class A { void show() {System.out.println("Method in Super class");} } class B extends A {void show() { super.show(); System.out.println("Method in Sub class");} } Out put will be Method in super class Method in sub class

CSI 3125, Preliminaries, page 17 Dynamic method dispatch Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time.

CSI 3125, Preliminaries, page 18 Dynamic method dispatch class overrid { public static void main(String[] args) { A obj1=new A(); B obj2=new B(); A a1; //reference of type A a1=obj1; a1.show();//calls A's version of show() a1=obj2;// a1 refers to B's Object a1.show();//calls B's version of show() } class A { void show() {System.out.println("Method in Super class");} } class B extends A { void show() { System.out.println("Method in Sub class");} }

CSI 3125, Preliminaries, page 19 Abstract Classes To define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes want to create a superclass that only defines a generalized form that will be shared by all of its subclasses One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method Abstract class can de declared using the abstract

CSI 3125, Preliminaries, page 20 Abstract Classes class abs { public static void main(String[] args) { B obj=new B(); obj.show(); } abstract class A { abstract void show(); } class B extends A { void show() { System.out.println("Method in Sub class");} }

CSI 3125, Preliminaries, page 21 Abstract Classes Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java’s approach to run-time polymorphism is implemented through the use of superclass references Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object

CSI 3125, Preliminaries, page 22 Using final with Inheritance The keyword final has three uses. First, it can be used to create the equivalent of a named constant. The other two uses of final apply to inheritance.

CSI 3125, Preliminaries, page 23 Using final to Prevent Overriding To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden. class A { final void show() { System.out.println("This is a final method."); }} class B extends A { void show() { // ERROR! Can't override. System.out.println("Illegal!"); }

CSI 3125, Preliminaries, page 24 Using final to Prevent Inheritance To prevent a class from being inherited. To do this, precede the class declaration with final. It is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations. final class A { //... } // The following class is illegal. class B extends A { // ERROR! Can't subclass A //... }

CSI 3125, Preliminaries, page 25 The Object Class Special class, Object, defined by Java. All other classes are subclasses of Object. That is, Object is a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class. Object defines the following methods

CSI 3125, Preliminaries, page 26 The Object Class Method Purpose Object clone( ) Creates a new object that is the same as the object being cloned. boolean equals(Object object) Determines whether one object is equal to another. void finalize( ) Called before an unused object is recycled. Class getClass( ) Obtains the class of an object at run time. int hashCode( ) Returns the hash code associated with the invoking object. void notify( ) Resumes execution of a thread waiting on the invoking object. void notifyAll( ) Resumes execution of all threads waiting on the invoking object. String toString( ) Returns a string that describes the object. void wait( ) void wait(long milliseconds) void wait(long milliseconds, int nanoseconds) Waits on another thread of execution.