Chapter 9 Slides - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Chapter 9 Slides

Description:

Inheritance is the process of using features (both attributes and methods) from ... The existing classis called the superclass and the new class, which inherits the ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 43
Provided by: johns379
Learn more at: http://kehs.ksd.org
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9 Slides


1
Exposure Java
Chapter 9 Slides
Introduction to Inheritance GUI
PowerPoint Presentation created by Mr. John L.
M. Schram
From Materials Created by Mr. Leon Schram
2
Inheritance Definition
Inheritance is the process of using features
(both attributes and methods) from an existing
class. The existing classis called the
superclass and the new class, which inherits the
superclass features, is called the subclass.
3
Is-A and Has-A
The creation of new classes with the help of
existing classes makes an important distinction
between two approaches. An "is-a" relationship
declares a new class as a specialnew-and-improved
case of an existing class. In Geometry, a
parallelogram "is-a" quadrilateral with special
properties. A has-a relationship declares a
new class composed of an existing class or
classes. A line has points, a square has lines,
and a cube has squares. A truck "is-a" vehicle,
but it "has-an" engine. In computer science an
"is-a" relationship involves inheritance and a
"has-a" relationship involves composition.
4
// Java0901.java // This program demonstrates
fundamental inheritance with ltextendsgt. // There
are no constructors yet, and the private data has
no value. public class Java0901 public
static void main(String args)
System.out.println("\nJAVA0901\n")
Student tom new Student()
tom.showAge() tom.showGrade()
System.out.println() class
Person private int age public
void showAge()
System.out.println("Person's Age is unknown right
now") class Student extends
Person private int grade public
void showGrade()
System.out.println("Student's Grade is unknown
right now")
Java0901.java Output JAVA0901 Person's Age is
unknown right now Student's Grade is unknown
right now
5
// Java0902.java // This program adds
constructors to the ltPersongt and ltStudentgt
classes. // Note how the ltPersongt constructor is
called, even though there does not appear to be a
ltPersongt object instantiated. public class
Java0902 public static void main(String
args) System.out.println(
"\nJAVA0902\n") Student tom new
Student() tom.showData()
System.out.println() class
Person private int age public
Person()
System.out.println("Person Constructor") age
17 public int getAge()
return age class Student extends
Person private int grade public
Student()
System.out.println("Student Constructor")
grade 12 public int
getGrade() return grade public
void showData()
System.out.println("Age " getAge())
System.out.println("Grade "
getGrade())
Java0902.java Output JAVA0902 Person
Constructor Student Constructor Age
17 Grade 12
6
Inheritance and Constructors
When an object of a subclass is instantiated,
the constructor of the superclass is called
first, followed by a call to the constructor of
the subclass.
7
// Java0903.java // This program shows that the
subclass does not have access to the private data
of the superclass. // This program will not
compile. public class Java0903 public
static void main(String args)
System.out.println("\nJAVA0903\n")
Student tom new Student()
tom.showData() System.out.println()
class Person private int
age public Person()
System.out.println("Person Constructor") age
17 class Student extends
Person private int grade public
Student()
System.out.println("Student Constructor")
grade 12 public void
showData()
System.out.println("Student's Grade is "
grade) System.out.println("Student'
s Age is " age)
Java0903.java Output Java0903.java27 age has
private access in Person System.out.println("
Student's Age is " age)

1 error
8
// Java0904.java // This program changes private
member data to "protected" data. // The Student
class can now access data from the Person
class. public class Java0904 public
static void main(String args)
System.out.println("\nJAVA0903\n")
Student tom new Student()
tom.showData() System.out.println()
class Person protected int
age public Person()
System.out.println("Person Constructor") age
17 class Student extends Person
private int grade public
Student()
System.out.println("Student Constructor")
grade 12 public void
showData()
System.out.println("Student's Grade is "
grade) System.out.println("Student'
s Age is " age)
Java0904.java Output JAVA0904 Person
Constructor Student Constructor Student's Grade
is 12 Student's Age is 17
9
Public Attributes
Attributes methods declared public can be
accessed by methods declared both outside and
inside the class.
10
Private Attributes
Attributes methods declared private can only
be accessed by methods declared inside the class.
11
Protected Attributes
Attributes methods declared protected can only
be accessed by methods declared inside the class
or subclass, and yes it can also be accessed
from anywhere in the same package.
12
// Java0905.java // This program uses a parameter
constructor to pass information to the Student
constructor. //Person still calls a default (no
parameter) constructor. public class Java0905
public static void main(String args)
System.out.println("\nJAVA0905\n"
) Student tom new Student(12)
tom.showData()
System.out.println() class Person
protected int age public Person()
System.out.println("Person
Default Constructor") age 17
public int getAge() return age class
Student extends Person protected int
grade public Student(int g)
System.out.println("Student Parameter
Constructor") grade g
public int getGrade() return grade
public void showData()
System.out.println("Student's Grade is "
grade) System.out.println("Student'
s Age is " age)
Java0905.java Output JAVA0905 Person Default
Constructor Student Parameter Constructor Student'
s Grade is 12 Student's Age is 17
13
// Java0906.java This program demonstrates how
to pass information to the superclass by using
the ltsupergt Java keyword. public class
Java0906 public static void main(String
args) System.out.println(
"\nJAVA0906\n") Student tom new
Student(12,17) tom.showData()
class Person protected int age
public Person(int a)
System.out.println("Person Parameter
Constructor") age a public
int getAge() return age class Student
extends Person protected int grade
public Student(int g, int a)
super(a) // this must be the first call
grade g
System.out.println("Student Parameter
Constructor") public int
getGrade() return grade public
void showData()
System.out.println("Student's Grade is "
grade) System.out.println("Student'
s Age is " age)
Java0906.java Output JAVA0906 Person Parameter
Constructor Student Parameter Constructor Student'
s Grade is 12 Student's Age is 17
14
// Java0907.java // In this program both the
ltPersongt class and the ltStudentgt class each have
a method with the same identifier. // The
program returns the grade information twice and
does not know that the ltgetDatagt method of the
ltPersongt // class should also be used. public
class Java0907 public static void
main(String args)
System.out.println("\nJAVA0907\n")
Student tom new Student(12,17)
tom.showData() System.out.println()
class Person protected int
age public Person(int a)
System.out.println("Person Parameter
Constructor") age a public int
getData() return age class Student
extends Person protected int grade
public Student(int g, int a) super(a)
grade g System.out.println("Student
Parameter Constructor") public int
getData() return grade public
void showData()
System.out.println("Student's Grade is "
getData()) System.out.println("Stud
ent's Age is " getData())
Java0907.java Output JAVA0907 Person Parameter
Constructor Student Parameter Constructor Student'
s Grade is 12 Student's Age is 12
15
// Java0908.java // This program solves the
problem of Java0907.java. // The Java keyword
ltsupergt is used to indicate that the getData
method of the superclass is intended. public
class Java0908 public static void
main(String args)
System.out.println("\nJAVA0907\n")
Student tom new Student(12,17)
tom.showData() System.out.println()
class Person protected int
age public Person(int a)
System.out.println("Person Parameter
Constructor") age a public int
getData() return age class Student
extends Person protected int grade
public Student(int g, int a) super(a)
grade g System.out.println("Student
Parameter Constructor") public int
getData() return grade public
void showData()
System.out.println("Student's Grade is "
getData()) System.out.println("Stud
ent's Age is " super.getData())
Java0908.java Output JAVA0908 Person Parameter
Constructor Student Parameter Constructor Student'
s Grade is 17 Student's Age is 12
16
Using super
The keyword super used as the first statement in
a constructor passes information to the super
class constructor, like super(a) The same
keyword super used in front of a method indicates
that a method of the superclass needs to be
called, like super.getData()
17
// Java0909.java // This program draws a graphics
string at graphics coordinate (20,50). // This
program is designed to execute as an Applet. //
The "awt" (Abstract Window Tools) package is used
to create GUI output. import java.awt.Graphics pu
blic class Java0909 extends java.applet.Applet
public void paint(Graphics screen)
screen.drawString("Welcome to
Applet GUI Output",5,40)
No main Method for applets! The web browser
handles that function.
lt!-- Java0909.html --gt lt!-- This html file is
necessary to execute the "Java0909" applet.
--gt lt!-- The applet will be shown in a 400 X 100
window. --gt lt!-- Use "appletviewer" or a
webbrowser to run the program. --gt ltAPPLET CODE
"Java0909.class" WIDTH400 HEIGHT100gt lt/APPLETgt

18
appletviewer
Any applet can be viewed in a web browser, but if
you dont want to take the time to do that, you
can also use appletviewer. C\gt appletviewer
Java0909.html
19
// Java0910.java // This program draws a graphics
string at graphics coordinate (20,50). // in a
graphics windows, sized 400 by 100 pixels. //
This program is designed to execute a GUI
application. // The "awt" (Abstract Window Tools)
package is used to create GUI output. import
java.awt. import java.awt.event. public class
Java0910 public static void main(String
args) Windows win new
Windows() win.setSize(400,100)
win.addWindowListener(new
WindowAdapter() public void
windowClosing(WindowEvent e) System.exit(0))
win.show() class
Windows extends Frame public void
paint(Graphics screen)
screen.drawString("Welcome to Application GUI
Output",20,50)
20
Application class Windows extends Frame public void paint(Graphics screen) screen.drawString("Application GUI Output",20,50)
Applet public class Java0709 extends java.applet.Applet public void paint(Graphics screen) screen.drawString("Applet GUI Output",5,40)
21
Application public class Java0710 public static void main(String args) Windows win new Windows() win.setSize(400,100) win.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0)) win.show()
Applet lt!-- Java0709.html --gt lt!-- This html file is necessary to execute the "Java0709" applet. --gt lt!-- The applet will be shown in a 400 X 100 window. --gt lt!-- Use "appletviewer" or a webbrowser to run the program. --gt ltAPPLET CODE "Java0709.class" WIDTH400 HEIGHT100gt lt/APPLETgt
22
// Java0911.java // This program demonstrates how
to draw lines. // Lines are drawn from (X1,Y1) to
(X2,Y2) with drawLine(X1,Y1,X2,Y2). // This
program execute as a Java applet. import
java.awt. public class Java0911 extends
java.applet.Applet public void
paint(Graphics screen)
screen.drawLine(0,0,800,600)
screen.drawLine(0,600,800,0)
screen.drawLine(100,300,700,300)
screen.drawLine(400,100,400,500)
lt!-- Java0911.html --gt ltAPPLET CODE
"Java0911.class" WIDTH800 HEIGHT600gt lt/APPLETgt
23
// Java0912.java // This program demonstrates how
to draw lines as was in Java0911.java. // Lines
are drawn from (X1,Y1) to (X2,Y2) with
drawLine(X1,Y1,X2,Y2). // This program execute as
an application. import java.awt. import
java.awt.event. public class Java0912
public static void main(String args)
Windows win new Windows()
win.setSize(800,600)
win.addWindowListener(new WindowAdapter() public
void windowClosing(WindowEvent e)
System.exit(0)) win.show()
class Windows extends Frame
public void paint(Graphics screen)
screen.drawLine(0,0,800,600)
screen.drawLine(0,600,800,0)
screen.drawLine(100,300,700,300)
screen.drawLine(400,100,400,500)
24
// Java0913.java // This program introduces the
rectangle command. A rectangle is drawn from //
the top-left (X,Y) coordinate of a rectangle
followed by Width and Height using //
ltdrawRect(X,Y,Width,Height)gt. The ltfillRectgt
command draws a rectangle filled with color. //
This program will execute as an applet. import
java.awt. public class Java0913 extends
java.applet.Applet public void
paint(Graphics screen)
screen.setColor(Color.red)
screen.drawRect(50,50,100,100)
screen.fillRect(100,300,300,200)
screen.setColor(Color.green)
screen.drawRect(300,100,300,150)
screen.fillRect(500,400,200,100)
lt!-- Java0913.html --gt ltAPPLET CODE
"Java0913.class" WIDTH800 HEIGHT600gt lt/APPLETgt
25
// Java0914.java // This program introduces the
rectangle command. A rectangle is drawn from //
the top-left (X,Y) coordinate of a rectangle
followed by Width and Height using //
ltdrawRect(X,Y,Width,Height)gt. The ltfillRectgt
command draws a rectangle filled with color. //
This program will execute as an
application. import java.awt. import
java.awt.event. public class Java0914
public static void main(String args)
Windows win new Windows()
win.setSize(800,600)
win.addWindowListener(new WindowAdapter() public
void windowClosing(WindowEvent e)
System.exit(0)) win.show()
class Windows extends Frame
public void paint(Graphics screen)
screen.setColor(Color.red)
screen.drawRect(50,50,100,100)
screen.fillRect(100,300,300,200)
screen.setColor(Color.green)
screen.drawRect(300,100,300,150)
screen.fillRect(500,400,200,100)
26
// Java0915.java // This program uses the
ltdrawOvalgt method to draw ovals and circles. //
The four parameters of the ltdrawOvalgt method are
identical to the ltdrawRectgt method. // With
ltdrawOval(X,Y,Width,Height)gt (X,Y) is the
coordinate of the top-left corner of the
rectangle // that circumscribes the oval. This
program will execute as an application. import
java.awt. import java.awt.event. public class
Java0915 public static void main(String
args) Windows win new Windows() win.se
tSize(800,600) win.addWindowListener(new
WindowAdapter() public void windowClosing(Windo
wEvent e) System.exit(0)) win.show()
class Windows extends Frame public void
paint(Graphics screen) screen.drawOval(50,50,
100,100) screen.fillOval(100,250,300,200) sc
reen.drawOval(400,50,200,300) screen.fillOval(5
00,350,200,200)
27
Lab 9A - 80 Point Version
28
Lab 9A - 90 Point Version
29
Lab 9A - 100 Point Version
30
GUI Input With Java Swing
31
// Java0916.java // This program introduces Java
GUI capabilities with ltswinggt. // This program
displays output in a dialog box. // The output
stays on the screen until the OK box is
clicked. import javax.swing.JOptionPane public
class Java0916 public static void main
(String args)
JOptionPane.showMessageDialog(null,"Java0916
Output") System.exit(0)

32
// Java0917.java // This program is identical to
Java0916 with detailed comments. import
javax.swing.JOptionPane // The import statement
allows access to the JOptionPane class. // This
class allows easy output to a window without
using a web page browser. public class
Java0917 public static void main (String
args) JOptionPane.showMessageDi
alog(null,"Same Old Dialog Box You Saw Before")
// The showMessageDialog method
requires two parameters. // For now
accept that the first parameter will be null.
System.exit(0) // This
statement must be used for proper program
termination.
33
// Java0918.java // This program shows that the
dialog box "stretches" to the size of the output
text. // The arrow indicates something that
should be on one line, that doesnt fit
here. import javax.swing.JOptionPane public
class Java0918 public static void main
(String args)
JOptionPane.showMessageDialog(null,"The Quick
Brown Fox Jumps Over The Lazy Dog, Once Every
Ten Years.")
System.exit(0)
34
// Java0919.java // This program displays
multiple lines in a dialog box // using the \n
"newline" escape sequence. import
javax.swing.JOptionPane public class
Java0919 public static void main (String
args) JOptionPane.showMes
sageDialog(null,"Line 1\nLine 2\nLine 3\nLine
4") System.exit(0)

35
// Java0920.java // This program uses separate
"showMessageDialog" statements. // Note how each
output line requires clicking the "OK"
button. import javax.swing.JOptionPane public
class Java0920 public static void main
(String args)
JOptionPane.showMessageDialog(null,"Line 1 \n")
JOptionPane.showMessageDialog(null,
"Line 2 \n") JOptionPane.showMessa
geDialog(null,"Line 3 \n")
JOptionPane.showMessageDialog(null,"Line 4 \n")
System.exit(0)

36
// Java0921.java // This program shows how to
enter keyboard information // into a dialog box.
All dialog input is a string. import
javax.swing.JOptionPane public class
Java0921 public static void main (String
args) String word //
stores string entered at the keyboard
word JOptionPane.showInputDialog("Enter a
word") JOptionPane.showMessageDial
og(null,word)
System.exit(0)
37
// Java0922.java // This program shows how to
"concatenate" keyboard information // into the
output message box. import javax.swing.JOptionPan
e public class Java0922 public static
void main (String args)
String firstName String lastName
firstName JOptionPane.showInputDial
og("Enter First Name") lastName
JOptionPane.showInputDialog("Enter Last Name")
JOptionPane.showMessageDialog(null,"Yo
u Entered " firstName " " lastName)
System.exit(0)
38
// Java0923.java // This program demonstrates
that program input can be repeated // by placing
the window statement inside a loop structure.
import javax.swing.JOptionPane public class
Java0923 public static void main (String
args) String firstName
String lastName for
(int k 1 k lt 3 k)
firstName JOptionPane.showInputDialo
g("Enter First Name")
lastName JOptionPane.showInputDialog("Enter
Last Name")
JOptionPane.showMessageDialog(null,"Hello "
firstName " " lastName)
System.exit(0)
39
// Java0924.java // This program enters integers
in small window boxes. // The sum of the entered
integers is then displayed. import
javax.swing.JOptionPane public class
Java0924 public static void main (String
args) String strNbr1,
strNbr2 int intNbr1, intNbr2,
sum strNbr1 JOptionPane.showInput
Dialog("Enter Number 1") strNbr2
JOptionPane.showInputDialog("Enter Number 2")
intNbr1 Integer.parseInt(strNbr1)
intNbr2 Integer.parseInt(strNbr2)
sum intNbr1 intNbr2
JOptionPane.showMessageDialog(null,intNbr1
" " intNbr2 " " sum)
System.exit(0)
40
// Java0925.java // This program demonstrates how
to enter real numbers // and display the sum of
the real numbers. import javax.swing.JOptionPane
public class Java0925 public static
void main (String args)
String strNbr1, strNbr2 double
realNbr1, realNbr2, sum strNbr1
JOptionPane.showInputDialog("Enter Number 1")
strNbr2 JOptionPane.showInputDialog(
"Enter Number 2") realNbr1
Double.parseDouble(strNbr1)
realNbr2 Double.parseDouble(strNbr2)
sum realNbr1 realNbr2
JOptionPane.showMessageDialog(null,realNbr1 "
" realNbr2 " " sum)
System.exit(0)
41
// Java0926.java // This program shows how to
include a title in the dialog box // by adding
the "INFORMATION_MESSAGE" parameter. import
javax.swing.JOptionPane public class Java0926
public static void main (String args)
String strNbr1, strNbr2
double realNbr1, realNbr2, sum
strNbr1 JOptionPane.showInputDialog("Enter
Number 1") strNbr2
JOptionPane.showInputDialog("Enter Number 2")
realNbr1 Double.parseDouble(strNbr1)
realNbr2 Double.parseDouble(strNb
r2) sum realNbr1 realNbr2
JOptionPane.showMessageDialog(null,realN
br1 " " realNbr2 " " sum,
"Display Sum",JOptionPane.INFORMATION_MES
SAGE)
System.exit(0)
42
// Java0927.java // This is the applet version of
the GUI input/output program shown earlier. //
Note how method init replaces the main
method. import javax.swing. public class
Java0927 extends JApplet public void
init() String strNbr1,
strNbr2 int intNbr1, intNbr2,
sum strNbr1 JOptionPane.showInput
Dialog("Enter Number 1") strNbr2
JOptionPane.showInputDialog("Enter Number 2")
intNbr1 Integer.parseInt(strNbr1)
intNbr2 Integer.parseInt(strNbr2)
sum intNbr1 intNbr2
JOptionPane.showMessageDialog(null,intNbr1
" " intNbr2 " " sum)
lt!-- Java0927.html --gt ltAPPLET CODE
"Java0927.class" WIDTH400 HEIGHT200gt lt/APPLETgt
Write a Comment
User Comments (0)
About PowerShow.com