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
INTERFACES. with machine learning and data
dineshkesav07
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PDF
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
bca23189c
 
PPTX
java interface and packages
VINOTH R
 
PPTX
Unit3 packages & interfaces
Kalai Selvi
 
PPT
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
PDF
Exception handling and packages.pdf
Kp Sharma
 
PDF
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
KusumitaSahoo1
 
INTERFACES. with machine learning and data
dineshkesav07
 
it is the quick gest about the interfaces in java
arunkumarg271
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
bca23189c
 
java interface and packages
VINOTH R
 
Unit3 packages & interfaces
Kalai Selvi
 
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
Exception handling and packages.pdf
Kp Sharma
 
JAVA 3.1.pdfdhfksuhdfshkvbhdbsjfhbvjdzfhb
KusumitaSahoo1
 

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

PDF
this keyword in Java.pdf
ParvizMirzayev2
 
PPTX
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
PPTX
Interface
vvpadhu
 
PPTX
U3 JAVA.pptx
madan r
 
PPTX
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PPTX
Java Interface
Manish Tiwari
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PDF
java-06inheritance
Arjun Shanka
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPTX
ABSTRACTION IN JAVA AAAAPROGRAMMING.pptx
DrNeetuSharma5
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
DOC
Java interview questions
G C Reddy Technologies
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
PDF
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
PDF
Java 06
Loida Igama
 
PPTX
Java interface
GaneshKumarKanthiah
 
this keyword in Java.pdf
ParvizMirzayev2
 
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Interface
vvpadhu
 
U3 JAVA.pptx
madan r
 
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Java Interface
Manish Tiwari
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
java-06inheritance
Arjun Shanka
 
oops with java modules i & ii.ppt
rani marri
 
oops concept in java | object oriented programming in java
CPD INDIA
 
ABSTRACTION IN JAVA AAAAPROGRAMMING.pptx
DrNeetuSharma5
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Java interview questions
G C Reddy Technologies
 
this keyword in Java.pptx
ParvizMirzayev2
 
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Java 06
Loida Igama
 
Java interface
GaneshKumarKanthiah
 
Ad

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Understanding operators in c language.pptx
auteharshil95
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
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 */