SlideShare a Scribd company logo
Java Programming –
Inheritance
Oum Saokosal
Master’s Degree in information systems,Jeonju
University,South Korea
012 252 752 / 070 252 752
oumsaokosal@gmail.com
Contact Me
• Tel: 012 252 752 / 070 252 752
• Email: oumsaokosal@gmail.com
• 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
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
1.What is Inheritance?
4
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
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
1.What is Inheritance? (3)
• In fact, every class in Java is always
inherited 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
1. What is Inheritance? (4) - Example
1. Please createa blankclass,say, BlankSample
public class BlankSample {
}
2. Then create a test class, say,TestBlank
public class TestBlank {
public static void main(String[] args){
BlankSample bs = new BlankSample();
System.out.print(bs.toString());
}
}
The question iswhy we can call bs.toString()?
If we look at BlankSample,there istoString(). Why? 8
1.What is Inheritance? (5) - IDE
9
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
2.Why Inheritance?
11
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
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 differencesbetween the superclass
and the subclass that inherits from it.
• Make class more flexible.
13
3. How to use it?
14
3. How to use it? (1)
• In Java, to enable a class inherit an existing class, we have to usea 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
3. How to use it? (2)
• Then we want another class, say, TestCircle, inherits from the Circle
class.
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
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 alreadythe findArea()
• So the formulato findCylinder’sVolumeis :
volume =Area * length
17
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
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
4. Superclass &
Subclass
20
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
4. Superclass & Subclass (2)
Quick Check:
C1 <- C2 <- C3 <- C4
What are superclass and subclass?
- C1 is thesuperclass of C2,C3, &C4
- C2 are the subclass ofC1 and thesuperclass ofC3 &C4
- C3 are the subclassof C1 &C2 andthe superclass ofC4
- C4 is the subclass ofC1,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
4. Superclass & Subclass (3) – Java API
• Please checkAPIDocumentation:
Javax.swing.JFrame is the subclassof
Frame,Window,Container,Component,Object.
So if we use JFrame, it meanswe use features from all of
the superclasses.
23
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
5. Using keyword super
25
5. Using keyword super (1)
super is used to call:
1. Constructors of the superclass
2.Methods of the superclass
26
Using keyword super (2)
• To call constructors of the superclass
super(); //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 callsuperclassconstructor.
27
Using keyword super (3)
• To call methods of the superclass
super.setRadius(5); // setRadius(5);
super.findArea();
super.toString();
Note:
• This keyword isnot alwaysused to call methodsfrom superclass.
• We can call superclassmethodsby calling directly the methods
name.Please look at slide # 14.
• However, super isused not to confuse with the name of the
overriding methods.
28
6. Overriding Methods
29
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
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
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 Cylinder
doesn’t has it. If we callfindArea(), it is always
the Circle’s.
- But the cylindercan have findArea() for itself.
This implementationis calledoverridingmethod.
32
Overriding Methods (3)
•Please look at the code on page 292,
Listing 8.2.
33
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 classinheritsJFrame, then we can call
setTitle(), setSize(), setVisible() etc.
2.In a constructor of subclass, the non-arg
constructor of the superclass is ALWAYS
invoked.
3.A subclass can NEVER inherit a superclass
which has no non-arg constructor. Let see slide
“Important Note (3)”.
34
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
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 symbol
symbol: constructor
Circle()
location: class
Circle
1 error
The Object class
37
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
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

More Related Content

Viewers also liked (20)

Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with ExamplesJavascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for Android
OUM SAOKOSAL
 
Measuring And Defining The Experience Of Immersion In Games
Measuring And  Defining The  Experience Of  Immersion In GamesMeasuring And  Defining The  Experience Of  Immersion In Games
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
OUM SAOKOSAL
 
ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
Terminology In Telecommunication
Terminology In TelecommunicationTerminology In Telecommunication
Terminology In Telecommunication
OUM SAOKOSAL
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Tutorial 1
Tutorial 1Tutorial 1
Tutorial 1
Bible Tang
 
Actionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other NoteActionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
Kimchi Questionnaire
Kimchi QuestionnaireKimchi Questionnaire
Kimchi Questionnaire
OUM SAOKOSAL
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
babak danyal
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Rayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication SystemRayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication System
OUM SAOKOSAL
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Matt Harrison
 
Meetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonMeetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en python
Arthur Lutz
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with ExamplesJavascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for Android
OUM SAOKOSAL
 
Measuring And Defining The Experience Of Immersion In Games
Measuring And  Defining The  Experience Of  Immersion In GamesMeasuring And  Defining The  Experience Of  Immersion In Games
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
Terminology In Telecommunication
Terminology In TelecommunicationTerminology In Telecommunication
Terminology In Telecommunication
OUM SAOKOSAL
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM SAOKOSAL
 
Actionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other NoteActionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
Kimchi Questionnaire
Kimchi QuestionnaireKimchi Questionnaire
Kimchi Questionnaire
OUM SAOKOSAL
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
babak danyal
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Rayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication SystemRayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication System
OUM SAOKOSAL
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Matt Harrison
 
Meetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonMeetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en python
Arthur Lutz
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 

Similar to Java OOP Programming language (Part 5) - Inheritance (20)

Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
Oum Saokosal
 
7 inheritance
7 inheritance7 inheritance
7 inheritance
Abhijit Gaikwad
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
Hamid Ghorbani
 
Java Basics
Java BasicsJava Basics
Java Basics
Rajkattamuri
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
SivaSankari36
 
Java basic
Java basicJava basic
Java basic
Sonam Sharma
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
Bui Kiet
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
Hari Christian
 
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
CORE JAVA PPT FOR ENGINEERS  BBBBBBBBBBBBBBBBBBBCORE JAVA PPT FOR ENGINEERS  BBBBBBBBBBBBBBBBBBB
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
atharvtayde5632
 
Java Basics
Java BasicsJava Basics
Java Basics
F K
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
mcollison
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
HarshithaAllu
 
04inherit
04inherit04inherit
04inherit
Waheed Warraich
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
Oum Saokosal
 
03_chapter_inheritance for java OOP Design
03_chapter_inheritance for java OOP Design03_chapter_inheritance for java OOP Design
03_chapter_inheritance for java OOP Design
Lucky Sithole
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
OktJona
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part IIPROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part II
SivaSankari36
 
Inheritance in java computer programming app
Inheritance in java computer programming appInheritance in java computer programming app
Inheritance in java computer programming app
figeman282
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
Oum Saokosal
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
Bui Kiet
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
Hari Christian
 
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
CORE JAVA PPT FOR ENGINEERS  BBBBBBBBBBBBBBBBBBBCORE JAVA PPT FOR ENGINEERS  BBBBBBBBBBBBBBBBBBB
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
java02.pptsatrrhfhf https://www.slideshare.net/slideshow/java-notespdf-259708...
atharvtayde5632
 
Java Basics
Java BasicsJava Basics
Java Basics
F K
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
mcollison
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
Oum Saokosal
 
03_chapter_inheritance for java OOP Design
03_chapter_inheritance for java OOP Design03_chapter_inheritance for java OOP Design
03_chapter_inheritance for java OOP Design
Lucky Sithole
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
OktJona
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part IIPROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part II
SivaSankari36
 
Inheritance in java computer programming app
Inheritance in java computer programming appInheritance in java computer programming app
Inheritance in java computer programming app
figeman282
 
Ad

More from OUM SAOKOSAL (20)

Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Aggregate rank bringing order to web sites
Aggregate rank  bringing order to web sitesAggregate rank  bringing order to web sites
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
How to succeed in graduate school
How to succeed in graduate schoolHow to succeed in graduate school
How to succeed in graduate school
OUM SAOKOSAL
 
Google
GoogleGoogle
Google
OUM SAOKOSAL
 
E miner
E minerE miner
E miner
OUM SAOKOSAL
 
Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
Consumer acceptance of online banking an extension of the technology accepta...
Consumer acceptance of online banking  an extension of the technology accepta...Consumer acceptance of online banking  an extension of the technology accepta...
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
When Do People Help
When Do People HelpWhen Do People Help
When Do People Help
OUM SAOKOSAL
 
Mc Nemar
Mc NemarMc Nemar
Mc Nemar
OUM SAOKOSAL
 
Correlation Example
Correlation ExampleCorrelation Example
Correlation Example
OUM SAOKOSAL
 
Sem Ski Amos
Sem Ski AmosSem Ski Amos
Sem Ski Amos
OUM SAOKOSAL
 
Sem+Essentials
Sem+EssentialsSem+Essentials
Sem+Essentials
OUM SAOKOSAL
 
Path Spss Amos (1)
Path Spss Amos (1)Path Spss Amos (1)
Path Spss Amos (1)
OUM SAOKOSAL
 
How To Succeed In Graduate School
How To Succeed In Graduate SchoolHow To Succeed In Graduate School
How To Succeed In Graduate School
OUM SAOKOSAL
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 
Actionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And FlashActionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 
Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3
OUM SAOKOSAL
 
Actionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display ListActionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display List
OUM SAOKOSAL
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 Interactivity
OUM SAOKOSAL
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Aggregate rank bringing order to web sites
Aggregate rank  bringing order to web sitesAggregate rank  bringing order to web sites
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
How to succeed in graduate school
How to succeed in graduate schoolHow to succeed in graduate school
How to succeed in graduate school
OUM SAOKOSAL
 
Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
Consumer acceptance of online banking an extension of the technology accepta...
Consumer acceptance of online banking  an extension of the technology accepta...Consumer acceptance of online banking  an extension of the technology accepta...
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
When Do People Help
When Do People HelpWhen Do People Help
When Do People Help
OUM SAOKOSAL
 
Correlation Example
Correlation ExampleCorrelation Example
Correlation Example
OUM SAOKOSAL
 
Path Spss Amos (1)
Path Spss Amos (1)Path Spss Amos (1)
Path Spss Amos (1)
OUM SAOKOSAL
 
How To Succeed In Graduate School
How To Succeed In Graduate SchoolHow To Succeed In Graduate School
How To Succeed In Graduate School
OUM SAOKOSAL
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 
Actionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And FlashActionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 
Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3
OUM SAOKOSAL
 
Actionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display ListActionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display List
OUM SAOKOSAL
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 Interactivity
OUM SAOKOSAL
 
Ad

Recently uploaded (20)

Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 

Java OOP Programming language (Part 5) - Inheritance

  • 1. Java Programming – Inheritance Oum Saokosal Master’s Degree in information systems,Jeonju University,South Korea 012 252 752 / 070 252 752 [email protected]
  • 2. 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
  • 3. 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
  • 5. 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
  • 6. 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
  • 7. 1.What is Inheritance? (3) • In fact, every class in Java is always inherited 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
  • 8. 1. What is Inheritance? (4) - Example 1. Please createa blankclass,say, BlankSample public class BlankSample { } 2. Then create a test class, say,TestBlank public class TestBlank { public static void main(String[] args){ BlankSample bs = new BlankSample(); System.out.print(bs.toString()); } } The question iswhy we can call bs.toString()? If we look at BlankSample,there istoString(). Why? 8
  • 10. 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
  • 12. 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
  • 13. 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 differencesbetween the superclass and the subclass that inherits from it. • Make class more flexible. 13
  • 14. 3. How to use it? 14
  • 15. 3. How to use it? (1) • In Java, to enable a class inherit an existing class, we have to usea 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
  • 16. 3. How to use it? (2) • Then we want another class, say, TestCircle, inherits from the Circle class. 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
  • 17. 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 alreadythe findArea() • So the formulato findCylinder’sVolumeis : volume =Area * length 17
  • 18. 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
  • 19. 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
  • 21. 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
  • 22. 4. Superclass & Subclass (2) Quick Check: C1 <- C2 <- C3 <- C4 What are superclass and subclass? - C1 is thesuperclass of C2,C3, &C4 - C2 are the subclass ofC1 and thesuperclass ofC3 &C4 - C3 are the subclassof C1 &C2 andthe superclass ofC4 - C4 is the subclass ofC1,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
  • 23. 4. Superclass & Subclass (3) – Java API • Please checkAPIDocumentation: Javax.swing.JFrame is the subclassof Frame,Window,Container,Component,Object. So if we use JFrame, it meanswe use features from all of the superclasses. 23
  • 24. 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
  • 25. 5. Using keyword super 25
  • 26. 5. Using keyword super (1) super is used to call: 1. Constructors of the superclass 2.Methods of the superclass 26
  • 27. Using keyword super (2) • To call constructors of the superclass super(); //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 callsuperclassconstructor. 27
  • 28. Using keyword super (3) • To call methods of the superclass super.setRadius(5); // setRadius(5); super.findArea(); super.toString(); Note: • This keyword isnot alwaysused to call methodsfrom superclass. • We can call superclassmethodsby calling directly the methods name.Please look at slide # 14. • However, super isused not to confuse with the name of the overriding methods. 28
  • 30. 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
  • 31. 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
  • 32. 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 Cylinder doesn’t has it. If we callfindArea(), it is always the Circle’s. - But the cylindercan have findArea() for itself. This implementationis calledoverridingmethod. 32
  • 33. Overriding Methods (3) •Please look at the code on page 292, Listing 8.2. 33
  • 34. 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 classinheritsJFrame, then we can call setTitle(), setSize(), setVisible() etc. 2.In a constructor of subclass, the non-arg constructor of the superclass is ALWAYS invoked. 3.A subclass can NEVER inherit a superclass which has no non-arg constructor. Let see slide “Important Note (3)”. 34
  • 35. 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
  • 36. 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 symbol symbol: constructor Circle() location: class Circle 1 error
  • 38. 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
  • 39. 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