CH 2
CH 2
It returns the class object of “this” object and is used to get the
actual runtime class of the object. It can also be used to get
metadata of this class. The returned Class object is the object that
is locked by static synchronized methods of the represented class.
As it is final so we don’t override it.
public class Test {
public static void main(String[] args)
{
Object obj = new String("GeeksForGeeks");
Class c = obj.getClass();
System.out.println("Class of Object obj is : " + c.getName());
}}
4. equals(Object obj) method
It compares the given object to “this” object (the object on which
the method is called). It gives a generic way to compare objects
for equality. It is recommended to override the equals(Object
obj) method to get our own equality condition on Objects.
Syntax:
public boolean equals(Object obj)
5. finalize() Method
In Java, garbage means unreferenced object.
There are 3 ways to unreferenced object:
1. by nulling the reference( c=null;)
2.by assigning a reference to another (c1=c2;)
3.by annonyms object (new crickter();)
Syntax:
protected void finalize()
6.clone() method
The clone() method saves the extra processing task for creating
the exact copy of an object. If we perform it by using the new
keyword, it will take a lot of processing time to be performed that
is why we use object cloning.
Advantage:
It is the easiest and most efficient way for copying objects,
especially if we are applying it to an already developed or an old
project. Just define a parent class, implement Cloneable in it,
provide the definition of the clone() method and the task will be
done.
Clone() is the fastest way to copy array.
Disadvantage
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Usage:
Method overriding is used to provide the specific implementation
of a method which is already provided by its superclass.
Method overriding is used for runtime polymorphism.
Syntax:
interface <interface_name>
{
// declare constant fields
//declare methods that abstract
// by default.
}
The Java compiler adds public
and abstract keywords before
the interface method.
Moreover, it adds public, static
and final keywords before data
members.
interface Drawable System.out.println("drawing
//Interface declaration: by first user circle");
{ } }
void draw(); class A6
} //Using interface: by third user
class Rectangle implements Drawable {
//Implementation: by second user public static void main(String args[])
{ {
public void draw() //A6 d=new A6();
{ //In real scenario, object is
System.out.println("drawing provided by method getDrawable()
rectangle"); //Drawable d=new Circle();
} } Circle d= new Circle();
class Circle implements Drawable d.draw();
{ } }
public void draw()
{
Relationship Between Class and Interface
interpreter compiler
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package
will be accessible but not subpackages.
The import keyword is used to make the classes and interface of
another package accessible to the current package.
2) Using packagename.classname
To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple
Another way to run this program by -classpath switch of java:
e:\sources> java -classpath c:\classes mypack.Simple
Subpackage in java
Package inside the package is called the subpackage. It should be
created to categorize the package further.
Let's take an example, Sun Microsystem has definded a package
named java that contains many classes like System, String, Reader,
Writer, Socket etc.
These classes represent a particular group e.g. Reader and Writer
classes are for Input/Output operation, Socket and ServerSocket
classes are for networking etc and so on. So, Sun has
subcategorized the java package into subpackages such as lang,
net, io etc. and put the Input/Output related classes in io
package, Server and ServerSocket classes in net packages and so
on.
Example of Subpackage
package com.javatpoint.core;
class Simple
{
public static void main(String args[]){
System.out.println("Hello subpackage");
}
}
Java inner class or nested class is a class that is declared inside the
class or interface.
We use inner classes to logically group classes and interfaces in
one place to be more readable and maintainable.
Advantage of Java inner classes
1. Nested classes represent a particular type of relationship that
is it can access all the members (data members and methods) of
the outer class, including private.
2. Nested classes are used to develop more readable and
maintainable code because it logically group classes and
interfaces in one place only.
3. Code Optimization: It requires less code to write.
Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}