“ final ” keyword:-
“ final “ is a keyword in
java which can be used to declare
1.
class(final class)
2.
variables(constants)
3.
Methods or functions(final methods)
4.
Parameter inside a method(methodname(final
param))
5.
Local variable (final local variable)
1.class:-
If we use final to declare a
class this keyword destroys the concept of inheritance.
final class A {
//final
destroys the concept of inheritance A cannot extended by any other class.
}
class B extends A { }
|
|
class B { }
final class A extends B {
}
|
2.Variables:
Eg:-
class Final {
final int
X = 99;
// final Variables should be
initialized. // instance(non-static) constant.
//CONSTANTS will be declared in Caps .
static final int Y =
100; // static constant.
public static void main(String args[]) {
System.out.println(Y);
Final fin=new Final();
System.out.println(fin.x);
} }
|
|||
final parameters:-
we declare a parameter as
final that parameter can acts a constant inside a method.
We can not change the value
of final parameter inside the method
We can only access the
arguments which are passed while calling the method.
Same as above pgrm
class XXX{
int method1 ( final int a, final int b, final int c ) {
int
sum=a+b+c;
return sum; }
public static void main(String args[]) {
Int
tot=fin.method1(3,4,5);
s.o.pln(tot); }
|
Local variables as
final:-
We cannot the change the
value of local variable when we declare it as final.
class Final {
final int
X = 99; //
final Variables //should be intialized
// instance(non-static) constant.
static final int Y = 100; // static constant.
int method1( final int a, final int b,
final
int c )
{ //a = a+100; // compilation error
int
sum = a+b+c ;
return sum ; }
void method2()
{
final int
VAR = 212 ; // local variable// local constant
System.out.println(VAR);
VAR = VAR - 2 ;
} // compilation error.Because VAR
is final we cannot change itpublic static void
main(String args[]) {
Final fin = new Final();
fin.method2(); }
}
|
final methods:-
final methods can not be
overridden.
It means if there is a
method which is declared as amethod in one class we cannot change the
implementation in its subclass by overridden.
Ex:
class super {
final int
calculate (int a, int
b) {
return 0; //we
cannot overridden } }
class sub extends super {
int
calculate(int a,int
b) {
return 0; //comp error
} }
|
Program:-Object
polymorphism
class A {
int
calculate(int a,int
b) {
return a+b; }
}
class B extends A {
int
sum(int a,int
b,int c)
{
return a+b+c; }
int
calculate(int a,int
b) {
return a+b+100; }
}
public class ObjectPolymorphism extends B {
int
calculate(int a,int
b) {
return a+b+300; }
int
substract(int a,int
b) {
return a-b; }
public static
void main(String[] args)
{
B b=new
B();
ObjectPolymorphism op=new
ObjectPolymorphism();
A a=new
ObjectPolymorphism(); //super ref to sub class object
int tot=a.calculate(2,3);
System.out.println(tot); //sub
most class method
A a1=new B();
int res
=a1.calculate(100,98);
System.out.println(res); //subclass
method
A a2=new A();
int
resp=a2.calculate(400,500);
System.out.println(resp); }
} // a class method
|
Output:-
305
298
900.
|
Polymorphism:-
Concept of exhibiting
different behaviours in different situations by a method is known as
polymorphism.
There are 2 types of
polymorphism.
1.Method or static
polymorphism
2.Object or
dynamic polymorphism
Object or dynamic polymorphism:-
Same method will give
different result based on the object acting upon it.(object polymorphism)
In this case which method
has to be executed will be decided by the jvm during runtime(dynamic)
The above example completely
deals with object polymorphism.
We can achieve dynamic
polymorphism by using method overriddenig.
Method or static polymorphism:-
static
polymorphism:-
which method
has to be executed will be decided by the compiler during the compilation is known as static polymorphism.
Here the same method will
give different result based on the arguments that we are passing so this is
known as method polymorphism.
We can achieve the method
polymorphism using method overloading.
class StaticPoly {
int
sum( int a, int
b,int c)
{
return a+b+c ; }
int
sum( int a, int
b) {
return a+b; }
float
sum(int a, float
b) {
return a+b; }
public static
void main(String args[])
{
StaticPoly sp = new StaticPoly();
int
res1 = sp.sum(3,4,5);
System.out.println(res1);
int
res2 = sp.sum(3,4);
System.out.println(res2);
float
res3 = sp.sum(3,4.5f);
System.out.println(res3); }
}
|
Method overloading
or method overwriting:-
Concept of defining a method
with same name and with different signatures is known as method overloading.
Object
typecasting:-
class A {
int
calluculate1(int a, int b) {
return a+b; }
}
class B extends A {
int
sum(int a, int b, int c) {
return a+b+c ; }
int
calluculate2(int a, int
b) {
return a+b+100; }
}
class C extends B {
int
calluculate3(int a,int
b) {
return a+b+300 ; }
int
subtract(int y, int
z) {
return y-z ; }
public static
void main(String args[])
{
Object o = new A();
o.calluculate1(2,3); //compilation
error.Because calluculate1()is not in Object class
A a3
= (A)o;
a3.calluculate1(3,4);
A a1
= new B();
int
cal1 = a1.calluculate1(4,3);
System.out.println(cal1);
B b
= (B)a1;
int
sum = b.sum(1,2,3);
System.out.println(sum); }
}
|
Access Specifiers
in java:-
They are some keywords used
to represent accessibility criteria of variables,classes and methods.
They are 4 types of access
specifiers are there.
1. public
2. private
3. protected
4. default
There is no keyword called “default”.
Access specifier is a
keyword which is used to specify accessibility criteria of classes, variables,
methods.
A.java is inside
pack1;
public
class A {
private int x;
int
y;
protected int z;
private
void method1() {
System.out.println("private
method"); } }
|
B.java is also
inside of pack1;
public
class B extends A {
int
a;
private int b;
int
c;
public
void methodB() {
System.out.println("method
B"); }
public static
void main(String[] args)
{
B b1
= new B();
//b1.x=b1.x+100; //x
is private variable of A
b1.z
= b1.z+100;
System.out.println(b1.z);
//b1.method1(); //private method of A
b1.y
= b1.y+60;
System.out.println(b1.y);
} }
|
C.java is in
another package pack2;
package pack2;
import pack1.A;
public
class C extends A {
int
l;
private
int m;
public static
void main(String[] args)
{
A a=new
A();
//a.y=a.y+100; y is default variable of A
//a.z=a.z+70; z is protected variable of A
C c=new
C();
c.z=c.z+109;
System.out.println(c.z); }
}
|
Explanation for
access specifiers:-
Pack1.
A
B
Pack2
C
public à universally access
private à
access is limitted to the class
default à it acts as public
inside the package
private in other packages or
outside the package.
Package level access.
protected à protected is as similar as
default but there is one difference.
protected can be accessed inside the sub classes of the class in other
packages also.
Public
|
Private
|
Protected
|
default
|
|
A class
|
Y
|
Y
|
Y
|
Y
|
B sub class in the same package
|
Y
|
N
|
Y
|
Y
|
C subclass in other package
|
Y
|
N
|
Y
|
N
|
Non sub class in
Other package
|
Y
|
N
|
N
|
N
|
Structures:-
They are 3 kinds of
structers in java.
1.
Fully implemented
structure. (Class)
2.
Partially implemented
structure. (Abstract class)
3.
Unimplemented structure. (Interface)
Abstract class:-
Ø “abstract”
is a keyword which destroys the concept of “instantiation”.
Ø “instantiation”
means we cannot create object of abstract class.
abstract class Abstract {
}
class Sub extends Abstarct {
public static
void main(String args[])
{
Abstract ab = new Abstract(); // gives
compilation error.we cannot create object of
} }
abstract class//'abstract' destroys the concept of
instantiation.
|
Abstract methods:-
abstract class Person {
void walk() {
System.out.println("person
can walk"); }
abstract void gender(); } // no
body for abstract method
class MalePerson extends Person {
void gender() {
System.out.println("male
person"); } }
class FemalePerson extends Person {
void gender() {
System.out.println("female
person"); } }
class Demo {
public static
void main(String args[])
{
// Person p = new Person(); // compilation error
MalePerson mp = new MalePerson();
mp.gender();
FemalePerson fp = new FemalePerson();
fp.gender(); }
}
|
Ø we
can use “abstract” keyword to declare methods which are known as “abstract
methods”.
Ø Abstract
methods are the methods which will have no implementation.
Ø But,abstract
methods can be overridden.that means we can provide implementation in
subclasses.
Ø If
there is atleast one method as abstract method then we have to declare that
class as abstract class.
Ø We can
have abstract class without any abstract method.
Ø Program:-
abstract class Ab1 {
public abstract
void method1();
abstract void method2();
abstract void method3();
}
abstract class Ab1 extends Ab2 {
public void method1()
{ //stmts }
public void method2()
{ //stmts }
abstract void method3();
}
Class Ab3 extends Ab2 {
public void method3()
{ //stmts
} }
|
Explanation:-
Ø If
we have abstract class with 3 abstract methods in its subclass ,if we want to
provide implementation for 2 methods only, we have to provide that,but we have
to provide re declaration of abstract methods in Ab2.
Ø Since
we have declare a method as abstract method in a class then declare that class
as abstract class.
Ø Because Ab2 is an abstract class again we cannot
create an object out of it.so.we can extends this class provides implementation
to abstract methods in subclass Ab3;
Ø Now
we can create out of Ab3 only.
Important
program:-
abstract class Ab1 {
public abstract
void method1();
abstract void method2();
abstract void method3();
abstract void method4();
}
abstract class Ab2 extends Ab1 {
public void method1() {
System.out.println("method1()"); }
public void method2() {
System.out.println("method2()"); }
abstract void method3();
abstract void method4(); }
class Ab3 extends Ab2 {
public void method1() {
System.out.println("change
method1()"); }
public void method3() {
System.out.println("method3()"); }
public void method4() {
System.out.println("method4()"); }
public static
void main(String[] args)
{
Ab3 ab3 = new Ab3();
ab3.method4();
ab3.method3();
ab3.method2();
Ab1 ab1 = new Ab3(); //(Ab1 is Abstract class )
Ab2 ab2 = new Ab3();
ab1.method1();
} }
|
Note:-
We cannot create objects of
abstract classes but we can use references variable of abstract class to refer
the sub class object.
The above program is
referring that scenario.
(2) Interface (un-implemented structures):-
“interface “ is a keyword to
define un-implemented structures.
Interface is a specification
of method declarations (or) contact specification to the class.
class :-
define behaviour of a memory .
Eg:-
interface CustomerInterface {
boolean saveCustomer();
// public and abstract by //default
void updateCustomer();
void delecteCustomer();
}
class CustomerImpl
implements CustomerInterface {
// we have to provide the implementation
to all the methods. when we implement the interface
public boolean saveCustomer()
{
// in the sub structures we have to use stronger access specifiers than in super
//Structures.
boolean saved = false ;
System.out.println("cusotmer
is saved");
saved = true ;
return saved ; }
public void updateCustomer()
{
System.out.println("
Update cusotmer "); }
public void delecteCustomer()
{
System.out.println("
delete cusotmer "); }
public
static void main(String args[]) {
CustomerInterface custInter = new CustomerImpl();
// interface reference variable is used to
//refer its implementation class object
boolean saved = custInter.saveCustomer();
System.out.println(saved);
custInter.delecteCustomer();
} }
|
Interface reference variable
is used to refer its implementation class object.
Differences b/w class, interface and abstract class:-
Class:- we can always create an object to the
class only.
Interface:- we can’t create object to the interface, but
we can use its reference variables to refer its implementation class object.
Abstract class:- we can’t create object to
abstract class, but we can use its reference variable to refer its subclass
object.
Program:-
interface Ione {
void
method1();
void
method2();
void
method3();
}
abstract class ImplOne implements Ione {
public void method1() {
System.out.println("this
is method1");
}
public abstract
void method2();
public abstract
void method3();
}
class ImplTwo extends ImplOne {
public void method2() {
System.out.println("this
is method2");
}
public void method3() {
System.out.println("this
is method3");
}
public
static void main(String args[]) {
Ione obj = new
ImplTwo();
obj.method1();
obj. method2();
obj. method3();
ImplOne one = new ImplTwo();
one.method3();
} }
|
Note:- Duplicate methods, variables, local
variables are not allowed in java.
class Sample {
void read() {
System.out.println(“can
read”);
}
void read() {
System.out.println(“any
one can read”);
}
public
static void main(String args[]) {
Sample s = new
Sample();
s.read();
} }
|
Eg:-
Explanation:-
In the above Sample
class having 2 read() mathods with same signature,it will give you a
compilation error.this can’t be allowed in java.
Program:-
interface Istudent1 {
void read();
void write(); }
interface Istudent2 {
void read();
void play(); }
class Student
implements Istudent1,Istudent2 {
public void read() {
System.out.println("Student
can read"); }
public void write() {
System.out.println("Student
can write"); }
public void play() {
System.out.println("Student
can play"); }
public
static void main(String[] args) {
Istudent1 ione
=new Student();
ione .read();
ione .write();
Istudent itwo =
new Student();
itwo .paly();
itwo .read(); }
}
|
Package creation:-
Right click on src à new à package, then give package name and say finish.
Program:-
package com.itargetit;
interface Inter1 {
int
X=0; //inside the interface, variable act as a final
variable means constant variable
void imethod1(); }
public class
inter1Impl implements
Inter1
{
public void imethod1()
{
System.out.println(“imethod”); }
public static
void main(String args[])
{
Inter1 in1=new inter1Impl();
in1.imethod1();
} }
|
package com.itargetit;
public interface
Inter2 extends Inter1 { // we can extend one interface with the other interface
void imethod2();
}
public
class inter2Impl implements
Inter2 {
public void imethod2() {
System.out.println(“imethod2
of inter2impl”); }
public void imethod1() {
System.out.println(“imethod1()
of inter2Impl”); }
public static
void main(String args[])
{
Inter1 i1= new inter2Impl();
int
y= i1.X;
System.out.println(“x
value in interface”+y);
i1.imethod1();
// i1.imethod2(); //compilation error
Inter2 i2= new
inter2Impl();
i2.imethod2();
i2.imethod1(); }
}
|
We should not use java keywords as
class,methods,interface,package names and variable names.
Method with interface as a parameter:-
If any method is defined
with an interface as parameter while calling that method we have to pass the
object of implementation class of the
interface.
package com.itargetit.interfaceDemo;
import com.itargetit.Inter1;
import com.itargetit.Inter1Impl;
public class Demo {
void demomethod(Inter1 i1)
{
// method is
expecting interface as parameter, so while calling we should pass inter face object(object of its implementation
class).
i1.imethod1(); }
public static
void main(String args[])
{
Demo demo=new Demo();
Inter1 i1=new inter1Impl();
demo.demomethod(i1);
//we are
passing implementation class object reference variable as //an argument to
the function.
} }
|
A method with interface as a return type:-
class
Demo {
Inter1 retmathod()
{
Inter1 i1=new inter1Impl();
i1.imethod1();
return i1; }
public static
void main (String args[])
{
Demo demo=new Demo();
Inter1 inter=demo.retmethod(); }
}
|
·
If a method with an interface as return type
while writing body to that method, we have to take care of returning its
implementation class object.
·
While calling that method accept the return
type into reference variable.
·
We can have variables inside an interface by
default they will be treated as
public static final X=10; so
variables inside interface should be initialized.
·
Class name ambiguity will be removed by the
peckages.
·
Compiler or JVM will understand classes with
same name, if we will define them in different packages.
·
Class should not be declared as private. if
we declare a class as private we can’t create the object of that class in
outside of that class.
·
If we can’t create the object of a class its
waste to create a class or there no meaning of creation of a class.
Late binding:-
function body will be
associated with the function definition during runtime is known as “ Late Binding “.
Difference b/w Late Binding and Dynamic polymorphism:-
Late Binding:- method
body will be associated with method definition during runtime.
Dynamic polymorphism:- which method has to be executed will be decided during runtime,
but remember, method body will be associated with the definition during
compilation time.
Example for interface:-
Railway reservation form
Form
is same to every person, depends on the details entered in the form the reservations will be changed.
Note:- We should not use static keyword to
declared methods inside interface class.
interface MyInter {
public static final
int X =111;
int VAR = 10 ;
//static void
myMethod();
void openion();
void decission();
}
|
Methods
in object class:-
Hashcode:-
Hash code is a method
which is used to print the hash code an object.when we create an object or
instance of a class (memory for non-static contents),each object will be
assigned a value by the JVM is known as “hashcode” .
class A //extends Object {
public static void main(String[]
args) }
Explanation:-
Here class A is not
extending any other class. So during compilation compiler will extends object
class to A class as shown below.
class A extends
Object { }
|
If we create the object
to class A we can call all the methods of object classs, because A class is a
subclass to object class.
No comments:
Post a Comment