Java - Unit - 2
Java - Unit - 2
Java - Unit - 2
Example:
class Testarray
{
public static void main (String args [])
{
int [] = new int [5];
a [0] = 10;
a [1] = 20;
a [2] = 30;
a [3] = 40;
a [4] = 50;
for(int i=0; i<a.length;i++);
{
System.out.println(a[i]);
}
}
}
Output:
10
20
30
40
50
String class uses string objects for storing the String buffer class creates an object and stores
values value in it
These values are immutable (i.e.,) these string These values can be changed at any point of time.
object values cannot be changed once declared Hence the String Buffer class object are mutable.
Wrapper Class:
Wrapper Class: As java is purely OOP language, every entity should be measured in
terms of object. Wrapper class is a mechanism to convert primitive datatype in objects
and objects into primitive datatype.
We have wrapper class in java.lang.
Primitive datatypes Wrapper class
byte Byte
short Short
int Int
long Long
float Float
double Double
char Char
boolean Boolean
Java allows automatic conversion between the primitives and their respective wrapper
objects through auto-boxing and unboxing.
Auto Boxing
In Java, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
In the example below, the Car class (subclass) inherits the attributes and methods from
the Vehicle class (superclass):
Syntax:
______________
______________
_______________
Class Parent1
Void fun()
{
System.out.println(“parent1”);
Class Parent2
Void fun()
System.out.println(“parent2”);
In the above program class Text inherits the properties of parent1 and parent2.
A class which contains the abstract keyword in its declaration is called Abstract class.
Syntax:
Abstract class A
------
------------
}
In java, the following some important observations about abstract classes are as follows:
1. We cannot create object for abstract class, but we can create reference variable for
abstract class.
2. Constructors are allowed.
3. We can have an abstract class without any abstract method.
4. An abstract method has no body.
5. We can use the abstract keyword for declaring top-level classes (Outer class) as well as
inner classes as abstract
6. If a class contains at least one abstract method then compulsory should declare a class as
abstract .
abstract class A
{
abstract void display ( );
Class B extends A
{
Void display ( )
{
System.out.println(“class A”)
}
}
Class abstract
public static void main(tring args[ ])
{
B obj =new B ( );
Obj.display( );
}
}
o/p – class A
METHOD OVERRIDING:
Note :
1. In C++, we need virtual keyword to achieve overriding or Run Time Polymorphism. In Java,
methods are virtual by default.
program on overriding
class Parent {
void display()
{
System.out.println(“Parent Method”);
}
}
class child extends Parent
{
void display()
{
super.display();
System.out.println(“child method”);
}
}
class override
{
public static void main(String args[])
{
child obj=new child();
obj.display();
}
}
Output:
Child method.
Explaination of above program:
1.Whatever methods are there in parent class it can be by default get to child by inheritance
method.
2.If the child is not satisfied with the methods implementation available in parent class,it may
allow to re-define according to its requirement which is called overriding(child class) and
parent class method is overridden method.
3.If we create obj for child class then only child method will be displayed.
4. If we create obj for parent class then parent method will be displayed.
5.super keyword is used to access the method of parent class in child class if both are sharing
same method name.
6.This is called runtime polymorphism.
7.This is called dynamic binding.
8.Or else instead of using super keyword we can create obj for both parent and child class
like
child obj=new child();
obj.display();
Parent obj1=new Parent();
Obj1.display();
}
Interfaces in Java
1. Interface is just like a class which contain only abstract method (so we cannot create obj for it since it
has abstract method).
// PROGRAM ON INTERFACE
Interface A
Void Adisplay ( );//abstract method…we need not write abstract keyword for interface,since interface
contains abstract methods
Void Ashow ( );//these are abstract methods since it has no definition(body) only declaration.
Interface B
Void Bdisplay ( );
Void Bshow ( );
Class AB implements A,B //Advantage of interface is we can inherit from two base clases but it is not
possible to inherit two base classes in inheritance
System.out.println(“A display”);
}
Public void Bdisplay ( )
Class interface AB
Obj.Adisplay ( );
Obj.Ashow( );
Obj.Bdisplay( );
Obj.Bshow( );
o/p A display
A show
B display
B show
Syntax:
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
Implementation: To implement an interface we use the keyword implements
Comparable and comparator
Comparable comparator
1.It is meant for default natural 1.meant for customised sorting order
Sorting order
2.It is present in java.lang package 2.It is present in java.util package
Inner class:
It is a class which is defined within another class or interface are known as inner class or nested class.
//syntax
class outer
_____________;
_____________;
class Inner
_______________;
_______________;
The inner class declared without static keyword is known as Non-static inner class.
Example program:
package pack1;
class InnerExample//This inner classcan access all members of outer classes like int,private,static.
System.out.println("Sum is "+(a+b+C));
}
}
//Outer object (To create inner class object first we need outer class object)
//Inner object
Ob.doSum();
Class Outer
Void main()
class Inner
Example
If you compile and execute the above program, you will get the following result.
If you compile and execute the above program, it gives you the following result.
2) To access inner class members we have to create object for Inner class.
class Outer
void display()
System.out.println("static class");
obj.display();