Download Arrays

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Interfaces:Multiple Inheritance
- A Java class cannot be a subclass of more than one superclass,it can implement
more than one interface.
- Definition
o Interface is basically a kind of class that contains only abstract methods
and final fields.
o i.e no code for methods and data fields contains only constants.
o Using the keyword interface
-
Syntax for defining an interface
interface interfacename
{
Variable declaration
Method declaration
}
e.g
interface Area
{
final float PI=3.141;
float compute(float x,float y);
}
Extending Interfaces
-Interfaces can be extended using the keyword extends
e.g
interface name2 extends name1
interface Volume extends Area
{
{
Variable declaration
float volume(float x,float y,float z);
Method declaration
void display();
}
}
Implementing Interfaces
- Interfaces are used as “superclasses” whose properties are inherited by classes.
- Syntax for Implementing an interface by creating a class
class classname implements interfacename
{
Data Members
Methods
}
e.g
class C implements B
{
Data Members
Methods
}
Rules or conditions for using interfaces
1. Classes inherit from interface using the keyword implements.
2. Classes that implement an interface should redefine all the methods of the
interfaces with visibility modifier public.
3. Declaration of objects for Interface but cannot be instantiated.
Note: if a Class that implements an interface does not redefine all the methods of the
interface, then the class becomes an abstract class and cannot be instantiated.
e.g//InterfaceTest.java
interface Area
{
final static float PI=3.14f;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return(PI*x*x);
}
}
class interfaceTest
{
public static void main(String args[])
{
Rectangle rect =new Rectangle ();
Circle cir=new Circle ();
Area A1;
//Interface object
A1=rect;
//area A1 refers to rect object
System.out.println(“Area of Rectangle=” + A1.compute(10,20));
A1=cir;
//area A1 refers to rect object
System.out.println(“Area of Circle=” + A1.compute(10,20));
}
}
Multiple Inheritance (several super classes)
Super class
B
Interface
implements
A
extends
C
- Syntax for Implementing multiple inheritance
class subclassname extends superclassname implements interface1,interface2,…….
{
Data Members
Methods
}
class student
{
int rollnumber;
void getNumber(int n)
{
rollNumber=n;
}
void putNumber()
{
System.out.println(“Roll no:+ rollNumber);
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println(“Marks obtained”);
System.out.println(“Part1=”+ part1);
System.out.println(“Part2=”+ part2);
}
interface Sports
{
float sportwt=6.0f;
void putwt()
}
class Results extends Test implements Sports
{
float total;
public void putwt()
{
System.out.println(“Sports wt:”+sportwt);
}
void display()
{
Total=part1+part2+sportwt;
putNumber();
putMarks();
putwt();
System.out.println(“Total score =” + total);
}}
class Hybrid
{
public static void main(String args[])
{
Results student1=new Results();
student1.getNumber(1234);
student1.getMarks(27.5f,33.0f);
student1.display();
}
}
1.
a. Write an interface called MyOwnInterface, which has the
following method (AddressInterface getAddress();)
b. The AddressInterface is a Java interface that has the following
methods.
i. int getStreetNumber();
ii. void setStreetNumber(int streetNumber);
iii. String getStreetName();
iv. void setStreetName(String streetName);
v. String getCountry();
vi. void setCountry(String country);
c. Write AddressImpl class that implements AddressInterface
d. Make the Person class to implement MyOwnInterface.
e. Initialize a Person object with proper data and display it.
a. Create a super class called Car. The Car class has the following
fields and methods. (int speed;double regularPrice;String color;
double getSalePrice();)
b. Create a subclass of Car class and name it as Truck. The Truck
class has the following fields and methods.(int weight; double
getSalePrice(); // If weight > 2000, 10% discount. Otherwise, 20%
discount.
c. Create a subclass of Car class and name it as Ford. The Ford
class has the following fields and methods (int year; int
manufacturerDiscount;double getSalePrice(); // From the sale price
computed from Car class, subtract the manufacturerDiscount.)
d. Create a subclass of Car class and name it as Sedan. The Sedan
class has the following fields and methods. (int length; double
getSalePrice(); // If length > 20 feet, 5% discount, Otherwise, 10%
discount.)
e. Create MyOwnAutoShop class which contains the main()
method. Perform the following within the main() method.
i. Create an instance of Sedan class and initialize all the fields
with appropriate values. Use super(...) method in the
constructor for initializing the fields of the super class.
ii. Create two instances of the Ford class and initialize all the
fields with appropriate values. Use super(...) method in the
constructor for initializing the fields of the super class.
iii. Create an instance of Car class and and initialize all the
fields with appropriate values.
iv. Display the sale prices of all instance.
2.
Write a class AvailableAppt that implements the Appointment for appointments for
which folks might sign up for meetings.
Implement classes WalkInAppt, SignUpAppt, and RestrictedAppt. These classes
may inherit the printing method from AvailableAppt. Making appointments should
proceed according to the following table.
Class
Owner
Logged-in User
Can make
WalkInAppt
Told to drop in
changes
Can sign up if comment
field is null or says
Can make
SignUpAppt
"office hours";
changes
otherwise told time
taken
Can make Told e-mail request will
RestrictedAppt
changes be sent
Unknown User
Told to drop in
Told e-mail request will be
sent if comment field is null
or says "office hours";
otherwise told time taken
Told e-mail request will be
sent
Packages
-Way to group a related class and interface into one unit
-To resolve the name conflicts between class names
-Benefits
1. Classes in other packages of other programs can be easily reused.
2. It provides a way to hide classes from other programs.
3. In packages, classes can be unique compared with classes in other packages.
4. In packages,two classes have the same name it can be referred by full qualified
name.
5. It provide a way to separate “design” from “coding”. i.e design classes and then
implement for methods.
-Creating package
-Steps for creating our own package:
1. Declare the package at the beginning of a file as
package packagename;
e.g package computation;
2. Define the class that is to be put in the package and declare it public
3. Create a subdirectory with name that match the package name under the directory
where the main source files are stored.
4. Store the listing as the classname.java file in the subdirectory created.
5. Compile the file. This creates .class file in the subdirectory.
-Accessing a package
Format:
import package1 [.package2] [.package3]….. .classname;
e.g import java.lang.String;
import java.lang.*;
Write a java program using two packages namely NUMERIC and TEXT for computing
operation of related context. Create a main method class called as TESTPKG class to test
these packages.
e.g
class student
{
int rollnumber;
void getNumber(int n)
{
rollNumber=n;
}
Void putNumber()
{
System.out.println(“Roll no:+ rollNumber);
}
class Test extends Student
{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println(“Marks obtained”);
System.out.println(“Part1=”+ part1);
System.out.println(“Part2=”+ part2);
}
interface Sports
{
float sportwt=6.0f;
void putwt()
}