0% found this document useful (0 votes)
12 views

Object Oriented Programming: Java Language Characteristics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Object Oriented Programming: Java Language Characteristics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Oriented Programming

L1
Java Language Characteristics
• The portable, interpreted nature of Java impacts its performance. While the performance of
interpreted Java code is better than scripting languages and fast enough for interactive
applications, it is slower than traditional languages whose source code is compiled directly
into the machine code for a particular machine. To improve performance, Just-In-Time
compilers (JITs) have been developed. A JIT compiler runs concurrently with the Java Virtual
Machine and determines what pieces of Java code are called most often.

• The bytecode portability is what enables Java to be transported across a network and
executed on any target computer system. Java applets are small
Java programs designed to be included in an HTML (HyperText Markup Language) Web
document. HTML tags specify the name of the Java applet

+and its Uniform Resource Locator (URL). The URL is the location on the Internet at which the
applet bytecodes reside. When a Java-enabled Web browser displays an HTML document
containing an applet tag, the Java bytecodes are downloaded from the specified location and
the Java Virtual Machine interprets or executes the bytecodes. Java applets are what enable
Web pages to contain animated graphics and interactive content.

• Because Java applets can be downloaded from any system, security mechanisms exist within
the Java Virtual Machine to protect against malicious or errant applets.

• Java is an object-oriented programming language, borrowing heavily from Smalltalk,


Objective C, and C++. It is characterized by many as a better, safer C++. Java uses C++ syntax
and is readily accessible to the large existing C++ development community.

• Java, however, does not drag along the legacy of C. It does not allow global variables,
functions, or procedures. With the exception of a few primitive data types like integers or
floating-point numbers, everything in Java is an object.

• Object references are not pointers, and pointer manipulation is not allowed. This contributes
to the general robustness of Java programs since pointer operations tend to be particularly
nasty and bug-prone. Java also manages memory itself, thereby avoiding problems with
allocation and deallocation of objects. It does not allow multiple inheritance like C++ does,
but supports another type of reuse through the use of formal interface definitions.
• Java is similar enough to C and C++ that it already feels familiar to most of the existing
programming community. But it is different enough in important ways (memory management
and cross-platform portability) that it is worth it for programmers to switch to a new
language.

The Java Runtime Environment

In some Java technology runtime environments, apportion of the verified bytecode is compiled to
native machine code and executed directly on the hardware platform. What is the difference to use
that?
Classes and Objects:
The class is the essence of Java. It is the foundation upon which the entire Java language is built
because the class defines the nature of an object. As such, the class forms the basis for object oriented
programming in Java. Within a class are defined data and code that acts upon that data. The code is
contained in methods.

Class Fundamentals:
A class is a template that defines the form of an object. It specifies both the data and the code that will
operate on that data. Java uses a class specification to construct objects. Objects are instances of a class.
Thus, a class is essentially a set of plans that specify how to build an object. It is important to be clear on
one issue: a class is a logical abstraction. It is not until an object of that class has been created that a
physical representation of that class exists in memory.

One other point: recall that the methods and variables that constitute a class are called members of the
class. The data members are also referred to as instance variables.

Vehicle minivan = new Vehicle( );


This declaration performs two functions.

First, it declares a variable called minivan of the class type Vehicle. This variable does not define an
object. Instead, it is simply a variable that can refer to an object. Second, the declaration creates a
physical copy of the object and assigns to minivan a reference to that object. This is done by using the
new operator.
The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns
a reference to it. This reference is, more or less, the address in memory of the object allocated by new.
This reference is then stored in a variable. Thus, in Java, all class objects must be dynamically allocated.
Vehicle minivan; // declare reference to object
minivan = new Vehicle( ); // allocate a Vehicle object
The first line declares minivan as a reference to an object of type Vehicle. Thus, minivan is a variable
that can refer to an object, but it is not an object, itself. At this point, minivan contains the value null,
which means that it does not refer to an object. The next line creates a new Vehicle object and assigns a
reference to it to minivan. Now, minivan is linked with an object.

To access these variables, you will use the dot (.) operator. The dot operator links the name of an object
with the name of a member. The general form of the dot operator is shown here:

object.member
Reference Variables and Assignment
In an assignment operation, object reference variables act differently than do variables of a primitive
type, such as int. When you assign one primitive-type variable to another, the situation is
straightforward. The variable on the left receives a copy of the value of the variable on the right. When
you assign an object reference variable to another, the situation is a bit more complicated because you
are changing the object that the reference variable refers to.

The effect of this difference can cause some counterintuitive results. For example, consider the following
fragment:

Vehicle car1 = new Vehicle();


Vehicle car2 = car1;
At first glance, it is easy to think that car1 and car2 refer to different objects, but this is not the case.
Instead, car1 and car2 will both refer to the same object. The assignment of car1 to car2 simply makes
car2 refer to the same object as does car1. Thus, the object can be acted

L2
Method types
parameter Return type
‫يوجد‬ ‫يوجد‬
‫يوجد‬ ‫اليوجد‬
‫اليوجد‬ ‫يوجد‬
‫اليوجد‬ ‫اليوجد‬
L3
Constructors: A constructor initializes an object when it is created. It has the same
name as its class and is syntactically similar to a method. However, constructors
have no explicit return type. Typically, you will use a constructor to give initial
values to the instance variables defined by the class, or to perform any other
startup procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables
to zero. However, once you define your own constructor, the default constructor is
no longer used.
Controlling Access to Class Members: In essence, there are two basic types of
class members: public and private. A public member can be freely accessed by
code defined outside of its class. This is the type of class member that we have
been using up to this point. A private member can be accessed only by other
methods defined by its class. It is through the use of private members that access
is controlled. Restricting access to a class’s members is a fundamental part of
object-oriented programming because it helps prevent the misuse of an object. By
allowing access to private data only through a well-defined set of methods, you
can prevent improper values from being assigned to that data—by performing a
range check, for example. It is not possible for code outside the class to set the
value of a private member directly. You can also control precisely how and when
the data within an object is used. Thus, when correctly implemented, a class
creates a “black box” that can be used, but the inner workings of which are not
open to tampering. Up to this point, you haven’t had to worry about access
control because Java provides a default access setting in which the members of a
class are freely available to the other code in your program. (Thus, the default
access setting is essentially public.) .

