Professional Documents
Culture Documents
Inheritance
Different kinds of objects often have a certain amount in common with
each other. Yet each also defines additional features that make them different:
tandem bicycles have two seats and two sets of handlebars; road bikes have
drop handlebars; some mountain bikes have an additional chain ring, giving them
a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and
behavior from other classes.
Classes and Objects
To differentiate between classes and objects, let us discuss an example:
Object
is composed of a set of data (properties) which are variables describing the
essential characteristics of the object, and consists of a set of methods
(behavior) that describes how an object behaves.
An object is an instance of a class.
Example of objects
objects in the physical world can easily be modeled as software objects using the
properties as data and the behaviors as methods.
Class
can be thought of as a template, a prototype or a blueprint of an object
is the fundamental structure in object-oriented programming
Two types of class members:
Fields (properties or attributes) - specify the data types defined
by the class
Methods - specify the operations
Class Instantiation
To create an object or an instance of a class, we use the new operator.
For example, if you want to create an instance of the class String, we write the
following code,
String str2 = new String(Hello world!);
or also equivalent to,
Concept of OOP
Encapsulation
It is the method of hiding certain elements of the implementation of a
certain class.
The method above has two parameters and displays the sum.
public static int computeSum(int x, int y) {
int sum=x+y;
return sum;
}
The method above has two parameters and return the sum where the method is
called.
To call a method,
<methodName>(<parameter>);
*This is only applicable to methods within the same class
Example:
message();
computeSum(5,10);
computeSum(a,b);
Sample program that uses the computeSum method:
public class UsingComputeSum{
public static void main(String args[]){
int sum, a=20, b=30;
//call computeSum and pass the value of a and b
sum=computeSum(a,b);
System.out.println(The sum is + sum);
}
A method may not have parameter list and it may not return any value.
Using the previous example computeSum method, we will just omit the word
static
public void computeSum(int x, int y) {
System.out.print(The sum is + (x+y));
}
computeSum method is now an instance method
Without parameter:
Classname.staticMethodName();
Another example:
//converts the String 10, to an integer
int i = Integer.parseInt(10);
Scope of a Variable
The scope
determines where in the program the variable is accessible.
determines the lifetime of a variable or how long the variable can exist in
memory.
2. Instance Method
Instance methods are methods that need an object to be used.
Instance method belongs to the instantiated object of a class.
your compiler will generate an error since you should have unique names for
your variables in one block.
However, you can have two variables of the same name, if they are not declared
in the same block. For example,
int test = 0; System.out.print( test );
//..some code here
{
int test = 20; System.out.print( test );
}
Coding Guidelines
Avoid having variables of the same name declared inside one
method to avoid confusion.
Exercises
Class name: UsingStaicMethod
1. Create a program that will ask for the grades in Math, Science, English
and Computer Science of a student. Create computeAve as static
method to compute and display the average of the grades.
Class name: UsingInstanceMethod
2. The same problem with Exercise No. 1 but change computeAve from
static method to instance method.
In the main method, the scopes of the variables are,
ages[] - scope A
i in B - scope B
i in C scope C
arr[] - scope D
i in E - scope E
When declaring variables, only one variable with a given identifier or name can
be declared in a scope.
That means that if you have the following declaration,
{
int test = 10;
int test = 20;
}