java oop programming language (part 5) - inheritance

39
Java Programming – Inheritance Oum Saokosal Master’s Degree in information systems, Jeonju University, South Korea 012 252 752 / 070 252 752 [email protected]

Upload: oum-saokosal

Post on 06-Jan-2017

71 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Java OOP Programming language (Part 5) - Inheritance

Java Programming –InheritanceOum Saokosal

Master’s Degree in information systems, JeonjuUniversity, South Korea

012 252 752 / 070 252 752

[email protected]

Page 2: Java OOP Programming language (Part 5) - Inheritance

Contact Me• Tel: 012 252 752 / 070 252 752

• Email: [email protected]

• FB Page: https://facebook.com/kosalgeek

• PPT: http://www.slideshare.net/oumsaokosal

• YouTube: https://www.youtube.com/user/oumsaokosal

• Twitter: https://twitter.com/okosal

• Web: http://kosalgeek.com

Page 3: Java OOP Programming language (Part 5) - Inheritance

Inheritance

1.What is Inheritance?

2.Why Inheritance?

3.How to use it?

4.Superclass & Subclass

5.Using keyword super

6.Overriding Methods

7.The Object class

3

Page 4: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance?

4

Page 5: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (1)

• OOP has 3 features:1. Class Encapsulation

2. Inheritance

3. Polymorphism

• OOP allows you to derive (create) new objects from existing classes. E.g.• You can create objects from a class:• Circle cir = new Circle();

• Word w = new Word(“N P I C”);

5

Page 6: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (2)

• But OOP has other mechanisms. One of them is called Inheritance.

• Inheritance is a mechanism to make classes inherit properties/methods from an existing class.

6

Page 7: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (3)

• In fact, every class in Java is alwaysinherited from an existing class, either explicitly or implicitly.

• In Java, every class is inherited from java.lang.Object.

To be clear, please look at an example at next slide.

7

Page 8: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (4) - Example

1. Please create a blank class, say, BlankSamplepublic class BlankSample {

}

2. Then create a test class, say, TestBlankpublic class TestBlank {

public static void main(String[] args){

BlankSample bs = new BlankSample();

System.out.print(bs.toString());

}

}

The question is why we can call bs.toString()?

If we look at BlankSample, there is toString(). Why? 8

Page 9: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (5) - IDE

9

Page 10: Java OOP Programming language (Part 5) - Inheritance

1. What is Inheritance? (6)

• Where these methods come from?

They are from java.lang.Object. Because every class in Java inherits from java.lang.Object.

• To be sure, please look at the API and find out java.lang.Object. Then see its methods.• clone(), equals(Object obj),

finalize(), getClass(), hashCode(), notify(),

notifyAll(), toString() and wait()

10

Page 11: Java OOP Programming language (Part 5) - Inheritance

2. Why Inheritance?

11

Page 12: Java OOP Programming language (Part 5) - Inheritance

2. Why Inheritance?

• Classes often share capabilities

•We want to avoid re-coding these capabilities

• Reuse of these would be best to

• Improve maintainability

• Reduce cost

• Improve “real world” modeling

12

Page 13: Java OOP Programming language (Part 5) - Inheritance

2. Why Inheritance? -Benefits

• No need to reinvent the wheel.

• Allows us to build on existing codes without having to copy it and past it or rewrite it again, etc.

• To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it.

• Make class more flexible.

13

Page 14: Java OOP Programming language (Part 5) - Inheritance

3. How to use it?

14

Page 15: Java OOP Programming language (Part 5) - Inheritance

3. How to use it? (1)• In Java, to enable a class inherit an existing class, we have to use a keyword

“extends”. For example, we have Circle class:

public class Circle{private double radius;

public Circle(){}public Circle(double radius){

this.radius = radius;}public void setRadius(double radius){

this.radius = radius;}public double findArea(){

return radius * radius *3.14;}

}15

Page 16: Java OOP Programming language (Part 5) - Inheritance

3. How to use it? (2)

• Then we want another class, say, TestCircle, inherits from the Circleclass.

public class TestCircle extends Circle{

public static void main(String[] args){

TestCircle tc1 = new TestCircle();

tc1.setRadius(5.0);

System.out.println(tc1.findArea());

}

}• Please note that TestCircle didn’t define setRadius() and getArea() methods but it

could use the methods.

• The reason is TestCircle inherits from Circle class.

16

Page 17: Java OOP Programming language (Part 5) - Inheritance

3. How to use it? – Note (1)

• Usually inheritance is used to improve features of an existing class.

• Please look at the code on page 288, listing 8.1 First Version of the Cylinder class.

• The Circle has already the findArea()

• So the formula to find Cylinder’s Volume is :

volume = Area * length

17

Page 18: Java OOP Programming language (Part 5) - Inheritance

3. How to use it? – Note (2)public class Cylinder extends Circle {

private double length = 1;

public double getLength(){return length;

}public void setLength(double length){

this.length = length;}public double findVolume(){

return findArea() * length;}

} 18

Page 19: Java OOP Programming language (Part 5) - Inheritance

3. How to use it? – Note (3)

public class TestCylinder {

public static void main(String[] args){

Cylinder c1 = new Cylinder();

c1.setRadius(2.5); // from Circle

c1.setLength(5); // from Cylinder

System.out.println(c1.findVolume());

}

}

• Please note that the cylinder’s object, c1, could call a method, “setLength()”, from Cylinder class and also could call a method, “setRadius()”, from Circle class.

19

Page 20: Java OOP Programming language (Part 5) - Inheritance

4. Superclass & Subclass

20

Page 21: Java OOP Programming language (Part 5) - Inheritance

4. Superclass & Subclass (1)