Modifier What mean


public that member can be accessed by any other code in your program. This includes methods
defined inside other classes.
private that member can be accessed only by other members of its class. Thus, methods in
other classes cannot access a private member of another class.
default is the same as public unless your program is broken down into packages. A package is,
essentially, a grouping of classes.
protected is the same as public for the classes the child classes and it the same as private to other
classes

‫مكمالت الكالس‬

Constructure initializes an object when it is created. It has the same name as its class and is
syntactically similar to a method. However, constructors have no explicit return type
setMethods Used to set value to members that is private from another class
getMethods Used to get the value of members that is private from another class
toString Most return a String and don’t take any parameters , it will called automatically when
the object is converted to a string. So you don’t need to call it
L4
Encapsulation: Encapsulation is one of the four fundamental OOP concepts. The
other three are inheritance, polymorphism, and abstraction. Encapsulation is the
technique of making the fields in a class private and providing access to the fields
via public methods. If a field is declared private, it cannot be accessed by anyone
outside the class, thereby hiding the fields within the class. For this reason,
encapsulation is also referred to as data hiding. Encapsulation can be described as
a protective barrier that prevents the code and data being randomly accessed by
other code defined outside the class. Access to the data and code is tightly
controlled by an interface. The main benefit of encapsulation is the ability to
modify our implemented code without breaking the code of others who use our
code. With this feature Encapsulation gives maintainability, flexibility and
extensibility to our code.
Benefits of Encapsulation:
• The fields of a class can be made read-only or write-only.
• A class can have total control over what is stored in its fields.
• The users of a class do not know how the class stores its data. A class can
change the data type of a field and users of the class do not need to change
any of their code.

call-by-value. This approach copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the parameter of the
subroutine have no effect on the argument in the call.
call-by-reference. In this approach, a reference to an argument (not the value
of the argument) is passed to the parameter. Inside the subroutine, this
reference is used to access the actual argument specified in the call. This
means that changes made to the parameter will affect the argument used to
call the subroutine.
L5
This mechanism of deriving a new class from existing/old class is called
“inheritance”.
The inheritance allows subclasses to inherit all properties (variables and methods)
of their parent classes
The old class is known as “base” class, “super” class or “parent” class”;
the new class is known as “sub” class, “derived” class, or “child” class;

• Single inheritance (only one super class)


• Multiple inheritance (several super classes)
• Hierarchical inheritance (one super class, many sub classes)
• Multi-Level inheritance (derived from a derived class)
• Hybrid inheritance (more than two types)
• Multi-path inheritance (inheritance of some properties from two sources).

L6
Superclass Subclass Legal
Have constructure Have constructure Legal
Have Constructure Don’t have Constructure Legal
Don’t have Constructure Have constructure illegal
Don’t have Constructure Don’t have Constructure Legal
Hidden attribute : the attribute that have a same name of an attribute from a
subclass

Super(parameters): using for call constructure from a super class


Super.method/attribute: used for call a member from a super class

L7
To Overload method You must observe one important restriction
the type and/or number of the parameters of each overloaded method must
differ.
Q1\Why It is not sufficient for two methods to differ only in their return types?
Answer\ Return types do not provide sufficient information in all cases for Java to
decide which method to use

Signatures: the parameters and return type and name of a method , in another
word it’s the head of a method

Polymorphism:
Polymorphism means many (poly) shapes (morph)
In Java, polymorphism refers to the fact that you can have multiple methods with
the same name in the same class
There are two kinds of polymorphism:
Overloading
Two or more methods with different signatures
Overriding
Replacing an inherited method with another having the same signature

Why overload a method?


• So you can use the same names for methods that do essentially the same
thing
Example: println(int), println(double), println(boolean), println(String), etc.
• So you can supply defaults for the parameters:
int increment(int amount) {
count = count + amount;
return count;
}
int increment() { return increment(1); }
• So you can supply additional information:
void printResults() {
System.out.println("total = " + total + ", average = " + average);
}
void printResult(String message) {
System.out.println(message + ": ");
printResults();
}

*The double can store an int value but the int can’t store a double value

L8
when a method in a subclass has the same return type and signature as a method
in its superclass, then the method in the subclass is said to override the method in
the superclass.
The version of the method defined by the superclass will be hidden

An abstract method is created by specifying the abstract type modifier. An


abstract method contains no body and is, therefore, not implemented by the
superclass. Thus, a subclass must override it—it cannot simply use the version
defined in the superclass. To declare an abstract method, use this general form:
abstract type name(parameter-list);
The abstract modifier can be used only on normal methods. It cannot be applied
to static methods or to constructors.
Since an abstract class does not define a complete implementation, there can be
no objects of an abstract class. Thus, attempting to create an object of an abstract
class by using new will result in a compile-time error.
When a subclass inherits an abstract class, it must implement all of the abstract
methods in the superclass. If it doesn’t, then the subclass must also be specified
as abstract
Fixability degree Class type
Loest Fixability Concrete class
High Fixability Abstract class
Super Fixability Interface

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy