Android Programming Lecture 2

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
MCS 270 Spring 2014 Object-Oriented Software Development.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CIT 590 Intro to Programming Lecture 13. Agenda A few topics that you have seen but might not have fully grasped Static Public, private, protected etc.
Object Oriented Programming
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object-Oriented Programming: Polymorphism Chapter 10.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Lecture 5:Interfaces and Abstract Classes
Introduction to Object-oriented Programming
Inheritance.
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Nested class.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 13 Abstract Classes and Interfaces
Comp 249 Programming Methodology
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Inheritance Inheritance is a fundamental Object Oriented concept
CMSC 202 Inner Classes.
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Presentation transcript:

Android Programming Lecture 2 Testing notes Intro to Java Programming

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

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

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! 我们该如何教授计算机去理解我们?

Class and Object

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

Bicycle Music Music Player

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

Bicycle

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

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.

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

Object Class Soldier aSoldier = new Soldier();

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()

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.

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

package

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

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.*; http://docs.oracle.com/javase/tutorial/java/package/createpkgs.html

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

Inheritance

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

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

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

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

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

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.

Modifier

Abstract and Interface Class

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();

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

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.

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

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.

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)

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()

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 { … }

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

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

Nested Class

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

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

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 }

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

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.

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

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

Anonymous Inner Class

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

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

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 */

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 */

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?