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

Programming in JAVA- Unit2class

The document provides an overview of key concepts in Java programming, including class declaration, object creation, methods, constructors, and method overloading. It explains the syntax for creating classes and objects, the purpose of methods and constructors, and the distinction between default and parameterized constructors. Additionally, it covers static members and the 'this' keyword, illustrating these concepts with code examples.

Uploaded by

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

Programming in JAVA- Unit2class

The document provides an overview of key concepts in Java programming, including class declaration, object creation, methods, constructors, and method overloading. It explains the syntax for creating classes and objects, the purpose of methods and constructors, and the distinction between default and parameterized constructors. Additionally, it covers static members and the 'this' keyword, illustrating these concepts with code examples.

Uploaded by

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

Topic/Title: CLASS , OBJECT , METHOD

Name of the Paper : PROGRAMMING IN JAVA


Semester : II
Department : Computer Science
Name of the Faculty : Dr.J.Vanathi
Designation : HEAD
Unit – II
Class Declaration in Java

access_modifier class <class_name>


{
data member;
method;
constructor;
nested class;
interface;
}
Example
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;}
There are three steps when creating an object from a class −
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.

Syntax to Create a Java Object


Consider the below syntax
Class_name object_name = new Class_name([parameters]);

class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
public static void main(String args[])
{
// creating an object of Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}}
Java Methods

A method is a block of code which only runs when it is called.


You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.

A method must be declared within a class.


It is defined with the name of the method, followed by parentheses ().
Java provides some pre-defined methods, such as System.out.println(), but you can
also create your own methods to perform certain actions:
// create a method

public class Main1 {


static void myMethod() {
// code to be executed
}
}

// run a method

public class Main1


{
static void myMethod()
{
System.out.println("I just got executed!");
}
public static void main(String[] args)
{
myMethod(); }
}
Parameters and Arguments
Information can be passed to methods as a parameter. Parameters act as variables
inside the method.

Parameters are specified after the method name, inside the parentheses. You can add
as many parameters as you want, just separate them with a comma.

public class Main {


static void myMethod(String fname) {
System.out.println(fname);
}

public static void main(String[] args) {


myMethod(“ABC");
myMethod(“DEF");
myMethod(“GHI");
}
}
Constructors in Java
In Java, a constructor is a block of codes similar to the method.
It is called when an instance of the class is created.
At the time of calling constructor, memory for the object is
allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least
one constructor is called.
It calls a default constructor if there is no constructor available in
the class.
In such case, Java compiler provides a default constructor by
default.
There are two types of constructors in Java: no-arg constructor,
and parameterized constructor.
Constructors in Java

Rules for creating Java constructor


There are two rules defned for the constructor.
1. Constructor name must be the same as its class name
2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, fnal, and synchronized
Types of Java constructors
There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any
parameter.

Syntax of default constructor:


<class_name>(){}

Example of default constructor


In this example, we are creating the no-arg constructor in the Bike class. It will
be invoked at the time of object creation.

//Java Program to create and call a default constructor


class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}}
Java Parameterized Constructor
A constructor which has a specifc number of parameters is called a
parameterized constructor.

Why use the parameterized constructor?


The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two
parameters.
We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n; }
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
METHOD OVERLOADING
Method overloading in Java means having two or more methods (or functions) in a
class with the same name and different arguments (or parameters).
It can be with a different number of arguments or different data types of arguments.
Method overloading is a form of compile-time polymorphism, meaning that the compiler
determines which method to call based on the method signature (name and parameter
list).
Method overloading is an example of Static Polymorphism.
Static Polymorphism is also known as compile time binding or early binding.
2. Static binding happens at compile time.

class OverloadingExample
{
static int add(int a,int b)
{return a+b;}
static int add(int a,int b,int c)
{return a+b+c;}
}
class DisplayOverloading
{
//adding two integer numbers
int add(int a, int b)
{
int sum = a+b;
return sum;
}
//adding three integer numbers
int add(int a, int b, int c)
{
int sum = a+b+c;
return sum;
}
}
class JavaExample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
Nesting Of Methods in Java
A nested method is a method that is defined inside another method.

Syntax to show the nesting of methods

class Main {
method1(){
}

method2(){
method1();
}

method3(){
method2();
}
}
//Nesting of Methods in Java
class demo {
private int m, n;
demo(int x, int y) {
m = x;
n = y;
}
int largest() {
if (m > n)
return m;
else
return n;
}
void display()
{
int large=largest();
System.out.println("The Greatest Number is : "+large);
}
}
public class nested_method {
public static void main(String args[]) {
demo o =new demo(10,20);
o.display();
}}
The this keyword in Java is a reference variable that refers to the current object.
It is used within an instance method or a constructor to access members of the current
object such as instance variables, methods, and constructors.

Syntax
this.memberName;
this.methodName();
this(parameters);

public class ThisExample {


int a;
int b;

ThisExample(int a, int b) {
this.a = a;
this.b = b;
}
void display() {
System.out.println("a: " + this.a + ", b: " + this.b);
}
public static void main(String[] args) {
ThisExample obj = new ThisExample(10, 20);
obj.display();
}}
STATIC MEMBERS
Static members are those which belongs to the class and you can access these
members without instantiating the class.

Static Methods − You can create a static method by using the keyword static. Static
methods can access only static fields, methods.
To access static methods there is no need to instantiate the class, you can do it just
using the class name.

Static Fields − You can create a static field by using the keyword static.
The static fields have the same value in all the instances of the class.
These are created and initialized when the class is loaded for the first time.
Just like static methods you can access static fields using the class name

public class MyClass {


public static int data = 20;

public static void main(String args[]){


System.out.println(MyClass.data);
}

You might also like