• The cylinder class inherits features from circle class. Then,• Cylinder is subclass

• Circle is superclass

Super inherit Subclass

21

Circle Cylinder

Page 22: Java OOP Programming language (Part 5) - Inheritance

4. Superclass & Subclass (2)

Quick Check: C1 <- C2 <- C3 <- C4

What are superclass and subclass?- C1 is the superclass of C2, C3, & C4- C2 are the subclass of C1 and the superclass of C3 & C4

- C3 are the subclass of C1 & C2 and the superclass of C4- C4 is the subclass of C1, C2 & C3

• It means if we call the final subclass, e.g. C4, then we can use features from C1, C2, C3, and, of course, C4 itself.

22

Page 23: Java OOP Programming language (Part 5) - Inheritance

4. Superclass & Subclass (3) – Java API

• Please check API Documentation:Javax.swing.JFrame is the subclass of Frame,Window,Container,Component,Object. So if we use JFrame, it means we use features from all of the superclasses.

23

Page 24: Java OOP Programming language (Part 5) - Inheritance

4. Superclass & Subclass (4)

• Sample of using JFrame

import javax.swing.*;

public class TestJFrame extends JFrame {

public static void main(String[] args){

TestJFrame frame = new TestJFrame();

frame.setTitle("Hi I am JFrame");

frame.setSize(400,300);

frame.setVisible(true);

frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE);

}

} // Note the underline codes 24

Page 25: Java OOP Programming language (Part 5) - Inheritance

5. Using keyword super

25

Page 26: Java OOP Programming language (Part 5) - Inheritance

5. Using keyword super (1)

super is used to call:

1. Constructors of the superclass

2. Methods of the superclass

26

Page 27: Java OOP Programming language (Part 5) - Inheritance

Using keyword super (2)

• To call constructors of the superclasssuper(); //call no-arg constructor

super(5.0); //call arg constructor

• Note

super():1. MUST be written in the 1st line of subclass

constructors

2. Cannot be written in other methods

3.Is the only way to call superclass constructor.

27

Page 28: Java OOP Programming language (Part 5) - Inheritance

Using keyword super (3)

• To call methods of the superclasssuper.setRadius(5); // setRadius(5);super.findArea();

super.toString();

Note:• This keyword is not always used to call methods from superclass.

• We can call superclass methods by calling directly the methods name. Please look at slide # 14.

• However, super is used not to confuse with the name of the overriding methods.

28

Page 29: Java OOP Programming language (Part 5) - Inheritance

6. Overriding Methods

29

Page 30: Java OOP Programming language (Part 5) - Inheritance

Overriding Methods (1)

In the real world:

• Researchers sometimes never invent or find a new thing. In fact, they just improve an existing thing.

• To improve the thing, they just:1. Add new features

2. Modify existing features.

30

Page 31: Java OOP Programming language (Part 5) - Inheritance

Overriding Methods (2)

In OOP:

It is true to the both things above. The inheritance helps us to do these. We can:

1. Add new methods to existing class

2. Modify the existing features. It is called Overriding Methods.

31

Page 32: Java OOP Programming language (Part 5) - Inheritance

Overriding Methods (3)

• Overriding method is a technique to modify a method in the superclass.

• Overriding method is a method, defined in subclass, which has the same name and return type to a method in superclass.

For example:

- The Circle has findArea() but Cylinderdoesn’t has it. If we call findArea(), it is always the Circle’s.

- But the cylinder can have findArea() for itself. This implementation is called overriding method.

32

Page 33: Java OOP Programming language (Part 5) - Inheritance

Overriding Methods (3)

• Please look at the code on page 292, Listing 8.2.

33

Page 34: Java OOP Programming language (Part 5) - Inheritance

Important Note (1)

1. In the subclass, we can invoke accessible things, e.g. public methods or constructor, from the superclass. E.g.:

- After a class inherits JFrame, then we can call setTitle(), setSize(), setVisible() etc.

2.In a constructor of subclass, the non-argconstructor of the superclass is ALWAYSinvoked.

3. A subclass can NEVER inherit a superclass which has no non-arg constructor. Let see slide “Important Note (3)”.

34

Page 35: Java OOP Programming language (Part 5) - Inheritance

Important Note (2)//Circle class

public class Circle{

private double radius;

public Circle(){ // non-arg constructor

radius = 5;}

public double findArea(){

return radius * radius * 3.14;

}

}

//TestCircle class

public class TestCircle extends Circle {

public static void main(String[] args){

TestCircle tc = new TestCircle();

System.out.println(tc.findArea());//output: 78.5

}

} 35

Page 36: Java OOP Programming language (Part 5) - Inheritance

Important Note (3)//Circle class

public class Circle{

private double radius;

//It doesn’t have non-arg constructor Here

public Circle(double radius){

this.radius = radius;

}

public double findArea(){

return radius * radius * 3.14;

}

}

//TestCircle class

public class TestCircle extends Circle {

public static void main(String[] args){

}

}36

cannot find symbolsymbol: constructor Circle()location: class Circle

1 error

Page 37: Java OOP Programming language (Part 5) - Inheritance

The Object class

37

Page 38: Java OOP Programming language (Part 5) - Inheritance

The Object class (1)

• public boolean equals(Object object)Indicates whether a object is "equal to" this one. E.g.:Circle c1 = new Circle();if(c1.equals(c1)){

}Note: We have to override it to test our comparison.

• public int hashCode()Returns a hash code value for the object. see “Java Collection Framework.”

38

Page 39: Java OOP Programming language (Part 5) - Inheritance

The Object class (2)

• public String toString()Return a string that represents the object. e.g.Circle c1 = new Circle();

c1.toString();

//output: Circle@24efe3

Note: We have to override it to display our wise.

39