• No results found

Instance Method: These are the methods which act upon instance variables of a class

In document CORE JAVA (Page 29-38)

Q: What is the difference between default constructor and parameterized constructor?

2) Instance Method: These are the methods which act upon instance variables of a class

Withrt s=new Withrt(10.3,20.5);

System.out.println("sum="+s.sum());

Withrt s1=new Withrt(23.5,12.6);

System.out.println("sum="+s1.sum());

} }

Types of Methods:

1) Static Method: Static methods are the methods which does not act upon the instance variables of a class. Static methods are declared as static by using a static keyword. Static methods are called using classname.methodname. Here no need to create an object. Static methods can act upon static variable. Static variable should be declared as static.

Ex: static double d1,d2,d3;

Example:

//Static method example class Sta

{

//static int a=10;

static int a1=10;

static void display() {

System.out.println(a1);

} }

class Static {

public static void main(String[] args) {

Sta.display();

} }

2) Instance Method: These are the methods which act upon instance variables of a class.

Instance methods are called using objectname.methodname.

a) Accessor Method: These methods access or read the instance variables.

b) Mutator Method: These methods not only read the instance variable but also modifies the contents of the object.

Example:

//Accesor and Mutator method class Person

{

String name;

int age;

char sex;

Person(String name,int age,char sex) {

this.name=name;

this.age=age;

this.sex=sex;

}

//Accesor Method void display() {

System.out.println("Name-->"+name);

System.out.println("Age-->"+age);

System.out.println("Sex-->"+sex);

}

//Mutator Method

Person modify(Person obj) {

obj.name="yyy";

--obj.age;

obj.sex='F';

return obj;

} }

class MutaAcc {

public static void main(String[] args) {

Person p=new Person("xxx",36,'M');

p.display();

Person p1=p.modify(p);

//Here we are not creating an object to p1. Here p1 is a reference variable.

p1.display();

p.display();

Person p2=new Person("sai",22,'M');

p2.display();

} }

3) Factory Method: A Factory method is a method that returns an object of the class to which it belongs. All the Factory methods are static method. When we use Factory Method we can not use new operator.

Example:

//Example for Factory Method import java.text.*;

class FactMe {

public static void main(String[] args) {

final double PI=(double)22/7;

double r=54.87;

double area=PI*r*r;

System.out.println("Area="+area);

NumberFormat obj=NumberFormat.getNumberInstance();

obj.setMaximumFractionDigits(2);

String str=obj.format(area);

System.out.println("Formatted Area="+str);

obj.setMinimumFractionDigits(6);

String str1=obj.format(area);

System.out.println("Formatted Area="+str1);

} }

Inner Class:

A class within another class is called inner class. We can not write private before main class. Private can be used only before inner class. Inner class not available to the other programmer. We can create an object to inner class only in its outer class.

Example:

//Inner class example

class BankAcct//Outer class {

private double bal;

BankAcct(double b) {

bal=b;

}

void start(double r) {

Interest in=new Interest(r);

in.calculateInterest();

}

private class Interest//Inner class {

private double rate;

Interest(double r)

{

rate=r;

}

void calculateInterest() {

double interest=bal*rate/100;

System.out.println("Interest="+interest);

bal+=interest;

System.out.println("Balance="+bal);

} }

}

class InnerDemo {

public static void main(String[] args) {

BankAcct acct=new BankAcct(100000);

acct.start(3);

} }

Inheritance:

Producing a new class from an existing class is called inheritance. The existing class is called super class and the newly produced class is called sub class.

Subclass object contains copy of the super class. The main advantage of

inheritance is the reusability of the code. We can create an object to the subclass only in inheritance.

Types of Inheritance:

1) Single Inheritance 2) Multilevel Inheritance 3) Multiple Inheritance 4) Hierarchical Inheritance 5) Hybrid Inheritance Example 1:

//Example of Inheritance(using setters and getters methods) class Teacher

{

int id;

String name,addr;

void setId(int id) {

this.id=id;

}

int getId() {

return id;

}

void setName(String name) {

this.name=name;

}

String getName() {

return name;

}

void setAddr(String addr) {

this.addr=addr;

}

String getAddr() {

return addr;

} }

