Inheritance, Interface and Package
Inheritance, Interface and Package
PACKAGE
INHERITANCE, INTERFACE AND PACKAGE
class person
{
String name;
int age;
void accept(String n,int a)
{
name=n;
age=a;
}
void display()
{
System.out.println("name--->"+name);
System.out.println("age--->"+age);
}
}
class employee extends person
{
String emp_designation;
float emp_salary;
void accept_emp(String d,float s)
{
emp_designation=d;
emp_salary=s;
}
void emp_dis()
{
INHERITANCE, INTERFACE AND PACKAGE
System.out.println("emp_designation-->"+emp_designation);
System.out.println("emp_salary-->"+emp_salary);
}
}
class single_demo
{
public static void main(String ar[])
{
employee e=new employee();
e.accept("ramesh",35);
e.display();
e.accept_emp("lecturer",35000.78f);
e.emp_dis();
}
}
}
class salary_calc extends salary_details
{
salary_calc(int id, String nm, String dpt, String j1,int bs)
{
super(id,nm,dpt,j1,bs);
}
public static void main(String args[])
{
salary_calc e1=new salary_calc(101,"abc","Sales","clerk",5000);
double gross_salary=e1.calc();
System.out.println("Empid :"+e1.empid);
System.out.println("Emp name :"+e1.ename);
System.out.println("Department :"+e1.dept);
System.out.println("Job :"+e1.job);
System.out.println("BAsic Salary :"+e1.basic_salary);
System.out.println("Gross salary :"+gross_salary);
}
}
Example:
class SingleLevelInheritanceParent {
int l;
INHERITANCE, INTERFACE AND PACKAGE
SingleLevelInheritanceParent(int l) {
this.l = l;
}
void area() {
int a = l*l;
System.out.println("Area of square :"+a);
}
}
class SingleLevelInheritance {
public static void main(String args[]) {
SingleLevelInheritanceChild cube = new
SingleLevelInheritanceChild(2);
cube.volume();
cube.area();
}
}
INHERITANCE, INTERFACE AND PACKAGE
}
String addition(String s1, String s2)
{
return s1 + s2;
}
double addition(double d1, double d2)
{
return d1 + d2;
}
}
class AddOperation
{
public static void main(String args[])
{
Sample sObj = new Sample();
System.out.println(sObj.addition(1,2));
System.out.println(sObj.addition("Hello ","World"));
System.out.println(sObj.addition(1.5,2.2));
}
}
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary>= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
public class Demo {
public static void main(String [] args) {
Salary s = new Salary("RAM", "Dadar", 3, 3600.00);
Employee e = new Salary("John ", "Thane", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--
");
e.mailCheck();
}
}
void show()
{
//definition of show
}
} //end of Box class
Class BoxWeight extends Box
{
BoxWeight()
{
}
void show() // method is overridden in derived
{
Super.show() // will call base class method
}
}
this.height = height;
this.depth = depth;
}
Final keywords
The keyword final has three uses. First, it can be used to create
the equivalent of a named constant.( in interface or class we use
final as shared constant or constant.) other two uses of final apply
to inheritance
MyClass(int t) {
total = t;
}
public void method1() {
int avg = total/strength;
System.out.println("Avg is "+avg);
}
public void method2() {
}
public static void main(String a[]) {
MyClass c = new MyClass(3600);
c.method1();
}
}
Need:
A java class can only have one super class. Therefore for
achieving multiple inheritance, that is in order for a java class to
get the properties of two parents, interface is used. Interface
defines a set of common behaviours. The classes implement the
interface, agree to these behaviours and provide their own
implementation to the behaviours.
Syntax:
interface InterfaceName {
int var1 = value;
int var2 = value;
public return_type methodname1(parameter_list) ;
public return_type methodname2(parameter_list) ;
}
Features:
Interface is defined using the keyword “interface”. Interface is
implicitly abstract. All the variables in the interface are by default
final and static. All the methods of the interface are implicitly public
and are undefined (or implicitly abstract). It is compulsory for the
subclass to define all the methods of an interface. If all the
INHERITANCE, INTERFACE AND PACKAGE
Syntax:
access interface InterfaceName
{
return_type method_name1(parameter list);
….
return_type method_nameN(parameter list);
type final-variable 1 = value1;
….
type final-variable N = value2;
}
Features:
Variable of an interface are explicitly declared final and static (as
constant) meaning that the implementing the class cannot change
them they must be initialize with a constant value all the variable
are implicitly public of the interface, itself, is declared as a public
Method declaration contains only a list of methods without
anybody statement and ends with a semicolon the method are,
essentially, abstract methods there can be default implementation
of any method specified within an interface each class that include
an interface must implement all of the method
INHERITANCE, INTERFACE AND PACKAGE
Need:
To achieve multiple Inheritance.
We can implement more than one Interface in the one class.
Methods can be implemented by one or more class.
Example:
interface sports
{
int sport_wt=5;
public void disp();
}
class Test
{
int roll_no;
String name;
int m1,m2;
Test(int r, String nm, int m11,int m12)
{
roll_no=r;
name=nm;
m1=m11;
m2=m12;
}
}
class Result extends Test implements sports
{
Result (int r, String nm, int m11,int m12)
{
super (r,nm,m11,m12);
}
public void disp()
{
System.out.println("Roll no : "+roll_no);
System.out.println("Name : "+name);
System.out.println("sub1 : "+m1);
System.out.println("sub2 : "+m2);
System.out.println("sport_wt : "+sport_wt);
int t=m1+m2+sport_wt;
INHERITANCE, INTERFACE AND PACKAGE
System.out.println("total : "+t);
}
public static void main(String args[])
{
Result r= new Result(101,"abc",75,75);
r.disp();
}
}
import java.io.*;
class Student
{
String name;
int roll_no;
double m1, m2;
Student(String name, int roll_no, double m1, double m2)
{
this.name = name;
this.roll_no = roll_no;
this.m1 = m1;
this.m2 = m2;
}
}
interface exam {
public void per_cal();
}
class result extends Student implements exam
{
double per;
result(String n, int r, double m1, double m2)
{
INHERITANCE, INTERFACE AND PACKAGE
super(n,r,m1,m2);
}
public void per_cal()
{
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
}
void display()
{
System.out.println("The name of the student is"+name);
System.out.println("The roll no of the student is"+roll_no);
per_cal();
}
public static void main(String args[])
{
BufferedReader bin = new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.println("Enter name, roll no mark1 and mark 2 of
the student");
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
result r = new result(n,rn,m1,m2);
r.display();
} catch(Exception e)
{
System.out.println("Exception caught"+e);
}
}
}
INHERITANCE, INTERFACE AND PACKAGE
interface gross
{
int ta=1000;
int da=4000;
public void gross_sal();
}
class employee
{
String name="Abc";
int basic_sal=8000;
}
class salary extends employee implements gross
{
int hra;
int total=0;
salary(int h)
{
hra=h;
}
public void gross_sal()
{
total=basic_sal+ta+da+hra;
}
void disp_sal()
{
INHERITANCE, INTERFACE AND PACKAGE
gross_sal();
System.out.println("Name :"+name);
System.out.println("Total salary :"+total);
}
public static void main(String args[])
{
salary s = new salary(3000);
s.disp_sal();
}
}
}
interface exam {
public void per_cal();
}
class result extends Student implements exam
{
double per;
result(String n, int r, double m1, double m2)
{
super(n,r,m1,m2);
}
public void per_cal()
{
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
}
void display()
{
System.out.println("The name of the student is"+name);
System.out.println("The roll no of the student is"+roll_no);
per_cal();
}
public static void main(String args[])
{
BufferedReader bin = new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.println("Enter name, roll no mark1 and mark 2 of the
student");
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
result r = new result(n,rn,m1,m2);
r.display();
} catch(Exception e)
{
INHERITANCE, INTERFACE AND PACKAGE
System.out.println("Exception caught"+e);
}
}
}
}
class result extends Student implements exam
{
double per;
result(String n, int r, double m1, double m2)
{
super(n,r,m1,m2);
}
public void per_cal()
{
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
}
void display()
{
System.out.println("The name of the student is"+name);
System.out.println("The roll no of the student is"+roll_no);
per_cal();
}
public static void main(String args[])
{
BufferedReader bin = new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.println("Enter name, roll no mark1 and mark 2 of the
student");
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
result r = new result(n,rn,m1,m2);
r.display();
}
catch(Exception e)
{
System.out.println("Exception caught"+e);
}
INHERITANCE, INTERFACE AND PACKAGE
}
}
Java cannot have more than one super class. Therefore, interface
is used to support multiple inheritance in java. Interface specifies
what a class must do but not how it is done.
obj.show();
}
}
26) State any four system packages along with their use.
(Winter 15) 4 marks
a) java.lang - language support classes. These are classes that
java compiler itself uses and therefore they are automatically
imported. They include classes for primitive types, strings,
math functions, threads and exceptions
b) java.util – language utility classes such as vectors, hash
tables, random numbers, date etc.
c) java.io – input/output support classes. They provide facilities
for the input and output of data
d) java.awt – set of classes for implementing graphical user
interface. They include classes for windows, buttons, lists,
menus and so on
e) java.net – classes for networking. They include classes for
communicating with local computers as well as with internet
servers.
f) java.applet – classes for creating and implementing applets.
27) List any four built-in packages from Java API along with
their use. (Summer 17) 4 marks
a) java.lang: Contains Language support classes which are
used by Java compiler during compilation of program
b) java.util: Contains language utility classes such as vectors,
hash tables, random numbers, date etc.
c) java.io: Contains I/O support classes which provide facility
for input and output of data.
d) java.awt: Contains a set of classes for implementing
graphical user interface.
e) java.aplet: Contains classes for creating and implementing
applets.
f) java.sql: Contains classes for database connectivity.
g) java.net: Contains classes for networking.
INHERITANCE, INTERFACE AND PACKAGE
28) Enlist any four built in packages in java API with atleast
two class name from each package (Winter 17) 4 marks
a) java.lang - language support classes. These are classes that
java compiler itself uses and therefore they are automatically
imported. They include classes for primitive types, strings,
math functions, threads and exceptions
classes: Thread, String
b) java.util – language utility classes such as vectors, hash
tables, random numbers, date etc
classes: Date,Collection,Vector
c) java.io – input/output support classes. They provide facilities
for the input and output of data
classes: FileReader, FileWriter
d) java.awt – set of classes for implementing graphical user
interface. They include classes for windows, buttons, lists,
menus and so on
classes: Button,Label
e) java.net – classes for networking. They include classes for
communicating with local computers as well as with internet
servers
classes: Socket,URL
Syntax:
To access package In a Java source file, import statements occur
immediately following the package statement (if it exists) and
before any class definitions.
Syntax:
import pkg1[.pkg2].(classname|*);
Example:
package1:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h)); }
}
}
Source file:
import package1.Box;
class VolumeDemo
{
public static void main(String args[])
{
Box b=new Box();
b.display();
}
INHERITANCE, INTERFACE AND PACKAGE
import myPack.*;
public class MyClassExample{
public static void main(String a[]) {
Myclass c= new Myclass();
INHERITANCE, INTERFACE AND PACKAGE
c.myMethod();
}
}
using packagename.*
If you use package.* then all the classes and interfaces of
this package will be accessible but not subpackages. The
import keyword is used to make the classes and interfaces of
another package
INHERITANCE, INTERFACE AND PACKAGE
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Using packagename.classname
If you import packagename.classname then only declared
class of this package will be accessible.
Eg.
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
{
length=l;
bredth=b;
return(length*bredth);
}
}
import Area.Rectangle;
class RectArea
{
public static void main(String args[])
{
Rectangle r=new Rectangle( );
double area=r.rect(10,5);
System.out.println(“Area of rectangle= “+area);
}
}
Example:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h)); }
}
}
Source file:
import package1.Box;
class VolumeDemo
{
public static void main(String args[])
{
Box b=new Box();
b.display();
}
}