0% found this document useful (0 votes)
22 views15 pages

00 2022 Lecture Abstract Classes and Interface

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

00 2022 Lecture Abstract Classes and Interface

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

Abstract Classes and Interfaces

Outcome of the Lecture

Upon completion of this lecture you will be able to

 Understand the concept and Utility of Abstract Classes

 Define and Use Interfaces

 Place final keyword appropriately


Outline of the Presentation

 Abstract Classes and Methods

 Defining Interfaces

 Extending Interfaces

 Implementing Interfaces

 Use of final keyword


Abstraction

 Abstraction is a process of hiding the implementation details and


showing only functionality to the user
 Abstraction lets you focus on what the object does instead of how
it does it
 There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract Classes and Methods

 In the inheritance hierarchy, classes become more specific and


concrete with each new subclass
 from a subclass back up to a superclass, the classes become more
general and less specific
 Sometimes a superclass is so abstract that it cannot have any
specific instances
 Referred to as an abstract class
 Such a class determines the nature of the methods that the
subclasses must implement
Abstract Classes and Methods

 Abstract class is declared with abstract keyword


 It can have abstract and non-abstract methods
 Abstract Method contains no implementation, i.e. no body.
 Abstract Method is created to force same name and signature
pattern in all the subclasses
 Subclasses have the flexibility to code these methods with their
own specific requirements
 An abstract method cannot be contained in a nonabstract class
Abstract Classes and Methods
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
 When an abstract class is subclassed, the subclass usually provides implementations for
all of the abstract methods in its parent class
class Circle extends GraphicObject { class Rectangle extends GraphicObject {
void draw() { void draw() {
... ...
} }
void resize() { void resize() {
... ...
} }
} }
Abstract Classes and Methods

 If a subclass of an abstract superclass does not implement all the abstract


methods, the subclass must be defined abstract
 An abstract class cannot be instantiated using the new operator, but it may
contain constructors, which are invoked in the constructors of its subclasses
 A class that contains abstract methods must be abstract. However, it is
possible to define an abstract class that contains no abstract methods
 A subclass can be abstract even if its superclass is concrete. For example, the
Object class is concrete, but its subclasses, such as GeometricObject, may be
abstract
 A subclass can override a method from its superclass to define it abstract
 You cannot create an instance from an abstract class using the new
operator, but an abstract class can be used as a data type
Interfaces

 Class like construct that contains only constants and abstract methods
 an interface is similar to an abstract class, but its intent is to specify
common behavior for objects

modifier interface InterfaceName {


/** Constant declarations */
/** Method signatures */
}

public interface Stack{


public abstract void push(int item);
public abstract void pop();
}
Interfaces

 An interface is treated like a special class in Java. Each interface is compiled


into a separate bytecode file, just like a regular class
 Can not create instance from an interface using the new operator
 Can be used as a data type for a reference variable
 Relationship between the class and the interface is known as interface
inheritance
 A constant defined in an interface can be accessed using the syntax
InterfaceName. CONSTANT_NAME
 All data fields are public final static and all methods are public abstract in an
interface, Java allows these modifiers to be omitted.
Implementing Interfaces

 An interface is treated like a special class in Java.

class MyStack implements Stack{ interface Stack{


int top = -1; public abstract void push(int item);
int[] stack = new int[5]; public abstract void pop();
public void push(int x){ }
top++;
stack[top]=x;
public class IntDemo{
}
public static void main(String[] a){
public void pop(){
MyStack ms = new MyStack();
System.out.println("Value is : "+stack[top]);
ms.push(10);
top--;
ms.push(20);
}
ms.push(30);
public void display()
ms.display();
{
ms.pop();
for(int i = 0;i<=top;i++)
ms.display();
System.out.println(stack[i]);
}
}
}
}
Implementing Interfaces

 Another Implementation of Stack


class MyStack implements Stack{
int top = 5; interface Stack{
int[] stack = new int[5]; public abstract void push(int item);
public void push(int x){ public abstract void pop();
top--; }
stack[top]=x;
}
public void pop(){ public class IntDemo{
System.out.println("Value is : "+stack[top]); public static void main(String[] a){
top++; MyStack ms = new MyStack();
} ms.push(10);
public void display() ms.push(20);
{ ms.push(30);
for(int i = top;i<5;i++) ms.display();
System.out.println(stack[i]); ms.pop();
} ms.display();
} }
}
Extending Interfaces

 An interface can inherit other interfaces using the extends keyword

public NewInterface Interface1, ..., InterfaceN {


// constants and abstract methods
}
 A class implementing NewInterface must implement the abstract methods
defined in NewInterface, Interface1, …. and InterfaceN

 An interface can extend other interfaces but not classes

 A class can extend its superclass and implement multiple interfaces.


Interfaces vs Abstract Classes
final keyword

Use with Effect

data field It becomes constant

method Prevents overloading

class Prevents further inheritance

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy