Professional Documents
Culture Documents
Classes Objects Methods
Classes Objects Methods
Classes Objects Methods
2
Classes, Objects & Methods
• object_name.variableName;
object_name.MethodName();
• Example of Constructor
} D:\>javac CommandLine
D:\>java CommandLine this is a command line 200 -100
• Public Access: Any variable or method is visible to the entire class in which
it is defined. But, to make a member accessible outside with objects, we
simply declare the variable or method as public. A variable or method
declared as public has the widest possible visibility and accessible
everywhere.
• Private Access: private fields have the highest degree of protection. They
are accessible only with their own class. They cannot be inherited by
subclasses and therefore not accessible in subclasses. In the case of
overriding public methods cannot be redefined as private type.
Own class ✓ ✓ ✓ ✓ ✓
Sub class
in same ✓ ✓ ✓ ✓
package
Other
classes
In same ✓ ✓ ✓
package
Sub class
in other ✓ ✓ ✓
package
Other
classes
In other ✓
package
Mrs. Chavan P.P.
Arrays & Strings
Types of arrays, creating an array
• Array is a collection of similar type of
elements that have contiguous memory
location.
• Types of Array in java
– Single Dimensional Array
– Multidimensional Array
For example:
For example:
For Example:
String str=“MMPolytechnic";
System.out.println(str.indexOf('P'));
System.out.println(str.indexOf(‘i', 3));
String subString=“tech"; System.out.println(str.indexOf(subString));
System.out.println(str.indexOf(subString,8));
Output:
2
11
6
-1
Mrs. Chavan P.P.
4. length() This function returns the number of characters in a String.
For example:
For example:
For Example:
String str = "0123456789";
System.out.println(str.substring(4));
Output : 456789
System.out.println(str.substring(4,7));
Output : 456
Mrs. Chavan P.P.
7. toLowerCase() This method returns string with all uppercase
characters converted to lowercase.
For Example:
Output : abcdef
For Example:
String str = "abcdef";
System.out.println(str.toUpperCase());
Output : ABCDEF
For example
int num=35;
String s1=String.valueOf(num); //converting int to String
System.out.println(s1+"IAmAString");
Output: 35IAmAString
10. trim() This method returns a string from which any leading and
trailing whitespaces has been removed.
For example
String str = " hello ";
System.out.println(str.trim());
output : hello
For example
String s1="java";
String s2="JAVA";
System.out.println(s1.equals(s2));
OUTPUT
false
For Example
String s1="java string";
s1.concat("is immutable");
Output
java string is immutable
Output : test123
Output : test123
Mrs. Chavan P.P.
• setCharAt() method sets the character at the
specified index to ch.
– setCharAt(int index, char ch)
• Here index is the index of the character to modify and
ch is the new character.
Output
After Set, buffer = AMIL
Output
length = 9
buff after new length = tutor
Mrs. Chavan P.P.
Vectors
• Vector implements a DYNAMIC ARRAY.
• Vectors can hold objects of any type and any
number.
• Vector class is contained in java.util package.
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Method Purpose
int i =Integer.parseInt(str) returns a signed decimal integer value
equivalent to string str
str=Integer.toString(i) returns a new String object representing
the integer i
byte b=intObject.byteValue() returns the value of this Integer as a byte
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
class Bike
{
final int speedlimit=80;//final variable
void run()
{
speedlimit=100;
}
public static void main(String args[])
{
Bike obj=new Bike();
obj.run(); Output: Compile Time Error
}
} Mrs. Chavan P.P.
Java final method
• If you make any method as final, you cannot override it.
• Example of final method
class Bike
{
final void run()
{
System.out.println("running");
}
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[])
{ Output: Compile Time Error
Honda honda= new Honda();
honda.run();
}
} Mrs. Chavan P.P.
Java final class
• If you make any class as final, you cannot extend it.
• Example of final class