Module III Oops
Module III Oops
Module III Oops
Defining an Interface
interface inerfacename {
return-type method-name1(parameter-list);
type final-varname1 = value;
……
}
• The variable inside an interface are public, final and static, meaning they cannot be
changed by the implementing class. They must also be initialized.
• All methods are implicitly abstract and public.
Ex:
interface A{
int x=20;
void show();
}
Dr. Venkata Rami Reddy Ch , Sr. Assistant Professor, SCOPE
Interfaces
Implementing Interfaces
• implements keyword is used to implement interface.
Syntax:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
• All the methods of an interface should be implemented in its sub class.
• If any method is not implemented in its subclass then declare that sub class as abstract
class.
• A sub class can implement more than one interface.
• We should not create an object for interface.
interface A
{ class demo3
int x=20; {
public void show(); public static void main(String[] args) {
} B ob=new B();
class B implements A ob.show();
{ }
int y=30; }
public void show()
{
system.out.println(x+y);
}
}
• public double periTriangle ();. The method returns the perimeter of the triangle.
• public double periRectangle (); The method returns the perimeter of the rectangle.
•(NOTE: For calculating the perimeter of a triangle use the formula, perimeter = side1 + side2 + side3
and for calculating the perimeter of a rectangle use the formula perimeter = 2 * (side1 + side2)).
John attended Object Oriented Programming lectures at VIT-AP and created a class named Perimeter
that implements an interface Shape. In Perimeter class, declare 4 instance variables namely double
side1, double side2, double side3, and double perimeter and take the values of the variables double
side1, double side2, double side3 from the user. Create an object of the Perimeter class and access the
instance methods periTriangle and periRectangle and display the perimeters of triangle and rectangle.
Shape interface
interface Shape { class Circle implements Shape {
double getArea(); double radius;
} public Circle(double radius) {
this.radius = radius;
class Rectangle implements Shape { }
double length; public double getArea() {
double width; return Math.PI * radius * radius;
public Rectangle(double length, double width) }
{ }
this.length = length;
this.width = width;
}
public double getArea() {
return length * width; public class Main {
} public static void main(String[] args) {
} Rectangle rectangle = new Rectangle(5, 3);
Circle circle = new Circle(4);
System.out.println("Rectangle area: " + rectangle.getArea())
System.out.println("Circle area: " + circle.getArea());
}}
Importing packages:
• import java.util.Scanner; https://docs.oracle.com/javase/8/docs/api/.
Creating a Package:
package packagename;
Ex:
package myPackage;
• The preceding statements in fact create a directory with the given package name.
• We should of course add our classes and interfaces to this directory.
package prime;
import prime.Primecheck;
public class Primecheck {
public boolean prime(int n) { public class calculate_prime {
if (n <= 1) { public static void main(String[] args) {
return false; Primecheck pc= new Primecheck();
} int[] numbers = {2,3,4,5,6,7,8,9};
for (int i = 2; i <= n-1; i++) System.out.println("Prime numbers in
{ the given list:");
if (n % i == 0) { for (int num : numbers) {
return false; if (pc.prime(num)) {
} System.out.println(num);
} } } }}
return true;
} Prime numbers in the given list:
} 2 3 5 7
Define an interface Shape within the operations package. This
interface includes two constants, side1 and side2, and a method
periRectangle() intended to compute the perimeter of a rectangle.
Write a java class Perimeter outside of the operations package
which implements the Shape interface. Write a main() method
designed to showcase the functionality of the program.
In the "train information.users " package, define a class named "userinfo" with properties such
as user id, user name, user address, and user contact number.
Create a Java program to demonstrate the use of packages. In the main() method of this
program, create objects of the “Table” and "userinfo" classes.
// Train.java - Inside the traininformation package package traininformation.users;
public class UserInfo {
package traininformation; String userID;
String userName;
public class Train { String userAddress;
String trainID; String userContactNumber;
String trainName; public UserInfo(String userID, String userName, String userAddress, String
String date; userContactNumber) {
String startingPoint; this.userID = userID;
String endingPoint; this.userName = userName;
this.userAddress = userAddress;
public Train(String trainID, String trainName, String this.userContactNumber = userContactNumber;
date, String startingPoint, String endingPoint) { }
this.trainID = trainID; public void print()
this.trainName = trainName; {
this.date = date; System.out.println("User Information:");
this.startingPoint = startingPoint; System.out.println("User ID: " + userID);
this.endingPoint = endingPoint; System.out.println("User Name: " +userName);
} System.out.println("userAddress: " +userAddress);
public void print() System.out.println("userContactNumber: " +userContactNumber);
{ }
} import traininformation.Train;
System.out.println("Train Information:");
import traininformation.users.UserInfo;
System.out.println("Train ID: " + trainID);
public class TrainMain {
System.out.println("Train Name: " +trainName);
public static void main(String[] args) {
System.out.println("Date: " +date);
Train train = new Train("12345", "Express", "2024-05-10", "City A", "City B");
System.out.println("Starting Point: " +startingPoint);
train.print();
System.out.println("Ending Point: " +endingPoint);
UserInfo userInfo = new UserInfo("001", "John Doe", "123 Main St", "123-456-7890
}
userInfo.print();
}
}}
Define a class Test1 which contains public members as a and b in package
Pack1. Define another class Test2 which contains public members as c and d
in package Pack2. Create a separate java file to write a program outside
those packages to find out and print the highest number among a, b, c , d.
package Pack1; import Pack1.Test1;
import Pack2.Test2; public static int getMax(int
public class Test1
a,int b,int c,int d)
{ public class Main {
public int a; {
public static void main(String[] args)
public int b; int max = a;
{
} Test1 obj1 = new Test1(); if (b > max) {
obj1.a = 5; max = b;
obj1.b = 7; }
package Pack2; if (c > max) {
Test2 obj2 = new Test2(); max = c;
public class Test2 obj2.c = 10;
{
}
obj2.d = 8;
public int c; if (d > max) {
public int d; int max = getMax(obj1.a, obj1.b, max = d;
} obj2.c, obj2.d); }
System.out.println("The highest return max;
number among a, b, c, and d is: " + max); }
} }