0% found this document useful (0 votes)
3 views38 pages

Java Unit 2 new

The document provides an overview of inheritance in Java, explaining its significance in object-oriented programming and detailing the types of inheritance, including single, multilevel, and hierarchical. It also covers concepts such as method overriding, the use of the super keyword, and the limitations of inheritance in Java, such as the prohibition of multiple inheritance. Additionally, it introduces abstract classes and methods, emphasizing their role in defining templates for subclasses.

Uploaded by

Manesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views38 pages

Java Unit 2 new

The document provides an overview of inheritance in Java, explaining its significance in object-oriented programming and detailing the types of inheritance, including single, multilevel, and hierarchical. It also covers concepts such as method overriding, the use of the super keyword, and the limitations of inheritance in Java, such as the prohibition of multiple inheritance. Additionally, it introduces abstract classes and methods, emphasizing their role in defining templates for subclasses.

Uploaded by

Manesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

MANESH PATEL

PRESIDENT INSTITUTE OF COMPUTER APPLICAION COLLEGE, SHAYONA CAMPUS, KADI

BCA SEM: 3 JAVA - Unit – 2 JAVA

Inheritance
 Inheritance means Reusability. It is very useful.
 The capability of a class to inherit the properties of some other class is
known as Inheritance.
 When we create a new class from the old class, it’s called Inheritance.
 The capability of a class to inherit properties from another class is called
Inheritance.
 It’s one of the important fundamentals of OOP principles.
 Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.

There are two types of classes in inheritance:

1. Parent Class or Base Class or Super class or old class


2. Child Class or Derived Class or sub class or new class
1. What is parent class?
The class that is being inherited by other class is known as parent class,
super class or base class.

2. What is child class?


A class that inherits another class is known as child class, it is also known
as derived class or subclass.

Advantages of Inheritance
o Reusability.
o It saves Memory Space.
o It saves Time.
o Increase Reliability of Code.
o Saves Developing and Testing efforts and Money too.
o Fast Process.

PREPARED BY: PATEL MANESH - M.Sc(CA & IT) Contact: 90165 17796 1
PATEL MANESH

The syntax of Java Inheritance


class Subclass-name extends Superclass-name
{
//Methods and fields
}

Example

class ABC extends XYZ // IS-A relationship


{
//Body part
}

 The extends keyword indicates that you are making a new class that
derives from an existing class.
 The meaning of "extends" is to increase the functionality.
Types of inheritance in java
 There can be three types of inheritance in java: single, multilevel and
hierarchical.
 JAVA does not support multiple and hybrid inheritances.
 Both are supported by interface only. We will learn about interfaces later.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 2
PATEL MANESH

Single Inheritance Example


 When a class inherits another class, it is known as a single inheritance.
 If there is only one super class and only one sub class in a program, it’s
called Single Inheritance.

class A
{
int no;
String name;

void get()
{
no = 10;
name = “Manish”;
}
}
class B extends A
{
void display()
{
System.out.println (no);
System.out.println (name);
}
}

class Demo
{
public static void main (String M[])
{
B b=new B();
b.get();
b.display(); 10
} Manish
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 3
PATEL MANESH

Multilevel Inheritance Example


 When there is a chain of inheritance, it is known as multilevel inheritance.
 The inheritance in which a class can be derived from another derived class
is known as Multilevel Inheritance.
class A class Demo
{ {
int no1; public static void main (String M[])
String name; {
C obj = new C();
void get()
obj.get();
{
no1 = 100; obj.put();
name = “Manish”;
obj.display();
}
} }
}
class B extends A
{
int no2;
void put()
{
no2 = 500;
}
}

class C extends B
{
void display()
{
System.out.println (no1);
System.out.println (no2);
System.out.println (name);
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 4
PATEL MANESH

Hierarchical Inheritance
 If there is only one super class and more than one child class in a program,
it’s called Hierarchical Inheritance.
 A single base class can have multiple derived classes, and other subclasses
can further inherit these derived classes, forming a hierarchy of classes.
class A class Demo
{ {
int no1,no2; public static void main (String M[])
{
void get() B obj1 = new B();
{
C obj2 = new C();
no1 = 200;
no2 = 200;
}
obj1.get();
}
obj1.add();
class B extends A
{
int sum; obj2.get();
void add() obj2.mul();
{
sum = no1 + no2; }
System.out.println (sum); }
}
}

class C extends A
{
int res;
void mul()
{
res = no1 * no2;
System.out.println (res);
}
}
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 5
PATEL MANESH

Why multiple inheritance is not supported in java?

 To reduce the complexity, multiple inheritance is not supported in java.


 Consider a scenario where A, B, and C are three classes.
 The C class inherits A and B classes.
 If A and B classes have the same method and you call it from child class
object, there will be complexity to call the method of A or B class.
 Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes.
 So whether you have same method or different, there will be compile time
error.
class ABC
{
void get()
{
System.out.println ("Hello");
}
}
class XYZ
{
void get()
{
System.out.println ("Welcome");
}
}
class C extends A,B //suppose if it were
{
public static void main(String M[])
{
C obj=new C();
obj.get(); //Which get() method would be called?
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 6
PATEL MANESH

Method Overriding in Java


 If subclass (child class) has the same method as declared in the parent class,
it is known as method overriding.
 Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding


1. The method must have the same name as in the super class.
2. The method must have the same parameter as in the super class.
3. The method must have the same data type as in the super class.
4. There must be an IS-A relationship (inheritance).
5. A final method cannot be overridden.
6. A static method cannot be overridden but can be re-declared.
7. Constructors cannot be overridden.
8. A subclass in a different package can only override the non-final methods
declared public or protected.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 7
PATEL MANESH

class Animal
{
public void move()
{
System.out.println ("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println ("Dogs can walk and run");
}
}
class Demo
{
public static void main (String M[])
{
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move(); // runs the method in Animal class


b.move(); // runs the method in Dog class
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 8
PATEL MANESH

Constructor calling during inheritance


Note: Constructor cannot be inherited in java

class Parent
{

Parent() // Parent() class constructor


{
System.out.println ("Parent Class constructor calling.");
}
}

class Child extends Parent


{

Child() // Child() class constructor


{
//super();
System.out.println ("Child Class constructor calling.");
}
}

class Order
{

public static void main (String M[])


{

Child C=new Child(); // Child() class constructor calling


}
}

Parent Class constructor calling.


Child Class constructor calling.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 9
PATEL MANESH

Super Keyword in Java


 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

1. super can be used to call parent class instance variable.


2. super can be used to call parent class method.
3. super() can be used to call parent class constructor.

1. super can be used to call parent class variable


 We can use super keyword to access the data member Super class.
 It is used if parent class and child class have same variable.
class Parent class Run
{ {
String color="White"; public static void main (String M[])
} {
1. Child C=new Child();
class Child extends Parent 2. C.display();
{
}
String color="Black";
}
void display()
{
System.out.println (color);
System.out.println (super.color);
Black

} White
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 10
PATEL MANESH

2. super can be used to call parent class method.


 It should be used if subclass contains the same method as parent class.
 In other words, it is used if method is overridden.
class Parent
{
void call()
{
System.out.println ("I am from call() from Parent class");
}
}
class Child extends Parent
{
void call()
{
System.out.println ("I am from call() from Child class ");
}
void sum()
{
System.out.println ("Normal Method of Child class");
}
void Display()
{
super.call();
sum();
}
}

class Test
{
public static void main(String M[])
{
Child C=new Child();
C.Display();
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 11
PATEL MANESH

3. super() can be used to call parent class constructor

class Parent
{

Parent() // Parent() class constructor


{
System.out.println ("Parent Class constructor calling.");
}
}

class Child extends Parent


{

Child() // Child() class constructor


{
super();
System.out.println ("Child Class constructor calling.");
}
}

class Order
{

public static void main (String M[])


{

Child C=new Child(); // Child() class constructor calling


}
}

Parent Class constructor calling.


Child Class constructor calling.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 12
PATEL MANESH

Method which cannot be override

 Inheritance has an important rule of any Object-Oriented Programming


(OOP) language but still, there are some methods in java to which we
cannot overriding in child classes which are as follows:
Methods:

1. static method
2. private access modifier
3. final class
4. final method
1. static method cannot overriding

class A class Demo5


{ {
static void get()
public static void main (String M[])
{
System.out.println ("Super class"); {
} B b=new B();
} b.get();

class B extends A }
{ }
void get()
{
System.out.println ("Sub class");
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 13
PATEL MANESH

2. final class cannot inherited

final class A
{
A()
{
System.out.println ("Super class");
}

}
class B extends A
{
B()
{
System.out.println ("Sub class");
}
}

class Demo5
{
public static void main (String M[])
{
B b=new B();
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 14
PATEL MANESH

3. final method cannot overriding

class A
{
final void display()
{
System.out.println ("Super class");
}
}

class B extends A
{
void display()
{
System.out.println ("Sub class");
}
}

class Demo5
{
public static void main (String M[])
{
B b=new B();

b.display();
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 15
PATEL MANESH

Abstract Class in Java


 A class which is declared as abstract is known as an abstract class.
 It can have abstract and non-abstract methods.
 It needs to be extended (inherited) and its method implemented.
 We cannot create an object of that interface.

Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.

Abstract Method in Java


 A method which is declared as abstract and does not have implementation is
known as an abstract method.

abstract void show(); //no method body and abstract

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 16
PATEL MANESH

Example of Abstract class that has an abstract method

abstract class Demo //abstract class


{
abstract void display(); //abstract method declaration

class MyClass extends Demo


{

void display() //method implementation

{
System.out.println ("Abstract method");
}

public static void main(String args[])


{
//creating reference object of abstract class

Demo obj = new MyClass();

obj.display(); // Calling abstract method

}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 17
PATEL MANESH

Understanding the real scenario of Abstract class


 In this example, Shape is the abstract class, and its implementation is provided
by the Rectangle and Circle classes.

abstract class Shape


{
abstract void show();
}

class Rectangle extends Shape


{
void show()
{
System.out.println ("I am rectangle");
}
}

class Circle extends Shape


{
void show()
{
System.out.println ("I am circle");
}
}

class Test
{
public static void main (String M[])
{
Shape s=new Circle(); //Reference
s.show();

Shape s1=new Rectangle(); //Reference


s1.show();
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 18
PATEL MANESH

Interface in Java

 An interface in Java is a blueprint of a class.


 It has static constants and abstract methods.
 There can be only abstract methods in the Java interface, not method body.
 It is used to achieve abstraction and multiple inheritance in Java.
 In other words, you can say that interfaces can have abstract methods and
variables.
 It cannot have a method body.
 Java Interface also represents the IS-A relationship.
 We cannot create an object of Interface just like the abstract class.
 Since Java 8, we can have default and static methods in an interface.
 Since Java 9, we can have private methods in an interface.

Why use Java interface?


 There are mainly three reasons to use interface.

1. It is used to achieve abstraction.

2. By interface, we can support the functionality of multiple inheritance.

3. It can be used to achieve loose coupling. (Loose coupling in Java means


that the classes are independent of each other.)

How to declare an interface?


 An interface is declared by using the interface keyword.
 It provides total abstraction; means all the methods in an interface are
declared with the empty body, and all the fields are public, static and final
by default.
 A class that implements an interface must implement all the methods
declared in the interface.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 19
PATEL MANESH

Syntax:

interface <interface_name> interface Demo


{ {

// declare constant fields //Body of interface


// declare methods that abstract
// by default. }
}

The relationship between classes and interfaces


 As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 20
PATEL MANESH

Java Interface Example


 In this example, the Demo interface has only one method,
 And its implementation is provided in the Manu class.

interface Demo
{
void print();
}

class Manu implements Demo


{
public void print() // must be public
{
System.out.println ("Hello");
}

public static void main (String args[])


{
Manu M = new Manu ();
M.print();
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 21
PATEL MANESH

Multiple inheritance in Java by interface


 If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.

interface ABC
{
void print();
}
interface XYZ
{
void show();
}
class Manish implements ABC, XYZ
{
public void print()
{
System.out.println ("Hello");
}
public void show()
{
System.out.println ("Welcome");
}
public static void main (String M[])
{
Manish m = new Manish();
m.print();
m.show();
Hello
} Welcome
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 22
PATEL MANESH

Interface inheritance
interface ABC
{
void print();
}

interface XYZ extends ABC


{
void show();
}

class Manish implements XYZ


{
public void print()
{
System.out.println ("Hello");
}
public void show()
{
System.out.println ("Welcome");
}

public static void main(String args[])


{
Manish obj = new Manish();
obj.print();
obj.show();
}
}
Hello
Welcome

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 23
PATEL MANESH

Create Multiple inheritance by Interface and class

interface ABC // interface


{
void print();
}

class XYZ // class


{
void show()
{
System.out.println ("I am from class");
}

class Manish extends XYZ implements ABC


{
public void print()
{
System.out.println ("I am from Interface");
}

public static void main(String args[])


{
Manish obj = new Manish();
obj.print(); // method of interface
obj.show(); // method of class
}
}

I am from Interface
I am from class

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 24
PATEL MANESH

Q) What is marker or tagged interface?


 An interface which has no member is known as a marker or tagged
interface,
 for example, Serializable, Cloneable, Remote, etc.
 They are used to provide some essential information to the JVM so that
JVM may perform some useful operation.

public interface Serializable { }

Difference between abstract class and interface

 Abstract class and interface both are used to achieve abstraction where we

can declare the abstract methods.

 We cannot create an object of both Abstract class and interface.

 But there are many differences between abstract class and interface that are

given below.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 25
PATEL MANESH

Abstract class Interface


1) Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods.

2) Abstract class doesn't support Interface supports multiple


multiple inheritance. inheritance.
3) Abstract class can have final, non- Interface has only static and
final, static and non-static variables. final variables.

4) Abstract class can provide the Interface can't provide the


implementation of interface. implementation of abstract
class.
5) The abstract keyword is used to The interface keyword is used to
declare abstract class. declare interface.
6) An abstract class can extend another An interface can extend another
Java class and implement multiple Java Java interface only.
interfaces.
7) An abstract class can be extended An interface can be implemented
using keyword "extends". using keyword "implements".
8) A Java abstract class can have class Members of a Java interface are
members like private, protected, etc. public by default.

9)Example: Example:
abstract class Demo interface Demo
{ {
public abstract void draw(); void paint();
} }

 Simply, abstract class achieves partial abstraction (0 to 100%) whereas

interface achieves fully abstraction (100%).

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 26
PATEL MANESH

Package
 A java package is a group of similar types of classes, interfaces and sub-
packages.
 Package in java can be categorized in two form, built-in package and user-
defined package.
 There are many built-in packages such as java, lang, awt, javax, swing, net,
io, util, sql etc.
 Here, we will have the detailed learning of creating and using user-defined
packages.
 Packages are used in Java, in-order to avoid name conflicts and to control
access of class, interface and enumeration etc.
 Using package it becomes easier to locate the related classes.
 It also provides a good structure for projects with hundreds of classes and
other files.

Types of Packages: Built-in and User defined


 Built-in Package: Existing Java package for example java.lang,
java.util etc.
 User-defined-package: Java package created by user to categorize their
project's classes and interface.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 27
PATEL MANESH

Advantage of Java Package


 Java package is used to categorize the classes and interfaces so that they
can be easily maintained.
 Java package provides access protection.
 Java package removes naming collision

Creating a package
 Creating a package in java is quite easy.
 Simply include a package command followed by name of the package as
the first statement in java source file.

package Manesh; //must be first statement


class Demo
{
//statement;
}

Additional points about package:


 A package is always defined as a separate folder having the same name as
the package name.
 Store all the classes in that package folder.
 All classes of the package which we wish to access outside the package
must be declared public.
 All classes within the package must have the package statement as its first
line.
 All classes of the package must be compiled before use (So that they are
error free)

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 28
PATEL MANESH

Example of Java packages


//save as Demo.java

package Manesh;
class Demo
{
public static void main(String M[])
{
System.out.println ("Welcome to package");
}
}

How to compile Java programs inside packages?


 This is just like compiling a normal java program.
 If you are not using any IDE, you need to follow the steps given below to
successfully compile your packages:

javac -d directory javafilename


Example:
javac -d . Demo.java
 The -d specifies the destination where to put the generated class file.
 You can use any directory name like d:/abc (in case of windows) etc.
 If you want to keep the package within the same directory, you can use .
(dot)

How to run Java package program?

To Compile:
javac -d . Demo.java OR javac -d d:/Jay Demo.java

To Run:

java Manesh.Demo Output: Welcome to package


s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 29
PATEL MANESH

import keyword
 import keyword is used to import built-in and user-defined packages into
your java source file
 So that your class can refer to a class that is in another package by directly
using its name.

There are 3 different ways to refer to any class that is present in a different
package:

1. Using fully qualified name (But this is not a good practice.)

 If you use fully qualified name to import any class into your program,
then only that particular class of the package will be accessible in
your program, other classes in the same package will not be
accessible.
 For this approach, there is no need to use the import statement.
 But you will have to use the fully qualified name every time you are
accessing the class or the interface, which can look a little untidy if
the package name is long.
 This is generally used when two packages have classes with same
names.
 For example: java.util and java.sql packages contain Date
class.
package sem1; package sem2;
public class A class B
{ {
public void msg() public static void main(String M[])
{ {
System.out.println ("Hello"); //using fully qualified name
} sem1.A obj = new sem1.A();
} obj.msg();
//save by A.java }
} //save by B.java

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 30
PATEL MANESH

2. To import only the class/classes you want to use


 If you import packagename.classname then only the class with
name classname in the package with name packagename will be
available for use.

package sem1; package sem2;


public class A import sem1.A;
{ class B
public void msg() {
{ public static void main (String M[])
System.out.println ("Hello"); {
} A obj = new A();
} obj.msg();
//save by A.java }
} //save by B.java

3. To import all the classes from a particular package


 If you use packagename.*; then all the classes and interfaces of this
package will be accessible but the classes and interface inside the
subpackages will not be available for use.
 The import keyword is used to make the classes and interface of another
package accessible to the current package.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 31
PATEL MANESH

package sem1; package sem2;


public class A import sem1.*;
{ class B
public void msg() {
{ public static void main (String M[])
System.out.println ("Hello"); {
} A obj = new A();
} obj.msg();
//save by A.java }
} //save by B.java

Point to remember
 While creating a package, care should be taken that the statement for
creating package must be written before any other import statements.

// not allowed
import package sem2.*;
import java.io.*;
package sem4;

Below code is correct, while the code mentioned above is incorrect.

//correct syntax
package sem4;
import package p1.*;

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 32
PATEL MANESH

Java.lang package
 java.lang.*; Provides classes that are fundamental to the design of the Java
programming language.
 The most important classes are Object, which is the root of the class
hierarchy, and Class, instances of which represent classes at run time.

Following are the Important Classes in Java.lang package:

1. Boolean: The Boolean class wraps a value of the primitive type


boolean in an object.
2. Byte: The Byte class wraps a value of primitive type byte in an object.
3. ClassLoader: A class loader is an object that is responsible for loading
classes.
4. Compiler: The Compiler class is provided to support Java-to-native-code
compilers and related services.
5. Double: The Double class wraps a value of the primitive type double in an
object.
6. Float: The Float class wraps a value of primitive type float in an object.
7. Integer :The Integer class wraps a value of the primitive type int in an
object.
8. Long: The Long class wraps a value of the primitive type long in an
object.
9. Math –: The class Math contains methods for performing basic numeric
operations such as the elementary exponential, logarithm,
square root, and trigonometric functions.
10. Number: The abstract class Number is the superclass. of classes
Byte, Double, Float, Integer, Long, and Short.
11. Object: Class Object is the root of the class hierarchy.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 33
PATEL MANESH

12. Package: Package objects contain version information about the


implementation and specification of a Java package.
13. ProcessBuilder: This class is used to create operating system
processes.
14. RuntimePermission: This class is for runtime permissions.
15. SecurityManager: The security manager is a class that allows
applications to implement a security policy.
16. Short: The Short class wraps a value of primitive type short in an
object.
17. String-: The String class represents character strings.
18. StringBuffer: A thread-safe, mutable sequence of characters.
19. System: The System class contains several useful class fields and
methods.
20. Thread: A thread is a thread of execution in a program.
21. ThreadGroup: A thread group represents a set of threads.
22. Throwable: The Throwable class is the superclass of all errors and
exceptions in the Java language.
23. Void:

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 34
PATEL MANESH

Object class in Java


 The Object class is the parent class of all the classes in java by default.
 In other words, it is the topmost class of java.
 The Object class is beneficial if you want to refer any object whose type
you don't know.

 The Object class provides some common methods to all the objects such as
object can be compared, object can be cloned, object can be notified etc.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 35
PATEL MANESH

1. GetClass() method

 It returns the class object of “this” object.


 It is used to get the actual runtime class of the object.
 It can also be used to get metadata of this class.
 As it is final method so we don’t override it.

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 36
PATEL MANESH

public class Test


{
public static void main(String[] args)
{
Object M = new String ("Shayona Campus");
Class c = M.getClass();
System.out.println("Class of Object obj is : " + c.getName());
}
}

hashCode() and finalize()

public class Jay {


public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.hashCode());

t = null;

// calling garbage collector


System.gc();

System.out.println("end");
}

protected void finalize()


{
System.out.println("finalize method called");
}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 37
PATEL MANESH

class Demo implements Cloneable


{
int rollno;
String name;

Demo ()
{
rollno=10;
name="manish";
}

public Object clone() throws CloneNotSupportedException


{
return super.clone();
}

public static void main(String args[])


{
try
{
Demo s1=new Demo ();

Demo s2=( Demo)s1.clone();

System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);

}
catch(CloneNotSupportedException c){}

}
}

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 38

You might also like