2. A class is a blueprint of a set of objects that have a common
behaviour. Based on the blueprint, actual objects are created. In
simple words, a class is defines an outline, based on which actual
objects are created. A class can contain fields and methods to
describe the behaviour of an object. In general, class declarations
can include these components, in order:
Modifiers : A class can be public or has default access .
1) Class name: The name should begin with a initial letter
(capitalized by convention).
2) Superclass(if any): The name of the class’s parent (superclass), if
any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
3) Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
4) Body: The class body is surrounded by braces, { }.
3. Class is a user defined data type
Variables and functions can be created with in a class
Syntax:
class classname[extends superclassname]
{
[fields declaration];
[method declaration];
}
Example:
class Area
{
int length;
int breadth;
}
4. A class is a blueprint from which individual objects are created.
A class can contain any of the following variable types.
Local variables − Variables defined inside methods, constructors or
blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.
Instance variables − Instance variables are variables within a class
but outside any method. These variables are initialized when the
class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
Class variables − Class variables are variables declared within a
class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various
kinds of methods. In the example, barking(), hungry() and sleeping() are
methods.
5. public class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
6. It is a basic unit of Object Oriented Programming and
represents the real life entities. A typical Java
program creates many objects, which as you know,
interact by invoking methods. An object consists of :
State : It is represented by attributes of an object. It
also reflects the properties of an object.
Behaviour : It is represented by methods of an object.
It also reflects the response of an object with other
objects.
Identity : It gives a unique name to an object and
enables one object to interact with other objects.
Example of an object: dog
7.
8. class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4
{
public static void main(String args[])
{
10. A method is a named piece of code within a
program and executes when it is called from
another section of code.
Now we will see how to create a methods with
or without return values, invoke a method with
or without parameters, and apply method
abstraction in the program design.
In java, without methods class has no LIFE.
Methods are essential for manipulating the
data in program.
11. Methods are the interface or communications between program
components.
In java, a method must be defined before it is used anywhere in the
program.
SYNTAX: Example:
Type methodname(parameter list) public class Cube
{ {
method body; int length;
} int breadth;
int height;
Public int getVolume()
{
return(length*breadth*height);
} }
Note: type of the value method return.-it can be void,int,double etc.
12. If a class have multiple methods by same name but
different parameters, it is known as Method
Overloading. If we have to perform only one
operation, having same name of the methods
increases the readability of the program.
Method Overloading is also known as Static
Polymorphism.
Static Polymorphism is also known as compile time
binding or early binding.
Static Binding happens at compile time. Method
Overloading is an example of static binding where
binding of method call to its definition happens at
compile time.
13. If subclass (child class) has the same method
as declared in the parent class, it is known as
Method Overriding in Java.
In other words, if subclass provides the
specific implementation of the method that
has been provided by one of its parent class,
it is known as Method Overriding.
ADVANTAGE : The main advantage of method
overriding is that the class can give its own
specific implementation to a inherited
method without even modifying the parent
class(base class).
14. OVERLOADING-
class sum{
void add(int a, int b){
System.out.println(“sum of two
=”+(a+b)); }
void add(int a, int b, int c){
System.out.println(“sum of
three=“+(a+b+c)); }
}
class Polymorphism {
public static void main(String
args[]) {
Sum s=new Sum();
s.add(10,15); s.add(10,20,30);}
}
o/p:- Sum of two=25
Sum of three=60
OVERRIDING-
class A{
void fun(){
System.out.println(“HELLO”); } }
class B extends A{
void fun() {
System.out.println(“HI”); } }
class c {
public static void main(String
args[]) }
B ob=new B();
ob.fun();
A ob1=new A();
Ob1.fun(); }
}
o/p:- HI
HELLO
15. A command-line argument is the
information that follows the name of the program
on the command line of the operating system. The
java command-line argument is an argument that
is passed at the time of running of the java
program.
The arguments passed from the
console can be received in the java program and it
can be used as an input.
16. Command line argument provides a
convenient way to check the behaviour of the
program for the different values. We can pass N
(1,2,3 and so on) numbers of arguments from the
command prompt.
To access the command-line
arguments inside a Java program is quite easy.
They are stored as strings in the String array
passed to main().
Command line argument is used to
pass the argument at the time when we write the
command to run any program.
17. The following program of command line
argument displays only one argument that
it is called with-
class Commandline{
public static void main(String args[]){
System.out.println(“First argument is :”+
args[0]);
}
}
18. compile by > javac CommandLine.java
run by > java CommandLine suresh
OUTPUT:
First argument is: suresh
19. The following program displays all of the
command line arguments that is called with-
class CommandLine {
public static void main(String args[]) {
for(int i=0;i<args.length;i++) {
System.out.println(“args[“+ i +”]:“+ args[i]);
}
}
}
20. // Try executing this:
$java CommandLine this is a command line
10
Output
args[0]:this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 10