SlideShare a Scribd company logo
Objects and classes in OO Programming concepts
Object Class and its methods
• Object class is present in java.lang package. Every
class in Java is directly or indirectly derived from the
Object class.
• If a class does not extend any other class then it is a
direct child class of Object and if extends another
class then it is indirectly derived.
• Therefore the Object class methods are available to
all Java classes. Hence Object class acts as a root of
the inheritance hierarchy in any Java Program.
Objects and classes in OO Programming concepts
Using Object Class Methods
The Object class provides multiple methods which are as follows:
• tostring() method
• hashCode() method
• equals(Object obj) method
• finalize() method
• getClass() method
• clone() method
• wait(), notify() notifyAll() methods
Method Description
public final Class getClass() returns the Class class object of
this object. The Class class can
further be used to get the
metadata of this class.
public int hashCode() returns the hashcode number
for this object.
public boolean equals(Object
obj)
compares the given object to
this object.
protected Object clone() throws
CloneNotSupportedException
creates and returns the exact
copy (clone) of this object.
public String toString() returns the string representation
of this object.
public final void notify() wakes up single thread, waiting
on this object's monitor.
public final void notifyAll() wakes up all the threads, waiting
on this object's monitor.
public final void wait(long
timeout)throws
InterruptedException
causes the current thread to wait
for the specified milliseconds,
until another thread notifies
(invokes notify() or notifyAll()
method).
public final void wait(long
timeout,int nanos)throws
InterruptedException
causes the current thread to wait
for the specified milliseconds
and nanoseconds, until another
thread notifies (invokes notify()
or notifyAll() method).
public final void wait()throws
InterruptedException
causes the current thread to
wait, until another thread
notifies (invokes notify() or
notifyAll() method).
protected void finalize()throws
Throwable
is invoked by the garbage
collector before object is being
garbage collected.
Interfaces: Interfaces vs. Abstract classes
Abstract class Interface
1) Abstract class can have
abstract and non- abstract
methods.
Interface can have only abstract
methods. Since Java 8, it can have
default and static methods also.
2) Abstract class doesn't
support multiple
inheritance.
Interface supports multiple
inheritance.
3) Abstract class can have final,
non-final, static and non-static
variables.
Interface has only static and final
variables.
4) Abstract class can
provide the
implementation of interface.
Interface can't provide the
implementationof abstract class.
5) The abstract keyword is used
to declare abstract class.
The interface keyword is used to
declare interface.
6) An abstract class can extend
another Java class and
implement multiple Java
interfaces.
An interface can extend another
Java interface only.
7) An abstract class can be
extended using keyword
"extends".
An interface can be implemented
using keyword "implements".
8) A Java abstract class can
have class members like
private, protected, etc.
Members of a Java interface are
public by default.
9)Example:
public abstract class
Shape{ public abstract void
draw();
}
Example:
public interface Drawable{
void draw();
}
Defining and implementing interface
• An interface in Java is a blueprint of a class. It has static
constants and abstract methods.
• The interface in Java is a mechanism to achieve abstraction.
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. It cannot
be instantiated just like the abstract class.
• 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.
• Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only
method signature, no body).
• Interfaces specify what a class must do and not how. It is the
blueprint of the behaviour.
• Interface do not have constructor.
• Represent behaviour as interface unless every sub-type of the class
is guarantee to have that behaviour.
• An Interface is about capabilities like a Player may be an interface
and any class implementing Player must be able to (or must
implement) move(). So it specifies a set of methods that the class
has to implement.
• If a class implements an interface and does not provide method
bodies for all functions specified in the interface, then the class
must be declared abstract.
• A Java library example is Comparator Interface. If a class
implements this interface, then it can be used to sort a collection.
interface Player
{
final int id = 10;
int move();
}
Implementation: To implement an interface
we use the keyword implements
import java.io.*;
interface In1
{
final int a = 10;
void display();
}
class TestClass implements In1 {
public void display(){
System.out.println("Hai");
}
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Output:
Hai
10
interface MyInterface
{
void callback(int param);
}
class Client implements MyInterface
{
public void callback(int p)
{
System.out.println("callback called with " + p);
}
}
public class Main {
public static void main(String args[]) {
MyInterface c = new Client();
c.callback(42);
}
}
output:
callback called with 42
Extending interfaces
• An interface contains variables and methods
like a class but the methods in an interface are
abstract by default unlike a class.
• An interface extends another interface like a
class implements an interface in interface
inheritance.
interface A {
void funcA();
}
interface B extends A
{
void funcB();
}
class C implements B
{
public void funcA()
{
System.out.println("This is funcA");
}
public void funcB()
{
System.out.println("This is funcB");
}
}
public class Demo {
public static void main(String
args[])
{
C obj = new C();
obj.funcA();
obj.funcB();
}
}
Output
This is funcA
This is funcB
Packages
• 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.
Advantage of Java Package
1) Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Objects and classes in OO Programming concepts
The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
compile java package
• javac -d directory javafilename
For example
• javac -d . Simple.java
• The -d switch specifies the destination where to put
the generated class file. You can use any directory
name like /home (in case of Linux), d:/abc (in case
of windows) etc. If you want to keep the package
within the same directory, you can use . (dot).
run java package program
To Compile:
• javac -d . Simple.java
To Run:
• java mypack.Simple
• Output:Welcome to package
• The -d is a switch that tells the compiler where to
put the class file i.e. it represents destination. The .
represents the current folder.
access package
There are three ways to access the package from outside the
package.
• import package.*;
• import package.classname;
• fully qualified name.
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.
Example of package that import the packagename.*
//save by A.java
package pack;
public class A
{
public void msg(){System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output:Hello
Using packagename.classname
//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
3) Using fully qualified name
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.
It is generally used when two packages have same
class name e.g. java.util and java.sql packages
contain Date class.
Importing a package
• To import the java package into a class, we need to use
the java import keyword which is used to access the
package and its classes into the java program.
• Use import to access built-in and user-defined packages
into your java source file to refer to a class in another
package by directly using its name.
syntax:
• import package.name.ClassName; // To import a certain
class only
• import package.name.* // To import the whole package
import java.util.Date;
// imports only Date class
import java.io.*;
// imports everything inside java.io
package
Example for creating and importing
java packages:
//save by Greet.java
package greeting; // creating package
public class Greet{
public void msg() {
System.out.println("Hello");
}
}
//save by Main.java
package Java;
import greeting.*;
//importing package
class Main {
public static void
main(String args[]) {
Greet g = new Greet();
g.msg();
}
}
• /*OUTPUT:
• Hello */

More Related Content

PPTX
Unit II Inheritance ,Interface and Packages.pptx
PPTX
OBJECT ORIENTED PROGRAMMING Unit2 second half.pptx
PDF
Java/J2EE interview Qestions
PPTX
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
PDF
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
PPTX
Packages and interfaces
PPTX
The smartpath information systems java
PPTX
Chap1 packages
Unit II Inheritance ,Interface and Packages.pptx
OBJECT ORIENTED PROGRAMMING Unit2 second half.pptx
Java/J2EE interview Qestions
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
Packages and interfaces
The smartpath information systems java
Chap1 packages

Similar to Objects and classes in OO Programming concepts (20)

PDF
Unit 2 notes.pdf
PDF
Exception handling and packages.pdf
PPTX
Lecture 18
PDF
Java - Interfaces & Packages
PDF
Core Java Introduction | Basics
PPT
web program-Inheritance,pack&except in Java.ppt
PPTX
java interface and packages
PPT
oops with java modules i & ii.ppt
PDF
Lecture 5 interface.pdf
PPTX
Java For beginners and CSIT and IT students
DOC
116824015 java-j2 ee
PPTX
Interfaces-in-Java.for engineering students pptx
DOCX
Core java notes with examples
ODP
Synapseindia reviews.odp.
PPT
Md02 - Getting Started part-2
PPTX
Java chapter 5
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPTX
Java interface
PPTX
OOFeatures_revised-2.pptx
PPT
025466482929 -OOP with Java Development Kit.ppt
Unit 2 notes.pdf
Exception handling and packages.pdf
Lecture 18
Java - Interfaces & Packages
Core Java Introduction | Basics
web program-Inheritance,pack&except in Java.ppt
java interface and packages
oops with java modules i & ii.ppt
Lecture 5 interface.pdf
Java For beginners and CSIT and IT students
116824015 java-j2 ee
Interfaces-in-Java.for engineering students pptx
Core java notes with examples
Synapseindia reviews.odp.
Md02 - Getting Started part-2
Java chapter 5
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Java interface
OOFeatures_revised-2.pptx
025466482929 -OOP with Java Development Kit.ppt
Ad

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Structure & Organelles in detailed.
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Computing-Curriculum for Schools in Ghana
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPH.pptx obstetrics and gynecology in nursing
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
Sports Quiz easy sports quiz sports quiz
Cell Structure & Organelles in detailed.
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
Computing-Curriculum for Schools in Ghana
Ad

Objects and classes in OO Programming concepts

