Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.

Similar presentations


Presentation on theme: "JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1."— Presentation transcript:

1 JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1

2 2 Session-III You learn  Inheritance  Constructors  Method overloading  Method overrding  Keywords extends, super,final  Abstract classes  Dynamic method despatch  Polymorphism

3 OOPs revolve around the three concepts: 1. Encapsulation OOPs revolve around the three concepts: 1. Encapsulation 2. Polymorphism 3. Inheritance 3

4 The three principles of OOP Encapsulation Encapsulation –Objects hide their functions (methods) and data (instance variables) Inheritance Inheritance –Each subclass inherits all variables of its superclass Polymorphism Polymorphism –Interface same despite different data types car auto- matic manual Super class Subclasses draw()

5 5 Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. Inheritance implements the “ is a ” relationship between objects. Inheritance implements the “ is a ” relationship between objects.

6 Using inheritance, you can create a Using inheritance, you can create a general class that defines traits common to a set of related items. 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. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. The class that does the inheriting is called a subclass. 6

7 7

8 8 Inheritance implements the “is a” relationship. Inheritance implements the “is a” relationship. Not to be confused with embedding (an object has another object as a part), which represents the “ has a” relationship: Not to be confused with embedding (an object has another object as a part), which represents the “ has a” relationship: A sailboat is a boat A sailboat has a sail

9 Using inheritance, you can create a Using inheritance, you can create a general class that defines traits common to a set of related items. 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. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. The class that does the inheriting is called a subclass. 9

10 Therefore, a subclass is a specialized version of a superclass. 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. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. 10

11 The new class will be similar to the existing The new class will be similar to the existing class, but will have some new characteristics. class, but will have some new characteristics. 11

12 BENEFITS Inheritance promotes the concept of code reusability. Inheritance promotes the concept of code reusability. Inheritance allows Inheritance allows a better data analysis, a better data analysis, reduction in development time, reduction in development time, less coding and better performance less coding and better performance 12

13 13 subclass or derived class superclass or base class extends Inheritance (cont’d)

14 14 In Java, a subclass can extend only one superclass. In Java, a subclass can extend only one superclass. In Java, a class can implement several interfaces — this is Java’s form of multiple inheritance. In Java, a class can implement several interfaces — this is Java’s form of multiple inheritance.

15 15 Inheritance (cont’d) Game GameFor2 BoardGame Chess Backgammon Solitaire

