Java Unit 3
Java Unit 3
Java Unit 3
Inheritance
Method Overriding
Constructor Calling
Runtime Polymorphism
Usage of Final
Package
Interfaces
Inheritance
• Allows the creation of hierarchical classification
• To inherit a class , we use the extends keyword
• Can specify only one superclass for any
subclass that you create
• Does not support inheritance of multiple
superclass
• A class member that has been declared as
private will remain private to its class. It is not
accessible by any code outside its class,
including subclasses.
Example on Inheritance
• A company needs information about their
employees like their names, ages and salaries
– class employee is created.
• the company have also programmers and
database specialists – need to add
programming language and database tool
public class Employee { public class Employee {
String name;
int age;
String name; double salary;
int age; public void printData(){
double salary; System.out.println("name: " + name);
System.out.println("age: " + age);
String language;
System.out.println("salary: " + salary);
String databaseTool; } }
….
…. public class Programmer extends Employee {
String language;
public void printData(){
} super.printData();
System.out.println("language: " + language);
} }
• Example : Overidedemo.java
IS-A Relationship
• Inheritance tree – follows is-a relationship
• Ex : Triangle is a Shape
• Cat is-a Feline
Difference between Overloading and Overriding
Overloading Overriding
1 Whenever same method or Constructor is existing Whenever same method name is
multiple times within a class either with different existing multiple time in both base
number of parameter or with different type of and derived class with same
parameter or with different order of parameter is number of parameter or same
known as Overloading. type of parameter or same order
of parameters is known as
Overriding.
2 Arguments of method must be different at least Argument of method must be
arguments. same including order.
3 Method signature must be different. Method signature must be same.
4 Private, static and final methods can be overloaded. Private, static and final methods
can not be override.
5 Also known as compile time polymorphism or static Also known as run time
polymorphism or early binding. polymorphism or dynamic
polymorphism or late binding.
6 Overloading can be exhibited both are method and Overriding can be exhibited only
constructor level. at method label.
7 The scope of overloading is within the class. The scope of Overriding is base
class and derived class.
8 Overloading can be done at both static and non-static Overriding can be done only at
methods. non-static method.
9 For overloading methods return type may or may not For overriding method return type
be same. should be same.
Using super to call base class constructor
• class BoxWeight extends Box {
• double weight; // weight of box
• // initialize width, height, and depth using super()
• BoxWeight(double w, double h, double d, double
m) {
• super(w, h, d); // call superclass constructor
• weight = m;
• }
• }
• class MySuper {
• int a = 1;
•
• MySuper() {
• System.out.print("-a" + a);
• } }
•
• class MySub extends MySuper {
•
• MySub(int c) {
• System.out.print("-c" + c); - a1 –c2
• } } The subclass’s
• constructor
• class TestInheritance { implicitly calls
• public static void main(String[] args){ superclass’s
• MySub mySub = new MySub(2); no-argument
• } } constructor
Constructors Call
• When a class hierarchy is created, in what order
are the constructors for the classes that
make up the hierarchy executed ?
}
• What happens when the following program is compiled and run?
public class MySuper {
int b = 1;
}
class TestMyProgram {
public static void main(String[] args){ -C5 –b1
MySub mySub = new MySub(4);
mySub.myMethod(); } } Without this
-c4-b1
Some Classes
Should not
be instantiated
Abstract Class
• an abstract class cannot be directly instantiated
with the new operator.
• They are mainly used as super classes.
• It has one or more abstract methods.
• An abstract method is a method without a body,
which are not defined.
• Abstract methods can be overridden by
subclasses by specifying the abstract type
modifier.
Usage
• It is possible to instantiate a sub class that
extends an abstract class.
• It is not mandatory to have any abstract
methods inside an abstract class.
• It is allowed in Java to declare a subclass of an
abstract class also abstract
• Example :constructor.AbstractAreas.java
Using final with Inheritance
• The keyword final has three uses.
– Constant
– To Prevent Method overriding
– To Prevent Inheritance
Prevent Overriding
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Prevent Inheritance
• final class A {
• //...
• }
• // The following class is illegal.
• class B extends A { // ERROR! Can't subclass A
• //...
• }
Multilevel Inheritance
• Single base class + single derived class +
multiple intermediate base classes
class Faculty {
float total_sal=0,
salary=30000; }
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
Access Protection Example
package p1;
p1/Derived.java
C:\Users\Hai...!\Desktop\JAVA>javac p1\Derived.java
C:\Users\Hai...!\Desktop\JAVA>javac p1\SamePackage.java
C:\Users\Hai...!\Desktop\JAVA>javac p1\Demo.java
C:\Users\Hai...!\Desktop\JAVA>java p1.Demo
Access Protection Example
base constructor
n=1
n_pri = 2
n_pro = 3
n_pub = 4
base constructor
n=1
n_pri = 2
n_pro = 3
n_pub = 4
derived constructor
n=1
n_pro = 3
n_pub = 4
base constructor
n=1
n_pri = 2
n_pro = 3
n_pub = 4
same package constructor
n=1
n_pro = 3
n_pub = 4
Access Protection Example
p2/Protection2.java
package p2;
class Protection2 extends p1.Protection {
Protection2() {
// class or package only
// System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
Access Protection Example
package p2;
p2/OtherPackage.java
class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("other package constructor");
// class or package only
// System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
// class, subclass or package only
// System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
Access Protection Example
p2/Demo.java
// Demo package p2.
package p2;
// Instantiate the various classes in p2.
public class Demo {
public static void main(String args[]) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
Access Protection Example
C:\Users\Hai...!\Desktop\JAVA>javac p2\Protection2.java
C:\Users\Hai...!\Desktop\JAVA>javac p2\OtherPackage.java
C:\Users\Hai...!\Desktop\JAVA>javac p2\Demo.java
C:\Users\Hai...!\Desktop\JAVA>java p2.Demo
Access Protection Example
base constructor
n=1
n_pri = 2
n_pro = 3
n_pub = 4
n_pro = 3
n_pub = 4
base constructor
n=1
n_pri = 2
n_pro = 3
n_pub = 4
other package constructor
n_pub = 4
Importing Packages
package MyPack;
/* Now, the Balance class, its constructor, and its
show() method are public. This means that they can
be used by non-subclass code outside their package.
*/
public class Balance {
String name;
double bal;
Shape.java
Public class Circle implements Shape
{
private double radius;
public Circle(double r){ this.radius = r; }
@Override
public void draw()
{ System.out.println("Drawing Circle"); }
@Override
public double getArea()
{ return Math.PI*this.radius*this.radius; }
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Circle c = new Circle(5);
double da = c.getArea();
System.out.println("Area" + da);
}
• interface Bank{
• float rateOfInterest();
• }
B reference variable
• class SBI implements Bank{ points to SBI class and
• public float rateOfInterest(){return 9.15f;} so the output would be
9.15 f
• }
• class PNB implements Bank{
• public float rateOfInterest(){return 9.7f;}
• }
• class TestInterface2{
• public static void main(String[] args){
• Bank b=new SBI();
• System.out.println("ROI: "+b.rateOfInterest());
• }}
Extended Interfaces
One interface can inherit another by use of the public void meth2() {
keyword extends System.out.println("Implement meth2().");
// One interface can extend another. }
interface A {
void meth1(); public void meth3() {
System.out.println("Implement meth3().");
void meth2();
}
} }
// B now includes meth1() and meth2() -- it class IFExtend {
adds meth3(). public static void main(String arg[]) {
interface B extends A { MyClass ob = new MyClass();
void meth3();
ob.meth1();
}
ob.meth2();
// This class must implement all of A and B ob.meth3();
class MyClass implements B { }
public void meth1() { }
System.out.println("Implement meth1().");
All methods of interface A and B have to be
}
implemented
Wrapper Classes