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

MCA I Yr Lect - 12

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)
14 views

MCA I Yr Lect - 12

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/ 30

MCA I Yr.

(AIML)- SEM – I

Course Name: Object Oriented Programming

Dr. Sachin Upadhye


Department of Computer Science and Applications
School of Computer Science and Engineering
RBU, Nagpur
[email protected], 9970094109
SYLLABUS OF SEMESTER - I,
MCA (MASTER OF COMPUTER APPLICATIONS)
(Artificial Intelligence and Machine Learning)

Course : Object Oriented Programming


Total Credits : 4
L: 3 Hrs, T: 0 Hr, P: 2 Hr, Per Week
Abstract Class and Methods
Abstract class in Java

• A class which is declared as abstract is known as an


abstract class.

• It can have abstract and non-abstract methods. It


needs to be extended and its method implemented.
It cannot be instantiated.
Abstract Class and Methods
Abstract Class and Methods
Abstract Method in Java

• A method which is declared as abstract and does not


have implementation is known as an abstract
method.

• Example of abstract method


Abstract Class and Methods
abstract class Bike{
abstract void run();
}
class Honda extends Bike{
void run(){
System.out.println(“running avg speed is 90");
}
public static void main(String args[]){
Bike obj = new Honda ();
obj.run();
}
}
Abstract Class and Methods
Abstract Class and Methods
• Example of an abstract class that has abstract and non-abstract methods
Abstract Class and Methods
Interface and implementation of interface
• Java interface is a collection of abstract methods.
The interface is used to achieve abstraction in which
you can define methods without their
implementations (without having the body of the
methods).

• An interface is a reference type and is similar to the


class.

• Writing an interface is similar to writing a class.


However, a class describes the attributes and
behaviors of an object.
Interface and implementation of interface
• An interface contains behaviors that a class
implements. Unless the class that implements the
interface is abstract, all the methods of the interface
need to be defined in the class.
Interface and implementation of interface
Interface and implementation of interface
Use to achieve Abstraction

An interface only stores the method signature and not


the method definition.

Method Signatures make an Interface achieve


complete Abstraction by hiding the method
implementation from the user.
Interface and implementation of interface
Multiple Inheritance

Without Interface, the process of multiple inheritances


is impossible as the conventional way of inheriting
multiple parent classes results in profound ambiguity.
Interface and implementation of interface
Loose Coupling
The term Coupling describes the dependency of one class for
the other. So, while using an interface, we define the method
separately and the signature separately. This way, all the
methods, and classes are entirely independent and archives
Loose Coupling.
Interface and implementation of interface
Java Interfaces and Classes: Similarities

• An interface can contain any number of methods.

• An interface is written in a file with a .java extension,


with the name of the interface matching the name of
the file.

• The byte code of an interface appears in a .class file.

• Interfaces appear in packages, and their


corresponding bytecode file must be in a directory
structure that matches the package name.
Interface and implementation of interface
Java Interfaces and Classes: Differences

• You cannot instantiate an interface.

• An interface does not contain any constructors.

• All of the methods in an interface are abstract.

• An interface cannot contain instance fields. The only


fields that can appear in an interface must be declared
both static and final.

• An interface is not extended by a class; it is implemented


by a class.

• An interface can extend multiple interfaces.


Interface and implementation of interface
Declaring an Interface in Java
Interface and implementation of interface
Properties of Java Interface

• An interface is implicitly abstract. You do not need to


use the abstract keyword while declaring an
interface.

• Each method in an interface is also implicitly


abstract, so the abstract keyword is not needed.

• Methods in an interface are implicitly public.


Interface and implementation of interface
Implementing
Interfaces in Java
Interface and implementation of interface
Rules for implementing Java Interfaces

• A class can implement more than one interface at a


time.

• A class can extend only one class, but implement


many interfaces.

• An interface can extend another interface, in a


similar way as a class can extend another class.
Interface and implementation of interface
Here we try to calculate the area of geometrical
shapes, and for each shape, we have different
methods. And all the methods are defined,
independent of each other. Only method
signatures/declaration are written in the Interface.
Interface and implementation of interface
//Interface //Class
public interface Area import java.util.Scanner;
{ public class shapeArea implements Area {
public void Square(); public void Circle() {

public void Circle(); Scanner kb = new Scanner(System.in);


System.out.println("Enter the radius of the circle");
public void Rectangle();
double r = kb.nextInt();
public void Triangle();
double areaOfCircle = 3.142 * r * r;
}
System.out.println("Area of the circle is " + areaOfCircle);
}
Interface and implementation of interface

//Class
import java.util.Scanner;
public class shapeArea implements Area {
public void Circle() {
Scanner kb = new Scanner(System.in);
System.out.println("Enter the radius of the circle");
double r = kb.nextInt();
double areaOfCircle = 3.142 * r * r;
System.out.println("Area of the circle is " + areaOfCircle);
}
Interface and implementation of interface
public void Square() {
// TODO Auto-generated method stub
Scanner kb2 = new Scanner(System.in);
System.out.println("Enter the length of the side of the
square");
double s = kb2.nextInt();
double areaOfSquare = s * s;
System.out.println("Area of the square is " +
areaOfSquare);
}
Interface and implementation of interface
public void Rectangle() {
// TODO Auto-generated method stub
Scanner kb3 = new Scanner(System.in);
System.out.println("Enter the length of the Rectangle");
double l = kb3.nextInt();
System.out.println("Enter the breadth of the Rectangle");
double b = kb3.nextInt();
double areaOfRectangle = l * b;
System.out.println("Area of the Rectangle is " + areaOfRectangle);
}
Interface and implementation of interface
public void Triangle() {
// TODO Auto-generated method stub
Scanner kb4 = new Scanner(System.in);
System.out.println("Enter the base of the Triangle");
double base = kb4.nextInt();
System.out.println("Enter the height of the Triangle");
double h = kb4.nextInt();
double areaOfTriangle = 0.5 * base * h;
System.out.println("Area of the Triangle is " + areaOfTriangle);
}
Interface and implementation of interface
public static void main(String[] args) {
shapeArea geometry = new shapeArea();
geometry.Circle();
geometry.Square();
geometry.Rectangle();
geometry.Triangle();
}
}

You might also like