0% found this document useful (0 votes)
11 views16 pages

Lecture No 10 Updated

The document provides an overview of interfaces and abstract classes in Java, detailing their definitions, syntax, and usage. It includes examples of creating and implementing interfaces, as well as the differences between interfaces and abstract classes. Additionally, it outlines tasks for creating specific interfaces and classes to reinforce understanding of the concepts.

Uploaded by

Batool Murtaza
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)
11 views16 pages

Lecture No 10 Updated

The document provides an overview of interfaces and abstract classes in Java, detailing their definitions, syntax, and usage. It includes examples of creating and implementing interfaces, as well as the differences between interfaces and abstract classes. Additionally, it outlines tasks for creating specific interfaces and classes to reinforce understanding of the concepts.

Uploaded by

Batool Murtaza
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/ 16

Lecture No.

10

Interface and Abstract


class
Interface in java

 Interface is just like a class, which contains only abstract methods.

 To achieve interface java provides a keyword called implements.

 Interface methods are by default public and static.

 Interface variables are by default public, static, and final.

 Interface methods must be overridden inside the implementing classes.

 Interface is nothing but deals between client and developer.


interface syntax

interface <interface-name>
{
// declare constants
// declare abstract methods
}

The syntax to implement an interface is as follows:


Syntax

class <class-name> implements <interface-name>,…


{
// class members
// overridden abstract methods of the interface(s)
}
A simple program of interface

interface Animal { // Define an interface


void makeSound(); // Abstract method
}

class Dog implements Animal { // Implement the interface in a class

@Override
public void makeSound() {
System.out.println("Dog barks");
}
}

public class InterfaceExample { // Main class

public static void main(String[] args) {


Dog myDog = new Dog();
myDog.makeSound(); // Output: Dog barks
}
}
Task
Create an interface Vehicle with a method speedup(). Implement this interface in two classes: Car and
Bike. Display Car is speeding up for car and bike is speeding up for bike.

// Define the interface // Main class


interface Vehicle { public class InterfaceExercise2 {
void speedUp(); public static void main(String[] args) {
} Vehicle myCar = new Car();
Vehicle myBike = new Bike();
class Car implements Vehicle { myCar.speedUp(); // Output: Car is speeding up!
@Override myBike.speedUp(); // Output: Bike is speeding up!
public void speedUp() { }
System.out.println("Car is speeding up!"); }
}}

class Bike implements Vehicle {


@Override
public void speedUp() {
System.out.println("Bike is speeding up!");
}}
Task
• Write a Java program to create an interface Shape with the getArea() method.
Create three classes Rectangle, Circle, and Triangle that implement the Shape
interface. Implement the getArea() method for each of the three classes.
// Define the Shape interface
interface Shape { class Triangle implements Shape {
double getArea(); // Abstract method double base, height;
}
Triangle(double base, double height) {
// Implement Rectangle class
this.base = base;
class Rectangle implements Shape {
this.height = height;
double length, width;
}
Rectangle(double length, double width) {
this.length = length; // Implement getArea() method
this.width = width; @Override
} public double getArea() {
return 0.5 * base * height;
// Implement getArea() method }}
@Override class ShapeInterfaceExample {
public double getArea() { public static void main(String[] args) {
return length * width; Shape rectangle = new Rectangle(10, 5);
}} Shape circle = new Circle(7);
class Circle implements Shape { Shape triangle = new Triangle(6, 4);
double radius; // Display the area of each shape
final double PI = 3.14159; System.out.println("Rectangle Area: " + rectangle.getArea());
System.out.println("Circle Area: " + circle.getArea());
Circle(double radius) { System.out.println("Triangle Area: " + triangle.getArea());
this.radius = radius; }
} } Output:
// Implement getArea() method
@Override Rectangle Area: 50.0
public double getArea() { Circle Area: 153.93791
return PI * radius * radius; Triangle Area: 12.0
}}
Extending interfaces in java
 One interface can inherit another by the use of keyword extends.

 When a class implements an interface that inherits another interface, it must provide an
implementation for all methods required by the interface inheritance chain.

interface ParentInterface {
void m1();
void m2();
}

interface ChildInterface extends ParentInterface


{
void m3();
}
Task
Create an interface A with void method1() and void method2() and second interface B extends 1st
interface with void method3(). Create a class implements the interface B and provide its
functionality.
Task
interface A {
public static void main(String[] args){
void method1(); GFG x = new GFG(); // Instance of GFG class created
void method2(); x.method1();
} x.method2();
interface B extends A { x.method3();
void method3(); }
} }
// the class must implement all method of A and B.
class GFG implements B
{
public void method1() {
System.out.println("Method 1");
}

public void method2() {


System.out.println("Method 2");
}

public void method3() {


System.out.println("Method 3");
}
Multiple inheritance in
java
class C extends A, B wrong

class C implements A, B right

11
Abstract class in java

• A class which contains the abstract keyword in its declaration is called abstract
class.

• To use an abstract class, it must be inherited from sub class.

• May or may not contain abstract methods.

• Note: Object of abstract class can not be instantiated but reference because
every super class contains the capability to store the object of sub class.
Example

abstract class Animal {


abstract void makeSound(); // Abstract method (no body)

void sleep() { // Concrete method (has implementation)


System.out.println("Sleeping...");
}
}

class Dog extends Animal {


public void makeSound() { // Implementing the abstract method
System.out.println("Bark! Bark!");
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
myDog.sleep();
}
}
Task
Create an abstract class Animal with its constructor prints “All animals….!” as well as abstract method void
sound(). The class dog and lion extends animal abstract class. Provide the functionality of abstract as well
as extending classes in the main class.
Task
abstract class Animal {
Animal(){ class Test{
System.out.println(“All animals…!”); public static void main(String args[]){
} Dog d = new Dog();
public abstract void sound(); Lion l = new Lion();
} d.sound();
l.sound();
class Dog extends Animal { }
Dog(){ }
super();
}
public void sound(){
System.out.println(“Dog is barking”);
}
}
class Lion extends Animal {
Lion(){
super();
}
public void sound(){
System.out.println(“Lion is roaring”);
}
}
Difference between interface and abstract class.

Interface Abstract

Contains only abstract methods. Contains both abstract and non-abstract methods.

Supports multiple inheritance Does not support multiple inheritance

By default interface methods are No any restriction on abstract class


public + abstract. method modifier

By default interface variables are No need to declare variable as public + static +


public + static + final. final.

interface keyword is used to declare interface. abstract keyword is used to declare abstract class.

You might also like