DOC PREVIEW
DREXEL CS 265 - java_OOP_and_ inheritance

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Basic OOP in JavaObjects and Classes in Javauser defined classesObjects and Object VariablesPassing Primitive TypesPassing Object ReferencesPerson ClassThe code is contained in the directory java/person_class on dunx1.An Example of InheritanceWe define Manager class, which extends Person class.The code is available in the directory java/manager_class on dunx1.public class ManagerTest{ public static void main(String[] args) { Manager P=new Manager("Harry", "Hacker", "Beatty_Rd", "Sprinfield", "NJ"); P.setAge(22); P.setSex("male"); P.setSalary(75000); System.out.println(P.getFirstName()); System.out.println(P.getLastName()); System.out.println(P.getStreet()); System.out.println(P.getCity()); System.out.println(P.getState()); System.out.println(P.getSex()); System.out.println(P.getAge()); System.out.println(P.getSalary()); }}class Person{ // constructor public Person(String aFirst, String aLast, String aStreet, String aCity, String aState) { firstname=aFirst; lastname=aLast; street=aStreet; city=aCity; state=aState; } //methods public String getFirstName() { return firstname; } public void setSex(String aSex) { sex=aSex; } //fields private String firstname; private String lastname; private String street; private String city; private String state; private String sex=""; private int age=0;}class Manager extends Person{ //construtor public Manager(String aFirst, String aLast, String aStreet, String aCity, String aState) { super(aFirst, aLast, aStreet, aCity, aState); salary=0; }//methods public int getSalary() { return salary; } public void setSalary(int aSalary) { salary=aSalary; } //fields private int salary;Basic OOP in Java Visit also http://java.sun.com/docs/books/tutorial/java/index.html. Objects and Classes in Java Program structure with user defined classes The basic structure of Java programs with user defined classes: public class ClassName { public static void main(String[] args) { program statements } user defined methods } user defined classes Class Definitions The basic structure of Java classes: class NameOfClass { constructors methods fields } Objects and Object Variables • Java constructors construct and initialize objects. • Constructors have the same name as the class. • Objects are constructed by an application of a constructor and the operator new. • Object variables store the reference to the object, not the object itself. Example: Date D=new Date(); // An object of a Date class is constructed and its reference System.out.println(D); // is stored inside variable D. Next we print out the object. Class Fields and Methods • Class fields define the state of an object. • Constructors define and initialize class fields. • Accessor methods access class fields of an object. • Mutator methods modify class fields. Example: inside main method: // We construct an object of Person class. Person P=new Person("Harry", "Hacker", "Beatty_Rd", "Sprinfield", "NJ");// We call mutator functions setAge() and setSex(). P.setAge(22); P.setSex("male"); //We call accessor functions getFirstName(), getLastName() and we print out the content // of the corresponding class fields. System.out.println(P.getFirstName()); System.out.println(P.getLastName()); Fields of Person class: private String firstname; private String lastname; private String street; private String city; private String state; private String sex=""; private int age=0; Parameter Passing Two kinds of method parameters • Primitive Types (variables storing numbers, characters, boolean values) • Object References (variables referring to class objects) In both cases the content of variables is passes by value. In the first case the method parameter contains the primitive type. In the second case the parameter contains the reference to the object, not the object itself. Examples: Passing Primitive Types inside main method: int n=2; square(n); System.out.println(n); // prints 2 square method: public static void square(int i) { i=i*i; System.out.println(i); // prints 4 } Passing Object References inside main: int n=2; int[] numbers = new int[n]; for(int i=0; i<numbers.length; i++) numbers[i]=i;for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); // prints 0 first and 1 next swap(numbers,0,1); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); //prints 1 first and 0 next swap method: public static void swap(int[] v, int i, int j) { int temp; temp=v[i]; v[i]=v[j]; v[j]=temp; } Person Class The code is contained in the directory java/person_class on dunx1. All methods of Person class are public. They may be accessed by any method in any class. All instance fields are private. Only methods of Person class may access them. public class PersonTest { public static void main(String[] args) { Person P=new Person("Harry", "Hacker", "Beatty_Rd", "Sprinfield", "NJ"); P.setAge(22); P.setSex("male"); System.out.println(P.getFirstName()); System.out.println(P.getLastName()); System.out.println(P.getStreet()); System.out.println(P.getCity()); System.out.println(P.getState()); System.out.println(P.getSex()); System.out.println(P.getAge()); } }class Person { // constructor public Person(String aFirst, String aLast, String aStreet, String aCity, String aState) { firstname=aFirst; lastname=aLast; street=aStreet; city=aCity; state=aState; } //methods public String getFirstName() { return firstname; } public String getLastName() { return lastname; } public String getStreet() { return street; } public String getCity() { return city; } public String getState() { return state; } public String getSex() { return sex; }public int getAge() { return age; } public void setAge(int aAge) { age=aAge; } public void setSex(String aSex) { sex=aSex; } //fields private String firstname; private String lastname; private String street; private String city; private String state; private String sex=""; private int age=0; }An Example of Inheritance We define Manager class, which extends Person class. The code is available in the directory java/manager_class on dunx1. public class ManagerTest { public static void main(String[] args) { Manager P=new Manager("Harry", "Hacker", "Beatty_Rd", "Sprinfield", "NJ"); P.setAge(22); P.setSex("male"); P.setSalary(75000);


View Full Document

DREXEL CS 265 - java_OOP_and_ inheritance

Download java_OOP_and_ inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view java_OOP_and_ inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view java_OOP_and_ inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?