  • 2. Object Class and its methods • Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. • If a class does not extend any other class then it is a direct child class of Object and if extends another class then it is indirectly derived. • Therefore the Object class methods are available to all Java classes. Hence Object class acts as a root of the inheritance hierarchy in any Java Program.
  • 4. Using Object Class Methods The Object class provides multiple methods which are as follows: • tostring() method • hashCode() method • equals(Object obj) method • finalize() method • getClass() method • clone() method • wait(), notify() notifyAll() methods
  • 5. Method Description public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of this class. public int hashCode() returns the hashcode number for this object. public boolean equals(Object obj) compares the given object to this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public String toString() returns the string representation of this object.
  • 6. public final void notify() wakes up single thread, waiting on this object's monitor. public final void notifyAll() wakes up all the threads, waiting on this object's monitor. public final void wait(long timeout)throws InterruptedException causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method). public final void wait(long timeout,int nanos)throws InterruptedException causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method).
  • 7. public final void wait()throws InterruptedException causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). protected void finalize()throws Throwable is invoked by the garbage collector before object is being garbage collected.
  • 8. Interfaces: Interfaces vs. Abstract classes Abstract class Interface 1) Abstract class can have abstract and non- abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can provide the implementation of interface. Interface can't provide the implementationof abstract class. 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
  • 9. 6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only. 7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements". 8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default. 9)Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); }
  • 10. Defining and implementing interface • An interface in Java is a blueprint of a class. It has static constants and abstract methods. • The interface in Java is a mechanism to achieve abstraction. 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. It cannot be instantiated just like the abstract class.
  • 11. • 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.
  • 12. • Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). • Interfaces specify what a class must do and not how. It is the blueprint of the behaviour. • Interface do not have constructor. • Represent behaviour as interface unless every sub-type of the class is guarantee to have that behaviour. • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement. • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract. • A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.
  • 13. interface Player { final int id = 10; int move(); } Implementation: To implement an interface we use the keyword implements import java.io.*; interface In1 { final int a = 10; void display(); } class TestClass implements In1 { public void display(){ System.out.println("Hai"); } public static void main(String[] args) { TestClass t = new TestClass(); t.display(); System.out.println(a); } } Output: Hai 10
  • 14. interface MyInterface { void callback(int param); } class Client implements MyInterface { public void callback(int p) { System.out.println("callback called with " + p); } } public class Main { public static void main(String args[]) { MyInterface c = new Client(); c.callback(42); } } output: callback called with 42
  • 15. Extending interfaces • An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. • An interface extends another interface like a class implements an interface in interface inheritance.
  • 16. interface A { void funcA(); } interface B extends A { void funcB(); } class C implements B { public void funcA() { System.out.println("This is funcA"); } public void funcB() { System.out.println("This is funcB"); } } public class Demo { public static void main(String args[]) { C obj = new C(); obj.funcA(); obj.funcB(); } } Output This is funcA This is funcB
  • 17. Packages • 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. Advantage of Java Package 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision.
  • 19. The package keyword is used to create a package in java. //save as Simple.java package mypack; public class Simple { public static void main(String args[]){ System.out.println("Welcome to package"); } }
  • 20. compile java package • javac -d directory javafilename For example • javac -d . Simple.java • The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
  • 21. run java package program To Compile: • javac -d . Simple.java To Run: • java mypack.Simple • Output:Welcome to package • The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.
  • 22. access package There are three ways to access the package from outside the package. • import package.*; • import package.classname; • fully qualified name. 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.
  • 23. Example of package that import the packagename.* //save by A.java package pack; public class A { public void msg(){System.out.println("Hello"); } } //save by B.java package mypack; import pack.*; class B{ public static void main(String args[]) { A obj = new A(); obj.msg(); } } Output:Hello
  • 24. Using packagename.classname //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
  • 25. 3) Using fully qualified name 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. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
  • 26. Importing a package • To import the java package into a class, we need to use the java import keyword which is used to access the package and its classes into the java program. • Use import to access built-in and user-defined packages into your java source file to refer to a class in another package by directly using its name. syntax: • import package.name.ClassName; // To import a certain class only • import package.name.* // To import the whole package
  • 27. import java.util.Date; // imports only Date class import java.io.*; // imports everything inside java.io package Example for creating and importing java packages: //save by Greet.java package greeting; // creating package public class Greet{ public void msg() { System.out.println("Hello"); } } //save by Main.java package Java; import greeting.*; //importing package class Main { public static void main(String args[]) { Greet g = new Greet(); g.msg(); } } • /*OUTPUT: • Hello */