Docsity

Object Oriented Methodology - OOM - Part 4.PPT, Study notes of Object Oriented Programming

Partial preview of the text

Download Object Oriented Methodology - OOM - Part 4.PPT and more Study notes Object Oriented Programming in PDF only on Docsity!

1

Object Oriented Methodology Object Oriented Methodology

2

OOM OOM

 (^) AboutAbout OOP :OOP :

 (^) Procedure Oriented Vs. Object Oriented

 (^) OOP ConceptsOOP Concepts

 (^) Objects & Classes

 Writing a Class & instantiating Objects in

Java

Problem solving in OOP methodology

 OOPS Characteristics: Invoking Methods,

Inheritance: Making use of Inheritance.

 Super & Final method

 (^) Implementation issues in JavaImplementation issues in Java

  • (^) Organizing and Using Classes in Java

 (^) Java Features for OOPSJava Features for OOPS

 Java PackageJava Package

 J2SEJ2SE

Final Final

 (^) The final keyword appears before the class keyword when declaring a final class. For example:

public final class Hourly extends Employee

{

//Class definition...

}

 (^) A compiler error is generated if you try to write a class that subclasses Hourly. The following class declaration will not compile:

public class PartTime extends Hourly //error!

{

//Class definition...

}

4

final Methods final Methods

 (^) When a child class overrides a method in the parent class,

the overridden method is essentially hidden.

 (^) The only way the parent method can be invoked is if the

child method explicitly invokes it. If you write a method with important behavior that you do not want a child class to override, you can declare the method final.final.

 (^) A final method cannot be overridden by a child class.

 (^) The getClass(), wait(), and notify() methods of the Object

class are declared final. Their implementations are essential for the proper behavior of an object, and these methods cannot be overridden by any class.

5

Instantiation Instantiation

 (^) When an object is instantiated, the sequence of events occurs:

  1. The new operator invokes a constructor in the child

class.

  1. The child class may use the this keyword to invoke

another constructor in the child class. Eventually, a constructor in the child class will be invoked that does not have this() as its first line of code.

  1. Before any statements in the child constructor execute,

a parent class constructor must be invoked.

This is done using the super keyword. If a constructor does

not explicitly make a call using super, the compiler will call super() with empty parentheses, thereby invoking the no argument parent class constructor.

7

Instantiation Instantiation

  1. If the parent class is also a child of another class, the parent class constructor must invoke a constructor in its parent before executing any statements. Again, this is done using the supersuper keyword.
  2. This process continues until we reach the top of the hierarchy tree, which must be the Object class.
  3. The constructor in the Object class executes, then the flow of control returns to the constructor in the class just below Object in the inheritance hierarchy tree.
  4. The constructors execute their way down the hierarchy tree. The last constructor to execute is actually the one that was invoked first in Step 1.

8

 (^) Notice that the BigScreenTV constructor explicitly invokes the no argument constructor in Television using the statement:

super();

 (^) The following ConstructorDemo program instantiates a BigScreenTV object.

 (^) Try to determine the output,

public class ConstructorDemo

{

public static void main(String [] args)

{

System.out.println(“Constructing a big screen TV”);

BigScreenTV tv = new BigScreenTV();

System.out.println(“Done constructing TV”);

}

}

10

11

 (^) Within main() of ConstructorDemo, we instantiate a

BigScreenTV object

 (^) This statement invokes the noargument constructor

in the BigScreenTV class.

 (^) The constructor in BigScreenTV invokes the no

argument constructor in Television.

 (^) The noargument Television constructor invokes the

other Television constructor, which does not have a this() or super() statement.

 (^) Therefore, the compiler added super(), causing the no

argument constructor in Object to be invoked.

Instantiation

Object Oriented Methodology Object Oriented Methodology

 (^) Objects & Classes in JavaObjects & Classes in Java

Lab. Exercises  (^) Objective : Writing a Class in Java

 (^) Instantiating an Object : Understanding the Methods, references etc.  (^) Inheritance: Characteristics & Implementation  (^) Method Overloading & Overriding

 (^) Java Features for OOM: Organizing ClassesJava Features for OOM: Organizing Classes

How to Add class to a package

Java Platform & OOM

Packages in Java Platform

13

Organizing Classes in OOPS Organizing Classes in OOPS

 (^) As we have seen that we create Java Class for

problem solving in OOM.

 (^) As for each and every component , there is a Class

written, consequently we are having large number of Classes.

 (^) We are using various OOM characteristics like

Inheritance etc. for making use of Class Characteristics.

 (^) For the efficient handling of the Classes, for re

usability of some classes( which are required very often in many problem solving) and also to use many inherent Core / Foundation Classes, we need to Organize these Classes with useful mechanism.

14

Package Package

Every class belongs to a package. Packages have two basic purposes:

  1. Packages provide a mechanism for organizing classes.

  2. Packages provide a namespace for the classes within the package.

 (^) When developing a Java program, you put classes that go together in the same package.

 (^) Java packages can be stored in compressed files called JAR

files, allowing classes to download faster as a group rather than one at a time.

 (^) Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

 (^) Classes in the same package can access each other's packageaccess members.

16

Adding a Class to a Package Adding a Class to a Package

 (^) A class is added to a package using the package keyword.package

The package statement must be the first statement in your source code

 (^) For example, suppose we wanted to create a package named

payroll for the Employee, Salary, and Hourly classes used in our program to pay employees.

 (^) The package declaration must appear first in the statements

in the Employee source code.

 (^) The class will not compile if an attempt is made to put any

statement besides comments before the package declaration.

17

Adding a Class to Package Adding a Class to Package

 (^) If we want Salary and Hourly in the payroll package also, we simply add the package statement to their class definition as well. For example, the Salary class would look similar to:

package payroll; package payroll

public class Salary extends Employee

{

//Salary definition...

}

and the Hourly class would look similar to:

package payroll;package payroll

public class Hourly extends Employee

{

//Hourly definition...

}

19

The Namespace Created by Packages The Namespace Created by Packages

 (^) Packages create a namespace for all classes. If a class is in a

package , the name of the package becomes a part of the name of the class. The package name is prefixed to the name of your class, separated by the dot operator.

 (^) For example, the Employee class is declared in the payroll

package. The Employee class is now referred to:

payroll.Employee

 (^) The Employee class can no longer be referred to as simply

Employee. The package name payroll must be used by any other class outside of the payroll package that wants to use the Employee class. Similarly, the Salary and Hourly classes become:

 (^) payroll.Salary

 (^) payroll.Hourly

20