The document discusses abstract classes and interfaces in Java. It defines abstract classes as classes that can have both abstract and non-abstract methods but cannot be instantiated. Abstract methods are declared without an implementation. Interfaces are collections of abstract methods that classes implement, inheriting the interface's behaviors. The Object class is the implicit superclass of all other classes in Java. It contains methods like getClass(), notify(), and wait() that are commonly used.
3. Abstraction in Java
• Abstraction is a process of hiding the implementation
details and showing only functionality to the user.
• Another way, it shows only essential things to the user and
hides the internal details,
There are two ways to achieve abstraction in java
Abstract class (0 to 100%)
Interface (100%)
4. Abstract Class
• An abstract class is a class that is declared abstract.
• An abstract method is a method that is declared without an
implementation (without braces, and followed by a semicolon),
like this:
• Abstract class may or may not include abstract methods.
• Abstract classes cannot be instantiated, but they can be sub-
classed.
5. • If a class includes abstract methods, the class itself must be
declared abstract, as in:
public abstract class GraphicObject
{ // declare fields
// declare non-abstract methods
abstract void draw();
}
6. • An abstract class must be declared with an
abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods
also.
• It can have final methods which will force the
subclass not to change the body of the method.
Points to Remember
7. A method which is declared as abstract and does not have
implementation is known as an abstract method.
abstract void printStatus();
//no method body and abstract
Abstract Method in Java
8. abstract class Bank {
abstract int getRateOfInterest();
}
class SBI extends Bank {
int getRateOfInterest() {
return 7;
} }
class PNB extends Bank {
int getRateOfInterest() {
return 8;
} }
class TestBank {
public static void main(String args[]) {
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
} }
10. Interfaces
• Using the keyword interface, you can fully abstract a
class’ interface from its implementation.
• An interface is a collection of abstract methods.
• An interface is not a class.
• A class describes the attributes and behaviors of an object
whereas an interface contains behaviors that a class
implements.
• A class implements an interface, thereby inheriting the
abstract methods of the interface.
• Unless the class that implements the interface is abstract,
all the methods of the interface need to be defined in the
class.
11. Properties of Interfaces
The interface keyword is used to declare an interface.
Interfaces have the following properties:
An interface is implicitly abstract. We do not need to use the
abstract keyword when declaring an interface.
Each method in an interface is also implicitly public and
abstract, so the abstract keyword is not needed.
Each variable in an interface is implicitly public, static and final.
By interface, we can support the functionality of multiple
inheritance
It can be used to achieve loose coupling
12. Interface Vs Class
An interface is similar to a class in the following ways:
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the
name of the interface matching the name of the file.
• The byte-code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding byte-
code file must be in a directory structure that matches the
package name.
13. Interface Vs Class
An interface is different from a class in several ways,
including:
• We cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a
class.
• An interface can extend multiple interfaces.
14. Implementing Interfaces
When a class implements an interface, then it has to
perform the specific behaviors of the interface.
If a class does not perform all the behaviors of the
interface, the class must declare itself as abstract.
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.
15. General form of an interface:
interface Bank
{
float rateOfInterest();
}
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
//...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
16. Implementing Interfaces
• Once an interface has been defined, one or more classes can implement that
interface.
• To implement an interface, include the implements clause in a class definition,
and then create the methods required by the interface.
• The general form of a class that includes the implements clause looks like this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
• If a class implements more than one interface, the interfaces are separated with a
comma.
• If a class implements two interfaces that declare the same method, then the same
method will be used by clients of either interface. The methods that implement an
interface must be declared public.
• Also, the type signature of the implementing method must match exactly the type
signature specified in the interface definition.
17. Example
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest()
{return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest()
{return 9.7f;}
}
class TestInterface{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
18. import java.io.*;
interface In1
{
final int a = 10;
void display(); // default void display(); }
class TestClass implements In1
{
public void display()
{
System.out.println("LPU");
}
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
19. import java.io.*;
interface In1
{
//static methods in interfaces which can be called independently
// without an object
static void display()
{
System.out.println("hello");
}
}
class TestClass implements In1
{
public void display()
{
System.out.println("LPU");
}
public static void main (String[] args)
{
In1.display();
}
}
20. Abstract Class Vs Interfaces
Abstract Class Interface
May contain non-abstract methods
and non-static non final data members
Contains only method declaration and
static final data members
Multiple Inheritance is not supported
through classes
Multiple Inheritance through
Interfaces is supported
Classes provide static classing
environment
Interfaces provide dynamic classing
environment
Inheritance using ‘extends’ keyword Inheritance using ‘implements’
keyword
22. Object Class
There is one special class, called Object, defined by Java.
All other classes are subclasses of Object.
A reference variable of type Object can refer to an object of any other
class.
Arrays are implemented as classes, so a variable of type Object can also
refer to any array.
In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final.
24. What is the output of this program?
class Test
{
public static void main(String[] args)
{
Object obj = new String(“LPU in Punjab");
Class c = obj.getClass();
System.out.println(c.getName());
}
}
a. java.util.String
b. LPU in Punjab
c. java.lang.Object
d. java.lang.String
25. public class Test
{
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");
}
}