Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Classes-Objects in Java

Uploaded by

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

Classes-Objects in Java

Uploaded by

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

JAVA

P.Prathibha
Topics for Today’s Session

About JAVA

Creating Classes and Objects

How to Create class and Object

Command line arguments


Java
 Java is a computing platform for application
development and an object-oriented,
 Java is Class-based and Concurrent programming
language
 It means the code can be executed by multiple
processes at the same time.
 Java can run on all platforms and free to access.
 Java is Simple, Secure, Robust, Complete Object
oriented and Platform Independent High level
Language
 It is Portable and Multi-thread technology gives
High Performance.
Objects and Classes in Java
 An object in Java is the physical as well as a logical entity, whereas, a class in Java is a
logical entity only.
 An entity that has state and behavior is known as an object e.g., chair, bike, pen, table,
car, etc.
 It can be physical or logical (tangible and intangible). The example of an intangible
object is the banking system.
 An object consists of :
 State : It is represented by attributes of an object. It also reflects the properties
of an object.
 Behavior : It is represented by methods of an object. It also reflects the
response of an object with other objects.
 Identity : An object identity is typically implemented via a unique ID. The value of the ID is
not visible to the external user. However, it is used internally by the JVM to identify each
object uniquely.
For Example, Pen is an object. Its name is Reynolds; color is Blue, known as its state. It is used to
write, so writing is its behavior.

Behaviours
Identity State / Attributes Bark
Breed Eat
Color
Age Sleep
Bite
 An object is an instance of a class. A class is a template or blueprint from
which objects are created. So, an object is the instance(result) of a class.
Object Definitions:
 An object is a real-world entity.
 An object is a runtime entity.
 The object is an entity which has state and behavior.
 The object is an instance of a class.
Class
 A class is a group of objects which have common properties.
 It is a template or blueprint from which objects are created.
 It is a logical entity. It can't be physical.

 A class in Java can contain:


 Fields
 Methods
 Constructors
 Blocks
 Nested class and interface
Create a Class

 Example: Create a class named "MyClass" with a variable a:

Class classname [extends superclassname] class MyClass


{ {
[variable/field declaration];
[methods declaration];
int a = 5;
} }

Here
Everything inside the brackets is optional.
classname and superclassname are any valid java identifiers.
The keyword extends indicate that the properties of the superclass name are
extended to the classname class.
This concept is known as Inheritance.
Fields declaration:
 Data is encapsulated in a class by placing data fields in the
class. These variables are called “instance variables”.
 These are declared just as local variables;
Methods Declaration:
 A class with only data fields has no life. Such classes cannot
respond to any messages.
 We must add methods that are useful for manipulating the data
of the class.
 Methods for a class are declared inside the body of the class
but immediately after the declaration of the variables.
Type methodname (parameter list) Method declaration having four parts:
{ 1. The name of the method ( method name) (It is any
method_body; valid identifier)
}
2. The type of the value of the method returns (type)
(it is any valid data type, it is void when it does not
return any value.)
Method in Java 3. A list of parameters (parameter list) (This list
 In Java, a method is like a contains variable names and types of all the values
function which is used to we want to send as input, when no input data are
expose the behavior of an required, we must use empty parentheses)
object.
 Advantage of Method 4. The body of the method. It describes the operations
 Code Reusability to be performed on the data.
 Code Optimization Example: : int sum (int a, int b)
{
c=a+b;
return c;
Local variables

Instance variable

Class variables
.
Creating Objects:
 An object is a block of memory that contains space to store all the
instance variables.
 An object is a software entity (unit) that combines a set of data with
set of operations to manipulate that data.
 A class defines a type of object. i.e., each object belongs to some class
object is also called as an instance.
 Java has an operator new to create objects. Creating an object is also
referred to as instantiating an object.
 Objects in java can be created by using the syntax.
Syntax:
Classname objname; Example
Objname = new classname(); class Dog {
 The first statement declares a variable to hold String breed;
the object reference and second one actually int age;
assigns the object reference to the variable. String color;
 Ex void barking() {
triangle tri1; // declaring the object) }
Tri1 = new triangle //instantiating the object)
void hungry() {
The above two statements are combined into a }
single statement as void sleeping() {
}
Triangle tri1 = new triangle(); }
Accessing class Members: every object contains its own set of variables.
 We should assign values to these variables in order to use them in our
program.
 When we are outside the class we cannot access the instance variables and
methods directly. For this we use dot operator.

objectname.varaiblename = value;
objectname.methodname (parameter list);

Example: tri1.length = 15;


tri2.length = 20;
tri1.getData( 20,30)

public class MyClass {


int a = 5;

public static void main(String[]


args) {
MyClass myObj = new
MyClass();
System.out.println(myObj.a);
}
}
Ex: A java program to demonstrate application of classes, objects and methods.(Pra. Prog. 17)
class triangle {
int length, width;
Instance variables declared in a single
line
void getData(int x, int y) getData is a method, it does not return any value
{ so its type is void, we are passing two integer
length = x; values and they are assigned to length and width
width = y;
}
int triarea( )
triarea is another method, it returns a value of
{ data type int. We are not passing values, so
int area = (length * width)/2 ; the parameter list is empty
return (area);
}
}
class triarea { class with main method
public static void main (String args[ ])
{
int area1, area2;
triangle tri1 = new triangle(); Creating objects
triangle tri2 = new triangle();
tri1.length = 20; Accessing variables
tri1.width = 30;
area1 =( tri1.length * tri1.width)/2;
tri2.getData( 10, 15); Accessing Methods
area2 = tri2.triarea();
System.out.println( "area1 = " + area1);
System.out.println( "area2 = " + area2);
}
}
Command line Arguments
 The java command-line argument is an argument i.e. passed at the time of running the java
program.
 A Java application can accept any number of arguments from the command line.
 The String array stores all the arguments passed through the command line.
 Arguments are always stored as strings and always separated by white-space.

// Example using Commandline arguments


class Cmdargs {
public static void main(String[] args) {
System.out.println("Command-Line arguments
are");
// loop through all arguments
for(String str: args) {
System.out.println(str);
}
}}
1. To compile the code
javac Cmdargs.java output.
2. To run the code Command-Line arguments are
java Cmdargs Apple
Now suppose we want to pass some arguments Ball
while running the program, Cat
we can pass the arguments after the class name. For
example,
java Main apple ball cat
// Program to check for command line arguments
class Hello {
public static void main(String[] args) {
// check if length of args array is greater than 0
if (args.length > 0) {
System.out.println("The command line"+ " arguments are:");
// iterating the args array and printing the command line arguments
for (String s:args)
System.out.println(s);
}
else
System.out.println("No command line "+ "arguments found.");
}}

javac Hello.java javac Hello.java


java Hello Lion Dog Cat Elephant java Hello
Output: Output
The Command-Line arguments are No Command-Line arguments
Lion foun
Dog
Cat
Elephant

You might also like