object oriented programming examples: c++, java advantages: 1. reusibility of code 2. ability to...

9
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Upload: ernest-bradley

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Object Oriented Programming

Examples: C++, Java

Advantages:

1. reusibility of code

2. ability to adapt (extend) previously written code

Page 2: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Def. Class - a coded computer entity that defines the attributes of an object, including data fields and methods for operating on that data.

Def. Object -

a computer entity that is created during the execution of a program and is an instance of a class, thereby possessing all of the attributes described in that class.

Note: many instances of the same class may be instantiated.

Page 3: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Classes can be extended

1. a subclass can be created which inherits all attributes from another class (its parent) or any other superclass that came before it.

2. The subclass can add its own methods and data fields.

Ex. Java provides the package called swing which has a large hierarchy of classes for creating graphical user interfaces.

Page 4: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Two type of Java programs:

1. applications - run alone like traditional programs

2. applets - may be used as part of a web page and require an HyperTextMarkupLanguage file which is run by a web browser or appletviewer program.

Page 5: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Java Applications

An application is a Java program that usually consists of two parts:

a client class

support classes.

The steps in writing a Java application are:1. Solve the problem by hand.2. Design Java classes to solve the problem.3. Write code for the classes and enter it using any text editor.4. Compile the classes by entering javac ClientClass.java5. Run the application by entering java ClientClass

Page 6: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

//Your name, date, course goes here.

//Purpose of the application here.

import javax.swing.* ;

public class MyFirstClientClass

{

public static void main(String [] args)

{

SupportClass myClass = new SupportClass();

myClass.doSomething();

}//end main method

}//end class

Page 7: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

//Your name, date, course and purpose of the application goes here.

import javax.swing.* ;

public class SupportClass

{ //data members go here

//default constructor

public Supportclass()

{}

//methods

public void doSomething( )

{ //statements go here}

}//end class

Page 8: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

Applets

1. Small Java programs designed to be part of a web page.

2. May reside on any computer attached to the Internet and may be run on any other computer (of any platform) using a program called a Web browser.

3. May also be run on the same computer on which it resides using a program called an appletviewer (which comes with the Java Virtual Machine)..

Page 9: Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code

4. The same object code (byte code) is sent to any user requesting it. The JVM on the user’s computer acts as an interpreter for the user’s particular computer.