Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Programming Lecture 2

Similar presentations


Presentation on theme: "Android Programming Lecture 2"— Presentation transcript:

1 Android Programming Lecture 2
Testing notes Intro to Java Programming

2 Agenda Classes & Objects Overview Packages Inheritance Abstraction
Interfaces Nested Classes Static nested & Inner classes Local & anonymous classes

3 Objectives Identify the basic parts of a Java program
Can identify a class and components of a class Read the codes in an app

4 Do{ if the baby is within 1-2 years old if the baby is happy today if the milk is still hot if … else … else …. else…} while(the baby is not 6 years old) A computer would ask you to: What is a baby? Define it! What is a feeding bottle? Define it! What is milk? Define! 我们该如何教授计算机去理解我们?

5 Class and Object

6 What is an “Object” An object is a thing, that we teach to the computer, so that the computer knows what the thing is Unlike people, a computer cannot see, feel and smell. It can only read codes. We have to define objects (or things) by describing the objects using (programming) languages. The way we describe a thing is composed of two parts Attributes (variables, properties): what an object has Methods: what an object can do

7 Bicycle Music Music Player

8 Example: Bicycle Define the object for computers by specifying
The properties the object has The functions the object provides Attributes: cadence gear speed Method: set cadence set the gear speed up brake

9 Bicycle

10 Bicycle Class can be thought of as a template, a prototype or a blueprint of an object is the fundamental structure in object-oriented programming

11 Constructor A special method which instantiates an object, it has the same name as the class. Must be used with the “new” operator. Code for initialization of the object can be found here. It can have initialization parameters.

12 To create a Bicycle object
Bicycle myBicycle = new Bicycle(5, 10, 12); An object is an instance of a class. Class are types of objects. Objects are actual things with memory space

13 Object Class Soldier aSoldier = new Soldier();

14 Naming Conventions Class Names: Class names representing types must be in mixed case starting with upper case. e.g., Bicycle, Circle Variable or Object Names: Variable or object names must be in mixed case starting with lower case. e.g., bicycle, circle Method Names: Names representing methods or functions must be verbs and written in mixed case starting with lower case. e.g., getName(), computeTotalWidth()

15 Package Include class Bicycle in the package com.example.test
Java includes the package at a level above classes. Packages can contain more than one class definition. Packages often contain libraries and can be defined in hierarchies.

16 com.example.test Class Bicycle myBicycle = new Bicycle(5, 10, 12);

17 package

18 package Bicycle myBicycle = new Bicycle(5, 10, 12); Bicycle

19 To use a package, you need to import it
The package statement (for example, package graphics;) must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. To use a package, you need to import it //in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file public abstract class Graphic { //in the Circle.java file public class Circle extends Graphic implements Draggable { //in the Rectangle.java file public class Rectangle extends Graphic //in the Point.java file public class Point extends Graphic Import a package member import graphics.Rectangle; Import an entire member import graphics.*;

20 Content of MainActivity
Content of MainActivity.java created in the HelloWorld Porject in Practical #1 Use the classes provided by android by importing the package Include the class in package Parent class Override methods in class Activity Call method to select layout

21 Inheritance

22 Inheritance To define a class is boring as we need to specify a lot of attributes and methods What if we need to create a new class object which is similar to an existing class object, but just with some minor modifications? Can I reuse the codes in the existing class? Inheritance solves code re-use issue. we can extend code that is already written in a manageable manner

23 increasingly specialized
Syntax Declaring subclasses class B extends A { } means class B is a specialization of class A the "is a" relationship exists a B object is an A object class B inherits the attributes and methods in class A A B "is a" increasingly general increasingly specialized Superclass

24 Example class TalkingParrot extends Parrot { … } When we say …
then a TalkingParrot object inherits all Parrot attributes In general, descendant classes inherit the attributes of all ancestor classes

25 Example In general, descendant classes inherit the attributes of all ancestor classes class FlyingBird extends Bird { … } class Parrot extends FlyingBird class TalkingParrot extends Parrot

26 The parent of a class is specified in the class definition with the extends reserved word.
[modifier] class class_name [extends parent_class] { …}

27 Three different modifiers can appear at the beginning of a class definition: public, abstract, and final. The visibility of variables and member functions (methods) defined in classes The public modifier makes the class visible to classes that are not in the same package. The abstract modifier specifies that the class cannot be instantiated. The final modifier specifies that the class cannot be extended.

28 Modifier

29 Abstract and Interface Class

30 An abstract class in incomplete It has “missing” method bodies
You cannot instantiate (create a new object of) an abstract class, because the computer does not know how to deal with the methods with unknown behaviors Used as the template class to create subclasses A class contains abstract methods A method that has been declared but not defined is an abstract method e.g., abstract void draw(); abstract void resize(); GraphicObjet aGraphicObject = new GraphicObject();

31 You can extend (subclass) an abstract class
//Implementation class Circle extends GraphicObject { void draw() { // circle specific implementation } void resize() { // circle specific resize class Rectangle extends GraphicObject { // rectangle specific implementation // rectangle specific resizing You can extend (subclass) an abstract class If the subclass defines all the inherited abstract methods, it is “complete” and can be instantiated If the subclass does not define all the inherited abstract methods, it too must be abstract

32 Abstract An abstract class cannot be instantiated.
Abstract class can be subclassed. An abstract method, only has signatures. If any class has abstract methods it must be declared abstract itself. Subclass must implement all the abstract methods of abstract class otherwise it must be declared abstract as well Abstract class may also contain abstract methods.

