1 / 53

Introduction to Inheritance in Java Programming

Learn about the concept of inheritance in object-oriented programming, its importance in creating reusable classes, and the different types of inheritance in Java.

maximina
Download Presentation

Introduction to Inheritance in Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modern Programming Tools And Techniques-I Lecture 8: Inheritance

  2. Introduction • Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. • In the Java programming language, each class is allowed to have one direct super-class, and each super-class has the potential for an unlimited number of subclasses. • Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes.

  3. Inheritance • Inheritance defines an is-a relationship between a super-class and its subclasses. It means that the subclass (child) is a more specific version of the super-class (parent). • An object of a subclass can be used wherever an object of the super-class can be used. • Inheritance is used to build new classes from existing classes. • The inheritance relationship is transitive: if class y extends class x, and a class z extends class y, then z will also inherit from class x.

  4. Inheritance • Using inheritance, we can create a general class that defines traits(state and behaviors) 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. • In Java, a class that is inherited is called a super-class. • The class that does the inheriting is called a subclass. • Therefore, a subclass is a specialized version of a super-class. It inherits all of the instance variables and methods defined by the super-class and adds its own, unique elements.

  5. Types of Inheritance The following kinds of inheritance are there in java. • Simple Inheritance • Multilevel Inheritance • Simple Inheritance: A  subclass is derived simply from it's parent class. • There is only a sub class and it's parent class. It is also called single inheritance or one level inheritance. • Multi-level Inheritance: A subclass is derived from a derived class.

  6. Example (Simple inheritance)

  7. Multiple Inheritance • The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. • Java does not support multiple inheritance using classes but the multiple inheritance can be achieved by using the interface. • In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

  8. Inheritance Example class Shape { int area(){…} } class Rectangle extends Shape{ int area() { area = length * width;} int length; int width; } class Square extends Rectangle { int area() {…} int length; int width = length; }

  9. Accessing parent class members

  10. Example (access parent member)

  11. Example (access parent member)

  12. Abstract Class and Abstract method

  13. Abstract Class and method • An abstract classis a class that is declared abstract. • An abstract methodis a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY); • Abstract class may or may not include abstract methods. • Abstract classes cannot be instantiated, but they can be sub-classed.

  14. If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); } • When an abstract class is sub-classed, the sub-class usually provides implementations for all of the abstract methods in its parent class. • However, if it does not, the sub-class must also be declared abstract.

  15. Example(abstract class)

  16. Example(abstract class)

  17. Example(abstract method)

  18. Functionality of constructors in Inheritance

  19. Example (constructor calling in inheritance)

  20. Example (constructor calling in inheritance)

  21. Role of access specifiers in Inheritance

  22. Key Points • Private members of the super-class are not inherited by the subclass. • Members that have default accessibility in the super-class are also not inherited by subclasses in other packages. • Constructors and initializer blocks are not inherited by a subclass. • A subclass can extend only one super-class.

  23. Example (access specifiers in same package)

  24. Example (access specifiers in same package)

  25. Method overloading in Inheritance

  26. Example (method overloading)

  27. Method overriding

  28. Method Overriding • Method overriding means having a different implementation of the same method in the inherited class. • These two methods would have the same signature, but different implementation. • One of these would exist in the base class and another in the derived class. These cannot exist in the same class. Note:- A class declared as final cannot be inherited and a method declared final cannot be overridden in sub-class.

  29. Key Points • The version of a method that is executed will be determined by the object that is used to invoke it. • If an object of a parent class is used to invoke the method, then the version in the parent class will be executed. • If an object of the subclass is used to invoke the method, then the version in the child class will be executed.

  30. Example (method overriding)

  31. Example (accessing parent method)

  32. Difference b/w Overloading & Overriding

  33. 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.

  34. Example class A {void callme() {System.out.println("Inside A's callme method");   } } class B extends A  {void callme() {System.out.println("Inside B's callme method"); }}class C extends A  {void callme() {System.out.println("Inside C's callme method"); }}

  35. class Dispatch  {   public static void main(String args[])  {    A a = new A();  // object of type A     B b = new B();  // object of type B    C c = new C();  // object of type C      A r;  // obtain a reference of type A     r = a;  // r refers to an A objectr.callme();  // calls A's version of callme()     r = b;  // r refers to a B objectr.callme();  // calls B's version of callme()     r = c;  // r refers to a C objectr.callme();  // calls C's version of callme()   }}

  36. Example

  37. Role of instanceof operator

  38. instanceof • The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface). • The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

  39. An object of subclass type is also a type of parent class. For example, if Dog extends Animal then object of Dog can be referred by either Dog or Animal class.

  40. Downcasting with java instanceof operator • When subclass type refers to the object of Parent class, it is known as downcasting. • If we perform it directly, compiler gives Compilation error. If you perform it by typecasting, ClassCastException is thrown at runtime.

  41. Possibility of downcasting with instanceof

  42. Downcasting to different classes with instanceof

  43. Role of static methods in Inheritance

  44. Static methods in inheritance

  45. Example (static method in inheritance)

  46. Key points

More Related