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

Extending Interface

In TypeScript, interfaces can be extended using extends to inherit properties and methods from one or more interfaces. This enables creating more complex types by reusing existing ones. Extended interfaces can also add new properties, including optional ones, promoting code reuse and flexibility in type design.

Uploaded by

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

Extending Interface

In TypeScript, interfaces can be extended using extends to inherit properties and methods from one or more interfaces. This enables creating more complex types by reusing existing ones. Extended interfaces can also add new properties, including optional ones, promoting code reuse and flexibility in type design.

Uploaded by

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

SNS COLLEGE OF ENGINEERING

Kurumbapalayam(Po), Coimbatore – 641 107


Accredited by NAAC-UGC with ‘A’ Grade
Approved by AICTE, Recognized by UGC & Affiliated to Anna University, Chennai

Department of Artificial Intelligence & Data Science

SUBJECT: OBJECT ORIENTED PROGRAMMING

I YEAR /II SEMESTER

Topic – Interface

20/05/2024 SUCHITHRA C/AI&DS/AP 1


Interface
Rules
• Like abstract classes, interfaces cannot be used to create
objects.

• Interface methods do not have a body - the body is


provided by the "implement" class

• On implementation of an interface, you must override all


of its methods

• Interface methods are by default abstract and public

• Interface attributes are by default public, static and final

• An interface cannot contain a constructor (as it cannot be


used to create objects)
20/05/2024 SUCHITHRA C/AI&DS/AP 2
Interface
Interface Uses

• To Achieve Abstraction (This also adds security)

• To Achieve Multiple Inheritance (Java does not support multiple inheritance but using
interface we can achieve it)

What’s allowed?
• JDK 8 accepts
• Method body but it should be default or static
• Default methods, Static methods and Private methods
• Private methods from java 9

20/05/2024 SUCHITHRA C/AI&DS/AP 3


Interface
Example
interface example
{
int a=10;// 4. By default all the variable are public, static and final
void show();
static void display() // 2. Methods do not have body 3.Shoud be overridden in sample class
{
System.out.println("Hello");
}
}
class sample implements example
{
public void show()
{
System.out.println("Value of a is " +a);
}
/*public void display()
{
System.out.println("Hello");
}*/
}

20/05/2024 SUCHITHRA C/AI&DS/AP 4


Interface
Example
public class test
{
public static void main(String[] args)
{
example x=new sample(); // 1. cannot create objects for interface
x.show();
example.display();
}
}

20/05/2024 SUCHITHRA C/AI&DS/AP 5


Multiple Interface
interface square
{ public class test
void square(int a); {
} public static void main(String[] args)
interface rectangle {
{
area a1=new area();
void rectangle(int l,int w);
} a1.square(5);
class area implements square, rectangle a1.rectangle(5,4);
{ }
public void square(int a) }
{
a = a*a;
System.out.println("Area of square is " + a);
}
public void rectangle(int l, int w)
{
int a;
a=l*w;
System.out.println("Area of rectangle is " +a);
}
}
20/05/2024 SUCHITHRA C/AI&DS/AP 6
Extending Interface
• One interface can inherit another by use of the keyword extends.

• Similar to classes, interfaces can extend other interfaces.

• The extends keyword is used for extending interfaces.

• The syntax is the same as for inheriting classes.

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

20/05/2024 SUCHITHRA C/AI&DS/AP 7


Extending Interface
interface square
{ public class test
int a=2; {
void square(); public static void main(String[] args)
}
{
interface cube extends square
{ area a1=new area();
void cube(); a1.square();
} a1.cube();
}
class area implements cube
{
public void square()
{
int b;
b = a*a;
System.out.println("Area of square is " + b);
}
public void cube()
{
int b;
b=6*(a*a);
System.out.println("Area of cube is " +b);
}
}
}
20/05/2024 SUCHITHRA C/AI&DS/AP 8
THANK YOU

20/05/2024 SUCHITHRA C/AI&DS/AP 9

You might also like