Download presentation
Presentation is loading. Please wait.
Published byAmberlynn Ryan Modified over 9 years ago
1
Java Applets and other things
2
Applets Very like usual classes Use init() method instead of main Applets provide 4 methods init() start() stop() destroy()
3
Usually only provide init Appletviewer will provide rest Applets compiled like usual classes Need to embed code in web page to see applet
4
Sample Applet import javax.swing.*; import java.awt.*; public class JGreet extends JApplet { Container con = getContentPane(); JLabel greet1 = new JLabel("hi"); public void init() { con.add(greet1); }
5
Web Page New Page 1
6
Style section <!-- div.Section1 {page:Section1;} p.MsoNormal {mso-style-parent:""; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman"; margin-left:0in; margin-right:0in; margin-top:0in} span.GramE {} span.SpellE {} -->
7
NB object embed
8
? operator Consider If (a>b) { max = a; } else { max = b; }
9
Using the ? operator The above could be written Max = (a>b) ? a:b;
10
First the condition (a>b) is tested If its true then a is returned i.e. value on left of : If its false then b is returned i.e. value on right of :
11
Abstract Classes In class hierarchies, the superclass is more general than its subclass(es). The superclass contains elements and properties common to all of the subclasses. The previous example was of a concrete superclass that instance objects can be created from. Often, the superclass will be set up as an abstract class which does not allow objects of its prototype to be created. In this case, only objects of the subclass are used. To do this the reserved word abstract is included in the class definition.
12
Example public abstract class Animal // class is abstract { private String name; public Animal(String nm) // constructor method { name=nm; } public String getName() // regular method { return (name); } public abstract void speak(); // abstract method - note no {} }
13
Abstract methods These are methods with no body specification. Subclasses must provide the method statements for their particular meaning. If the method was one provided by the superclass, it would require overriding in each subclass. And if one forgot to override, the applied method statements may be inappropriate
14
Polymorphism Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading and overriding are two types of polymorphism. Now we will look at the third type: dynamic method binding. Consider the following example
15
Example public class AnimalReference { public static void main(String args[]) Animal ref // set up var for an Animal Cow aCow = new Cow("Bossy"); // makes specific objects Dog aDog = new Dog("Rover"); Snake aSnake = new Snake("Earnie"); // now reference each as an Animal ref = aCow; ref.speak(); ref = aDog; ref.speak(); ref = aSnake; ref.speak(); }
16
Example Assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method. Notice that although each method reference was to an Animal (but no animal objects exist), the program is able to resolve the correct method related to the subclass object at runtime. This is known as dynamic (or late) method binding.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.