You are on page 1of 15

CO_IF_22412_UO3

Chetashri Bhusari, Lecturer, Vidyalankar Polytechnic

Date: 02 February 2021

MSBTE LEAD: Learning at your Doorstep


Unit 03 :
Inheritance ,Interface and
package

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Unit Outcome 3 :
Developed program using specified
interface

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Learning Outcome 3 :Students should
understand concept of interface.

Written by

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
What we will learn today

1. Concept of interface Key takeaways


2. Accessing interface Concept of interface
3. Extending interface How to access

Chetashri Sachin Bhusari


Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai

Page 5 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Map

Interface
Accessing
interface

Extending
Interface

Page 6 Maharashtra State Board of Technical Education 2 Feb 2021


Learning Objective/ Key learning

► Understand concept of interface


► Understand concept of Implementing Interface
► Understand concept of Accessing Interface Variables and Method
► Understand concept of Extending interface
► Understand concept of multilevel inheritance
► Example

Page 7 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: interface

► An interface is collection of abstract method and it is defined much like class.


► Writing an interface is similarto the class, with abstract methods and final field. This means that
interface do not specify any code to implement those methods & data fields containing only
constants.
► Interfaces cannot be instantiated they can be only implemented by the classes or extended by other
► Interfaces.
► Interfaces are design to support dynamic method resolution at runtime.

Page 8 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: interface

 The general from of interface is access interface interface name


{
variable declaration; method declaration;
}
 Access is either public or not used when no access specifier is included, then default access result and interface is
only available to other member of the package in which it is declared.
 For e.g.-
public interface IF4IA
{
String Lecture=”JPR”; int benches=35;
void display();
}

Page 9 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: Implementing Interface

► To declare a class that implements an interface, you need to include ‘implements’ clause in the class declaration.
► A class can only inherit from one super class. However, a class may implement several interfaces.
► Your class can implements keyword is followed by a ‘comma separated list’ of the interface implemented by a
class.
Syntax:- For e.g.- inteface shape
class class_name implements interface {
{ public String baseclass="Shape"; public void draw();
//body }
} OR class circle implements shape
class class_name extends class_name {
implemnts interface 1,2 public void draw()
{
System.out.println("Circle drawn here");
}
}

Page 10 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation : Accessing Interface Variables and Method

 An interface can use to declare set of constants that can be used in different classes.

 It is similar to creating header files in c++ to contain a large no. of constants.


 Such interface does not contain methods, there is no need to worry about implementing methods.
 All variable declared in interface must be constant.

Page 11 Maharashtra State Board of Technical Education 2 Feb 2021


Program

int x=12; public void disp()


} {
interface two System.out.println("Value access from interface one:" +a);
{ System.out.println("Value access from interface two:" +b);
int y=10; }
void display(); }
} class multipleinterface
class demo implemets one, two {
{ public static void main(String args[])
int a=x; int b=y; {
public vioddisplay() demo d= new demo(); d.display();
{ d.disp();
System.out.println("x in interfece one" +x); System.out.println("y in }
interfece one" +y); System.out.println("x & y"+(x+y)); }
}

12

Page 12 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation:Extending interface

• An interface can extend another interface, similarly to public void display()


the way that a class can extend another class. {
• The extends keywords is used to extend an interface, System.out.println("calling interface B");
and the child interface inherits the methods of the }
parent interface. public void disp()
Syntax:- {
interface class2 ectends class1 System.out.println("class C is working");
{ }
//body }
} class D
For e.g.:-inteface A {
{ Public static void main(String a[])
void show(); {
} C object=new C();
interface B extends A object.show();
{ object.display();
void display(); object.disp();
} }
class C implements B public void show() }
{
System.out.println("calling interface A"); 13
}
Page 13 Maharashtra State Board of Technical Education 2 Feb 2021
Program

class Super class A


{ {
int x; int i;
Super (int x) A(int a, int b)
{ {
this.x=x; i=a+b;
} }
void display() void add()
{ {
System.out.println(“Sum of a & b is=” +i);
System.out.println(“Super x=”‖+x); }
}
}
}
Class Sub extends Super class B extends A
{ {
int y; int j;
Sub( int x, int y) B(int a, int b, int c)
{ {
super (x); super(a,b); j=a+b+c;
this.y=y; }
} void add()
void display() {
{ super.add();
System.out.println(“Super x=”‖ +x); System.out.println(“Sum of a, b & c”+j);
System.out.println(“Sub y=‖” +y); }
} } 14
} class MethodOverride
class overrideTest {
{ public static void main(String args[])
public static void main(String arg[]) {
{ B b=new B(10,20,30);
Sub s1= new Sub(100,200); s1.display(); b.add();
}
}
}
}
Page 14 Maharashtra State Board of Technical Education 2 Feb 2021
Concept Explanation: multiple inheritance through interface

 It is type of interface where a derived class may have more than }


one parent class. }
 It is not possible in case of Java as you cannot have two parent class result extends test implements sports
classes at the parent level, instead there can be one class and {
one interface at parent level to achieve multiple inheritance. result (int r, String nm, int m11,int m12)
 Interface is similar to classes but can contain final variable and {
abstract methods. Interfaces can be implemented to a derived super (r,nm,m11,m12);
class. }
public void disp()
For e.g : - interface sports {
{ System.out.println("Roll no : "+roll_no);
int sport_wt=5; System.out.println("Name : "+name);
public void disp(); System.out.println("sub1 : "+m1);
} System.out.println("sub2 : "+m2);
class test System.out.println("sport_wt : "+sport_wt);
{ int t=m1+m2+sport_wt
int roll_no; System.out.println("total : "+t);
String name }
int m1,m2; public static void main(String args[])
test(int r, String nm, int m11,int m12) {
{ result r= new result(101,"abc",75,75); r.disp();
roll_no=r; }
name=nm; }
m1=m11; 15
=m12;
Page 15 Maharashtra State Board of Technical Education 2 Feb 2021

You might also like