Java Unit II
Java Unit II
Output:
Your first argument is: sonoo
The this keyword refers to the current object in a
method or constructor.
The most common use of the this keyword is to
eliminate the confusion between class attributes and
parameters with the same name
There can be a lot of usage of Java this keyword. In
Java, this is a reference variable that refers to the
current object.
The final keyword is a non-access modifier used for
classes, attributes and methods, which makes them
non-changeable (impossible to inherit or override).
The final keyword is useful when you want a variable
to always store the same value, like PI (3.14159...).
The final keyword is called a "modifier".
The static keyword in Java is used for memory
management mainly.
We can apply static keyword with variables, methods,
blocks and nested classes.
If you declare any variable as static, it is known as a
static variable.
The static variable can be used to refer to the
common property of all objects (which is not unique
for each object), for example, the company name of
employees, college name of students, etc.
The static variable gets memory only once in the
class area at the time of class loading.
A class which contains the abstract keyword in its
declaration is known as an abstract class.
An abstract class is a class that is declared abstract
means it may or may not include abstract methods.
Abstract classes may not be marked as private or final.
abstract class syntax
barking...
eating...
When there is a chain of inheritance, it is
known as multilevel inheritance.
As you can see in the example given below,
BabyDog class inherits the Dog class which
again inherits the Animal class, so there is a
multilevel inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
Dynamic method dispatch is a mechanism by which a
call to an overridden method is resolved at runtime.
This is how java implements runtime polymorphism.
When an overridden method is called by a reference,
java determines which version of that method to
execute based on the type of object it refer to.
Upcasting in Java
When Parent class reference variable refers
to Child class object, it is known as Upcasting.
In Java this can be done and is helpful in scenarios
where multiple child classes extends one parent class.
In those cases we can create a parent class reference
and assign child class objects to it.
The super keyword in Java is a reference variable
which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an
instance of parent class is created implicitly which is
referred by super reference variable.
Usage of Java super Keyword
super can be used to refer immediate parent class
instance variable.
super can be used to invoke immediate parent class
method.
super() can be used to invoke immediate parent class
constructor.
black
white
An interface is a reference type in Java. It is
similar to class. It is a collection of abstract
methods.
A class implements an interface, thereby
inheriting the abstract methods of the
interface.
The interface keyword is used to declare an
interface.
Here is a simple example to declare an interface −
Example
/* File name : Animal.java */
interface Animal
{
public void eat();
public void travel();
}
A class uses the implements keyword to
implement an interface.
The implements keyword appears in the class
declaration following the extends portion of
the declaration.
/* File name : MammalInt.java */
public class MammalInt implements Animal
{
public void eat()
{ System.out.println("Mammal eats"); }
public void travel()
{ System.out.println("Mammal travels"); }
public int noOfLegs()
{ return 0; }
public static void main(String args[])
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Mammal eats
Mammal travels
When implementation interfaces, there are several rules −
A class can implement more than one interface at a
time.
A class can extend only one class, but implement many
interfaces.
An interface can extend another interface, in a similar
way as a class can extend another class.
In java, an interface can extend another interface. When
an interface wants to extend another interface, it uses
the keyword extends.
The interface that extends another interface has its own
members and all the members defined in its parent
interface too.
The class which implements a child interface needs to
provide code for the methods defined in both child and
parent interfaces
interface ParentInterface
{ void parentMethod(); }
class B
{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:
Hello
If you import package.classname then only declared
class of this package will be accessible.
//save by A.java
package pack;
public class A
{
public void msg()
{System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output:
Hello
If you use fully qualified name then only
declared class of this package will be
accessible.
Now there is no need to import.
But you need to use fully qualified name every
time when you are accessing the class or
interface.
//save by A.java
package pack;
public class A
{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B
{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Output:
Hello
Note: If you import a package, subpackages will not
be imported.
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
The automatic conversion of primitive data
type into its corresponding wrapper class is
known as autoboxing,
for example, byte to Byte, char to Character,
int to Integer, long to Long, float to Float,
boolean to Boolean, double to Double, and
short to Short.
//Java program to convert primitive into objects
//Autoboxing example of int to Integer
public class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer explicitl
Integer j=a;//autoboxing, now compiler will write Integer.valueO
internally
System.out.println(a+" "+i+" "+j);
}
}
Output
20 20 20
The automatic conversion of wrapper
type into its corresponding primitive type
is known as unboxing.
It is the reverse process of autoboxing.
//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue()
internally
System.out.println(a+" "+i+" "+j);
}
}
Output
333