0% found this document useful (0 votes)
18 views

Abstraction & Object Class

Uploaded by

Bhumika Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Abstraction & Object Class

Uploaded by

Bhumika Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Abstract Class,

Interfaces,
Object Class,
T.Varun
Abstraction
• Abstraction is a process hiding the
implementation
of details and showing only
functionality to the user.

• This can be achieved using:


– Abstract class
– Interface
Abstract class
• A class which is declared with the
abstract keyword is known as an abstract class
in Java.

• It can have abstract and non-abstract methods


(method with the body).

• An Abstract class cannot be instantiated


– objects cannot be created.
Abstract Class Syntax
abstract class ClassName
{
...

abstract Type MethodName1();


Type Method2()
{
// method body
}
}

• The abstract methods of an abstract class must be


defined in its subclass.
4
Abstract Methods
Methods can also be abstracted:

An abstract method is one to which a signature has been


provided, but no implementation for that method is given.

An Abstract method is a placeholder. It means that we declare


that a method must exist, but there is no meaningful
implementation for that methods within this class

Any class which contains an abstract method MUST also be


abstract.
class Vehicle{
abstract void run(); Compile Time
} Error
Example-1
abstract class Bike{
abstract void run(); // Only declared
}
class Honda extends Bike{
void run(){ //Defined
System.out.println("running safely");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run(); }
}
Output: running safely
Example 2
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user

class Rectangle extends Shape{


void draw(){
System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){
System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class Abstraction {
public static void main(String args[]){
Shape s=new Circle1(); //In a real scenario, object is
provided through method
, e.g., getShape() method
}s.draw();
} Output: drawing circle
Abstract class : With constructor, data member
and methods

• An abstract class can have a data member,


abstract method, method body (non-
abstract method), constructor, and
even main() method.
Example
abstract class Bike{
Bike(){ Note: You can define a constructor in an
System.out.println( abstract class, but you can't construct
"bike is that object. However, concrete sub-
classes can (and must) call one of the
created");}
constructors defined in the abstract
abstract void run(); parent class.
void changeGear(){
//Creating a Child class which inherits Abstract class
System.out.println("
class
gearHonda extends Bike{
changed");}
void
} run(){System.out.println("running safely..");}
}
Constructor is
//Creating a Abstraction class which calls abstract and non- implicitly
abstract methods Called
class Abstraction{
public static void main(String args[]){
Bike obj = new Honda(); Output:
obj.run(); bike is created
obj.changeGear(); running safely..
} gear changed
}
Interfaces
Java Interface
• A Java interface is a collection of constants
and abstract methods
– abstract method: a method header without a method
body; we declare an abstract method using the modifier
abstract
– since all methods in an interface are abstract, the
abstract modifier is usually left off

• Methods in an interface have public visibility by


default.
• 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.
11
• Interfaces are declared using the "interface" keyword.

• Interfaces are implemented by classes using

the "implements" keyword


Implementing Interfaces

A Class can only inherit from one superclass. However, a class


may implement several Interfaces...........
The interfaces that a class implements are separated by commas

• Any class which implements an interface must provide


an
implementation for all methods defined within the interface.
NOTE: if an abstract class implements an interface, it NEED NOT
implement all methods defined in the interface. HOWEVER, each
concrete subclass MUST implement the methods defined in the
interface.

• Interfaces can inherit method signatures from other interfaces.


Example-1
interface
print{ void
printing();
}
class A6 implements
print{ public void printing()
{ System.out.println("Hello"
);
}
public static void main(String args[]){
A6 obj = new A6();
obj.printing();
} Output: Hello
}
Relationship between classes and
interfaces
Example- 2
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 Interface{ ROI: 9.15
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Multiple inheritance in Java by Interface
Example 3
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements
Printable,Showable{ public void print()
{System.out.println("Hello");}
public void show()
public static void main(String args[]){
{System.out.println("Welcome");}
A7 obj = new A7();
obj.print();
obj.show(); Output: Hello
Welcome
}
}
Object Class
Introduction
• Object class is the parent class of all
the classes in java by default.
• Every class in Java is directly or
indirectly derived from the Object class.
• It is the topmost class of java.
• Object class is beneficial if you want to
refer any object whose type you don't know.
Contd..
• Let’s say a method called showObject() is there that returns an object, but it
can be of any type like Employee,Student etc,.

• We can use Object class reference to refer that object.

Object obj=showObject(); //we don't know what object will be returned from this method

• The Object class defines the basic state and behavior that all objects must
have,
– such as the ability to compare oneself to another object,
– to convert to a string,
– to wait on a condition variable,
– to notify other objects that a condition variable has changed, and
– to return the object's class.
Methods of Object Class
• The equals Method: Use the equals to compare two objects
for equality. This method returns true if the objects are
equal, false otherwise.

• Example: Two Integers, one1 and one2, for equality:


Integer A = new Integer(1), B= new Integer(1);

if (A.equals(B))
System.out.println("objects are equal");

Output: objects are equal

o Though A and B reference two different, distinct objects, they are considered equal because they
contain the same integer value.
o Your equals method should compare the contents of the objects to see if they are functionally
equal.
Contd..
• The getClass Method: The getClass method is a final method (cannot be
overridden) that returns a runtime representation of the class of this
object. This method returns a Class object.
• You can query the Class object for a variety of information about the
class, such as its name, its superclass, and the names of the interfaces
that it implements.

• Example: The following method gets and displays the class name of an object:

void PrintClassName(Object obj) {


System.out.println("The Object's class is " + obj.getClass().getName());
}

• One handy use of the getClass method is to create a new instance of a class
without knowing what the class is at compile time. This sample
method creates a new instance of the same class as obj which can be any
class that inherits from Object (which means that it could be any
class):
Object createNewInstanceOf(Object obj)
{ return obj.getClass().newInstance();
}
Contd..

• The toString Method: It returns a String representation of the


object.
– The String representation for an object is entirely dependent on the
object.
– The String representation of an Integer object is the
integer value displayed as text.

• Example:

System.out.println(Thread.currentThread().toString());
Methods of Object class
Method Description

returns the Class class object of this object. The Class class
public final Class getClass() 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
CloneNotSupportedExceptio creates and returns the exact copy (clone) of this object.
n

public String toString() returns the string representation of this object.

public final void notify() wakes up single thread, waiting on this object's monitor.
Contd..
public final void notifyAll() wakes up all the threads, waiting on this object's monitor.

public final void wait(long causes the current thread to wait for the specified
timeout)throws milliseconds, until another thread notifies (invokes notify()
InterruptedException or notifyAll() method).

public final void wait(long causes the current thread to wait for the specified
timeout,int nanos)throws milliseconds and nanoseconds, until another thread
InterruptedException notifies (invokes notify() or notifyAll() method).

public final void wait()throws causes the current thread to wait, until another thread
InterruptedException notifies (invokes notify() or notifyAll() method).

protected void finalize()throws is invoked by the garbage collector before object is being
Throwable garbage collected.

You might also like