16 extends keyword 16 The general form The general form class subclass-name extends superclass-name { // body of class // body of class }

17 The following program creates a The following program creates a superclass called A and a superclass called A and a subclass called B. subclass called B. 17

18 class A { ----------; ---------; //super class } Class B extends class A { ---------------; ---------------; //sub class } class mainclass { class mainclass { public static void main(String args[]) { public static void main(String args[]) { } 18

19 // A simple example of inheritance. // A simple example of inheritance. // Create a superclass. // Create a superclass. class A { class A { int i, j; int i, j; void showij() { void showij() { System.out.println("i and j: " + i + " " + j); System.out.println("i and j: " + i + " " + j); } } // Create a subclass by extending class A. // Create a subclass by extending class A. class B extends A { class B extends A { int k; int k; void showk() { void showk() { System.out.println("k: " + k); System.out.println("k: " + k); } } void sum() { void sum() { System.out.println("i+j+k: " + (i+j+k)); System.out.println("i+j+k: " + (i+j+k)); } } } 19

20 class SimpleInheritance { class SimpleInheritance { public static void main(String args[]) { public static void main(String args[]) { A superOb = new A(); A superOb = new A(); B subOb = new B(); B subOb = new B(); subOb.showij(); subOb.showij(); subOb.showk(); subOb.showk(); System.out.println(); System.out.println(); System.out.println("Sum of i, j and k in subOb:"); System.out.println("Sum of i, j and k in subOb:"); subOb.sum(); subOb.sum(); } } } 20

21 21 Inheritance (cont’d) An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above. An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above.

22 Simple Single Inheritance Simple Single Inheritance The class that is used as a basis for defining a new class is called a parent class (or superclass or base class.) The class that is used as a basis for defining a new class is called a parent class (or superclass or base class.) The new class based on the parent class is called a child class (or subclass or derived class.) The new class based on the parent class is called a child class (or subclass or derived class.) In Java, children inherit characteristics from just one parent. In Java, children inherit characteristics from just one parent. This is called single inheritance. This is called single inheritance. 22

23 Single inheritance : A derived class with only one base class is called single inheritance. 23

24 24 Multiple inheritance A derived class with several base classes is called multiple inheritance A derived class with several base classes is called multiple inheritance

25 25

26 Multilevel inheritance The mechanism of deriving a class from another derived class is called multilevel inheritance. The mechanism of deriving a class from another derived class is called multilevel inheritance. 26

27 27

28 Hierarchical inheritance One class may be inherited by more than one classes. One class may be inherited by more than one classes. This process is known as hierarchical inheritance This process is known as hierarchical inheritance 28

29 29

30 Hybrid inheritance It is a combination of hierarchical and multiple inheritance. It is a combination of hierarchical and multiple inheritance. 30

31 31

32 32

33 Using super super has two general forms. super has two general forms. The first calls the superclass’ constructor. 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 The second is used to access a member of the superclass that has been hidden by a member of a subclass. subclass. 33

34 Using super to Call Superclass Constructors A subclass can call a constructor method defined by its superclass by use of the A subclass can call a constructor method defined by its superclass by use of the following form of super: following form of super: super(parameter-list); super(parameter-list); super( ) must always be the first statement executed inside a subclass’ constructor. super( ) must always be the first statement executed inside a subclass’ constructor. 34

35 parameter-list is defined by the constructor in the superclass. parameter-list is defined by the constructor in the superclass. super(parameter-list) must be the first statement executed inside a subclass' constructor. super(parameter-list) must be the first statement executed inside a subclass' constructor. 35

36 Here is a demo for how to use super to call constructor from parent class Here is a demo for how to use super to call constructor from parent class See pgm shapessuper1.java See pgm shapessuper1.java 36

37 Use super to reference members from parent class Its general form is: Its general form is: super.member super.member member can be either a method or an instance variable. member can be either a method or an instance variable. See pgm super2.java See pgm super2.java 37

38 When Constructors Are Called In a class hierarchy, constructors are called in order of derivation, from superclass to subclass. In a class hierarchy, constructors are called in order of derivation, from superclass to subclass. The following program illustrates when constructors are executed: The following program illustrates when constructors are executed: See constructorder.java See constructorder.java 38

39 What is Method Overriding Method Overriding happens when a method in a subclass has the same name and type signature as a method in its superclass. Method Overriding happens when a method in a subclass has the same name and type signature as a method in its superclass. When an overridden method is called within a subclass, it will refer to the method defined in the subclass. When an overridden method is called within a subclass, it will refer to the method defined in the subclass. The method defined by the superclass will be hidden. The method defined by the superclass will be hidden. See pgm override1.java or shaperssuper2.java See pgm override1.java or shaperssuper2.java 39

40 super and overridden method To access the superclass version of an overridden function, you can do so by using super. To access the superclass version of an overridden function, you can do so by using super. See override2.java or shapessuper2.java See override2.java or shapessuper2.java 40

41 Method overriding vs method overload Method overriding occurs when the names and the type signatures of the two methods are identical. Method overriding occurs when the names and the type signatures of the two methods are identical. If not, the two methods are overloaded. If not, the two methods are overloaded. For example, consider this modified version of the preceding example: For example, consider this modified version of the preceding example: See prg override3.java See prg override3.java 41

42 Dynamic Method Dispatch When an overridden method is called through a superclass reference, Java determines which version of that method to execute. When an overridden method is called through a superclass reference, Java determines which version of that method to execute. Here is an example that illustrates dynamic method dispatch: Here is an example that illustrates dynamic method dispatch: see dmd.java see dmd.java 42

43 What are Abstract Classes You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. To declare an abstract method, use this general form: To declare an abstract method, use this general form: abstract type name(parameter-list); abstract type name(parameter-list); 43

44 No method body is present for abstract method. No method body is present for abstract method. Any class that contains one or more abstract methods must also be declared abstract. Any class that contains one or more abstract methods must also be declared abstract. 44

45 abstract class MyAbstractClass{ abstract class MyAbstractClass{ abstract type name(parameter-list); abstract type name(parameter-list); } Here is an abstract class, followed by a class which implements its abstract method. Here is an abstract class, followed by a class which implements its abstract method. See prg abs1.java See prg abs1.java 45

46 Using abstract methods and classes http://www.java-examples.com/java- string-lower-case-example http://www.java-examples.com/java- string-lower-case-example http://www.java-examples.com/java- string-lower-case-example http://www.java-examples.com/java- string-lower-case-example http://www.particle.kth.se/~lindsey/Ja vaCourse/Book/Part1/Java/Chapter02/ exercises.html http://www.particle.kth.se/~lindsey/Ja vaCourse/Book/Part1/Java/Chapter02/ exercises.html 46


Download ppt "JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1."

Similar presentations


Ads by Google