33 Why have abstract classes (1)
Suppose you wanted to create a class GraphicObject, with subclasses Circle, Rectangle, Triangle, Hexagon, etc. You don’t want to allow creation of a “GraphicObject” Only particular shapes make sense, not generic ones If GraphicObject is abstract, you can’t create a new GraphicObject You can create a new Oval, a new Rectangle, etc. Abstract classes are good for defining a general category containing specific, “concrete” classes

34 Why have abstract classes (2)
The abstract class defines a template of the subclass By defining the super class GraphicObject as an abstract class, you define the signature of methods without implementation. All subclass have to use the signature you have defined. By defining the signature in advance without the implementation (as we may not know how to implement), code collaboration becomes easy as others know the method signature of the class you are going to define, and can call the methods in their class.

35 Interfaces An interface declares (describes) methods but does not supply bodies for them interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e); } All the methods are implicitly public and abstract You can add these qualifiers if you like, but why bother? You cannot instantiate an interface An interface is like a very abstract class—none of its methods are defined An interface may also contain constants (final variables)

36 Circle draw() resize() draw() resize() Rectangle //Implementation
class Circle implements GraphicInterface { void draw() { // circle specific implementation } void resize() { // circle specific resize class Rectangle implements GraphicInterface { // rectangle specific implementation // rectangle specific resizing draw() Circle resize() draw() Rectangle resize()

37 Designing interfaces Most of the time, you will use Android-supplied interfaces Sometimes you will want to design your own You would write an interface if you want classes of various types to all have a certain set of capabilities In Android, you may extend a class, but you implement an interface A class can only extend (subclass) one other class, but it can implement as many interfaces as you like Example: When you say a class implements an interface, you are promising to define all the methods that were declared in the interface class MyListener implements KeyListener, ActionListener { … }

38 Why using Interface? Reason 1: A class can only extend one other class, but it can implement multiple interfaces This lets the class fill multiple “roles” In writing a UI widget in Android, it is common to have one class implement several different listeners Example: class MyUIWidget extends Applet implements ActionListener, KeyListener { } Reason 2: You can write methods that work for more than one kind of class

39 Interfaces vs Abstract Classes vs Normal Classes
A Java interface can declare methods But cannot implement them Methods of an interface are called abstract methods An abstract class can have: Abstract methods (no body) Normal methods (with body) Data fields Unlike a normal class, an abstract class ... Cannot be instantiated Can declare abstract methods Which must be implemented in all concrete subclasses

40 Nested Class

41 Inner Class Each Java file defines one class
Java allows defining class within another class. Inner classes are classes defined within other classes The class that includes the inner class is called the outer class There is no particular location where the definition of the inner class (or classes) must be place within the outer class Placing it first or last, however, will guarantee that it is easy to find

42 Simple Use of Inner Class
An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members An inner class is local to the outer class definition The name of an inner class may be reused for something else outside the outer class definition If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class

43 Static & Inner Nested Classes
public class Outer { private class Inner // inner class instance variables // inner class methods } // end of inner class definition // outer class instance variables // outer class methods }

44 Simple Uses of Inner Classes
There are two main advantages to inner classes They can make the outer class more self-contained since they are defined inside a class Both of their methods have access to each other's private methods and instance variables Using an inner class as a helping class is one of the most useful applications of inner classes If used as a helping class, an inner class should be marked private

45 Accessing Nested Inner Class
OuterClass oc = new OuterClass(); OuterClass.InnerClass innerClassObject = oc.new InnerClass(); //Way#1 //Way#2 OuterClass.InnerClass innerClassObject = new OuterClass().new InnerClass(); Way#1 is using previous instance so all values of the object of outer class will be intact and accessible via inner class Way#2 is creating new instance of outer class as well.

46 Accessing Nested Static Class
OuterClass.StaticNestedClass sncObj = new OuterClass.StaticNestedClass(); As simple as creating the normal object. No added syntax.

47 Anonymous Classes If an object is to be created, but there is no need to name the object's class, then an anonymous class definition can be used The class definition is embedded inside the expression with the new operator

48 Anonymous Inner Class

49 Java Comments Comments
These are notes written to a code for documentation purposes. Those texts are not part of the program and does not affect the flow of the program. 3 Types of comments in Java C++ Style Comments C Style Comments Special Javadoc Comments

50 Java Comments C++-Style Comments C++ Style comments starts with //
All the text after // are treated as comments For example: // This is a C++ style or single line comments

51 Java Comments C-Style Comments
C-style comments or also called multiline comments starts with a /* and ends with a */. All text in between the two delimeters are treated as comments. Unlike C++ style comments, it can span multiple lines. For example: /* this is an exmaple of a C style or multiline comments */

52 Java Comments Javadoc Comments Javadoc comments are used for generating an HTML documentation for your Java programs. You can create javadoc comments by starting the line with /** and ending it with */. /** This is an example of special java doc comments used for \n generating an html documentation. It uses tags like: @author Florence Balagtas @version 1.2 */

53 Summary Why do we need class and how to define a class in Java?
Why is a constructor used for? What is the content of the name conventions for Java Why do we need inheritance? How to define a subclass (e.g., CoolButton) of another class (e.g., Button)? Why do we need abstract class? Why do we need interface class? What is an anonymous class?


Download ppt "Android Programming Lecture 2"

Similar presentations


Ads by Google