CIS283: Introduction to Application Programming with Java - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CIS283: Introduction to Application Programming with Java

Description:

You can create your own class hierarchy using the keyword extends. Class Student extends Person ... Create methods called whatAmI() for Person and Student ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 25
Provided by: jpC3
Category:

less

Transcript and Presenter's Notes

Title: CIS283: Introduction to Application Programming with Java


1
CIS283 Introduction to Application Programming
with Java
  • Jose perez-carballo
  • Day 5

2
  • Creating classes and methods

3
  • How is a class defined
  • class Person
  • //body of the class
  • //includes instance variables
  • //class variables
  • //constructors
  • //methods
  • //nothing else

4
  • A class inherits (properties and methods) from
    its superclasses
  • The Object class is the superclass of all Java
    classes

5
  • You can create your own class hierarchy using the
    keyword extends
  • Class Student extends Person

6
  • Define the variables of class Person
  • Instance variables space for data each object of
    the class will have
  • E.g. firstName, lastName
  • Class variables space for data that all objects
    of the class will share
  • E.g. nrOfPersonsCreated
  • Use the word static to declare class variables

7
  • Since Student inherits from Person
  • All the variables Person has are also variables
    of Student (I.e. Student has firstName,
    lastName).

8
  • A class has variables (data) and methods
    (behavior)
  • To define a method we need
  • The name of the method
  • A list of parameters
  • The type of the value returned by the method
  • The body of the method

9
  • Examples of methods of Person
  • void setFirst(String ff)
  • // body of method
  • void setLast(String ll)
  • // body of method
  • String getFullName()
  • // body of method
  • // this one must return a String

10
  • If the type returned is void then the method does
    not return a value
  • If the type returned is no void then the method
    must return a value of that type

11
  • In order to use the classes we are defining we
    need a main method
  • Create a class with a main method
  • Declare in method main variables of type Student
    and Person
  • Test that Student inherits the properties of
    Person

12
  • Scope of a name
  • is the part of the program where that name exists
  • Look at the different scopes of names of
    variables and methods in our current example
  • See the code of class ScopeTest
  • Avoid using the same name of different variables

13
  • Passing arguments to methods
  • See class Passer

14
  • Class methods
  • (beware couple of typos in book, p 123
  • int now )
  • Class methods dont need an instance of the class
    to be called
  • Person pp0 new Person()
  • System.out.println("nr of persons so far "
    Person.howManyPersons())

15
  • Java applications
  • Need a main method
  • There should be only one method called main

16
  • Passing arguments to an application
  • Using the array of Strings
  • main(String args)
  • Write an application that counts how many
    arguments are passed to it
  • And prints the arguments back

17
  • All arguments passed to an application are
    Strings
  • If you want to use some as ints you must convert
    String to int
  • See Averager class

18
  • constructors
  • It is a method of the class
  • When a new object of a class is created
  • Java allocates memory for the object
  • Initializes instance variables
  • automatically executes one of the constructors of
    the class

19
  • A constructor
  • Must have the same name as the class
  • Doesnt have a return type (not even void)
  • Write a constructor for Person class

20
  • Overloading methods
  • Same method name, different method
  • The compiler knows they are different because
  • Different nr of arguments
  • Data types of the arguments
  • See example with Person

21
  • Constructors can be overloaded
  • Write more constructors for Person

22
  • Overriding methods
  • Given classes Person and Student
  • And object st1
  • When st.foo() is used
  • Look for a method called foo in Student
  • If not found look for it in Person
  • Continue looking in all super classes of Person
  • If not found report error
  • Otherwise use the first definition of foo found
  • Create methods called whatAmI() for Person and
    Student

23
  • You can still use the method of the super class
  • Write method whatIsMySuperclass() for Student

24
  • x
  • x
Write a Comment
User Comments (0)
About PowerShow.com