class Student extends Teacher {

int marks;

void setMarks(int marks) {

this.marks=marks;

}

int getMarks() {

return marks;

} }

class Inherit {

public static void main(String[] args) {

Student st=new Student();

st.setId(1);

st.setName("xxx");

st.setAddr("131-22,P&T colony,DSNR");

st.setMarks(598);

System.out.println("ID="+st.getId());

System.out.println("NAME="+st.getName());

System.out.println("ADDRESS="+st.getAddr());

System.out.println("MARKS="+st.getMarks());

} }

Note: a) From above program all the Teacher class members are available to student class.

b) To access or read all the members of Teacher class(Super class) to Student class(Sub class) we use extends keyword.

Example 2:

class OneDemo {

OneDemo() {

System.out.println("OneDemo in super class!");

} }

class TwoDemo extends OneDemo {

TwoDemo() {

System.out.println("TwoDemo in sub class");

} }

class TDemo extends TwoDemo {

TDemo() {

System.out.println("TDemo in sub class");

} }

class InheritDemo {

public static void main(String[] args) {

TDemo t=new TDemo();

} }

Note: a) From above program first super class constructor is executed and than the sub class constructor is executed.

b) Super class parameterized constructor is not available to sub class.

c) Super is a keyword that refers to super class from a sub class. It means super can refer super class instance variable, super class constructor and also super class methods.

Example 3:

//Example for super class One

{

int x;

One(int x) {

this.x=x;

}

void show() {

System.out.println("super class method");

} }

class Two extends One {

int x;

Two(int a,int b) {

super(a);

x=b;

}

void show() {

super.show();

System.out.println(super.x);

System.out.println("Sub class method="+x);

} }

class SuperDemo {

public static void main(String[] args) {

Two t=new Two(100,299);

t.show();

} }

Polymorphism:

The word came from two Greek word. i) Poly—Poly means many and ii) Morph—morph means forms. If something exist in several form it is called polymorphism. If the same method performs various task is called

polymorphism. Different method bodies are required to perform various task.

In polymorphism there are two types:

a) Static polymorphism b) Dynamic polymorphism

Dynamic polymorphism: Dynamic means at runtime. The polymorphism executed at runtime is called dynamic polymorphism or runtime polymorphism or dynamic binding or dynamic linking. Here java compiler does not know which method is called by the user at the time of compilation. JVM can bind the method, call with the method body at the time of running the program.

Example:

Method Overloading: Writing two or more methods with the same name but with different method Signature.

//Method Overloading class Sample

{

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 Poly {

public static void main(String[] args) {

Sample s=new Sample();

s.add(10,20);

s.add(10,20,30);

} }

Method Signature: Method Signature represents method name and method parameters. When two or more methods are written with the same method name and there is a

difference in the method signature from can identify those method s different method.

The difference in the method signature may be due to the following factor:

a) There may be a difference in the number of parameter.

Ex: void add(int a, int b) void add(int a, int b, int c)

b) There is a difference in the data type of parameters Ex: void add(int a, int b)

void add(float a, float b)

c) There is a difference in the sequence of parameters.

Ex: int swap(int a, char b) Int swap(char a, int b)

Method Overriding: Writing two or more methods in super and sub classes with the same name and same signature is called method overriding. In method overriding JVM execute a method depending on the data type of reference variable used to call a method.

Example:

//Method Overriding class One

{

void calculate(double x) {

System.out.println("Square="+(x*x));

} }

class Two extends One {

void calculate(double x) {

super.calculate(2);

System.out.println("Square root value"+

Math.sqrt(x));

} }

class Poly1 {

public static void main(String[] args) {

Two t=new Two();

t.calculate(25);

t.calculate(9);

t.calculate(36);

} }

Static Polymorphism: The Polymorphism executed at compile time is called static polymorphism or static binding.

Use of Final keyword:

1) Final keyword is writing before constants.

2) Final keyword is used before the class name to prevent inheritance.

3) Final methods can not be overridden.

Type Casting or Casting:

Converting one data type into another type is called type casting or casting.

Casting primitive data type:

In document CORE JAVA (Page 29-38)

Related documents