Java Unit 2
Java Unit 2
19A05303T
COURSE CODE :
II
UNIT :
P LAKSHMI
PREPARED BY :
CONCEPTS:
• Inheritance Basics, Using Super, Creating Multilevel hierarchy,
1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
5. Hybrid inheritance
IS-A
Fig . 2.1
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating... COURSE: OOPJ UNIT: I1 Pg. 8
8
USING SUPER
super(arg-list);
}
COURSE: OOPJ UNIT: I1 Pg. 9
2. ACCESSING SUPER CLASS MEMBERS
//super.member (varible/method)
class A {
int i;
}
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
i = b; // i in B
super.i = a; // i in A
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
} OUT PUT;
}
class UseSuper { i in superclass: 1
public static void main(String args[]) { i in subclass: 2
B subOb = new B(1, 2);
subOb.show();
} }
COURSE: OOPJ UNIT: I1 Pg. 10
METHOD OVERRIDING
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in java.
Note:
Subclass is override the super class method
COURSE: OOPJ UNIT: I1 Pg. 11
NON OVERRIDING OVERRIDING
Dynamic method dispatch is important because this is how Java implements run-time
polymorphism.
Ex:
A obj=new B(); // A is the reference class for obj, the object is created for B
There is one special class, Object, defined by Java. All other classes are subclasses
of Object.
Object defines the following methods, which means that they are available in
every object
Subclass
Modifier Class Package World
public Y Y Y Y
protected Y Y Y N
no modifier
Y Y N N
(default)
private Y N N N
26
COURSE: OOPJ UNIT: I1 Pg. 26
A SIMPLE PACKAGE
package MyPack;
public class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
COURSE: OOPJ UNIT: I1 Pg. 27
class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++)
current[i].show();
} }
28
COURSE: OOPJ UNIT: I1 Pg. 28
PACKAGE IMPORTING :
Fig . 2.3
COURSE: OOPJ UNIT: I1 Pg. 33
GENERAL FORM OF TO CREATE AN INTERFACE:
variable declarations;
}
GENERAL FORM OF TO IMPLEMENT AN INTERFACE:
// definition of class
interface printable{
void print();
obj.print();
}
COURSE: OOPJ UNIT: I1 Pg. 35
MULTIPLE INHERITANCE IN JAVA BY INTERFACE
Fig . 2.4
Since Java 8, we can have method body in interface. But we need to make it default
method. Let's see an example:
File: TestInterfaceDefault.java
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle in interface");
public void drawRect(){System.out.println("drawing rectanglein class");
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Output:
drawing rectangle
default method
COURSE: OOPJ UNIT: I1 Pg. 39
Notice that variable d is declared to be of the interface type Drawable,
yet it was assigned an instance of Rectangle. Although d can be used to access the
draw and msg methods,
it cannot access any other members of the Client class. An interface reference
variable has knowledge only of the methods declared by its interface declaration.
Thus, d could not be used to access drawRect( ) since it is defined by Rectangle but
not Drawable.
MARKER OR TAGGED INTERFACE
An interface that have no member is known as marker or tagged interface.
For example: Serializable, Cloneable, Remote etc. They are used to provide some
essential information to the JVM so that JVM may perform some useful operation.
//How Serializable interface is written?
public interface Serializable
{
} COURSE: OOPJ UNIT: I1 Pg. 40
NESTED INTERFACE IN JAVA
An interface can have another interface i.e. known as nested interface. We will
learn it in detail in the nested classes chapter.
interface printable
void print();
interface MessagePrintable
void msg();
JDK 8 added another new capability to interface: the ability to define one or more
static methods.
Like static methods in a class, a static method defined by an interface can be
called independently of any object.
Thus, no implementation of the interface is necessary, and no instance of the
interface is required, in order to call a static method.
Instead, a static method is called by specifying the interface name, followed by a
period, followed by the method name.
Here is the general form:
InterfaceName.staticMethodName
For example:
int a, b;
interface Callback
void show() { {
void callback(int param);
System.out.println(a + " " + b);
Void show();
} }
//...
}
Here, the class Incomplete does not implement callback( ) and must be declared as
abstract. Any class that inherits Incomplete must implement callback( ) or be
declared abstract itself.
COURSE: OOPJ UNIT: I1 Pg. 44
VARIABLES IN INTERFACES
Interface variables are static because java interfaces cannot be instantiated on their
own. The value of the variable must be assigned in a static context in which no
instance exists.
The final modifier ensures the value assigned to the interface variable is a true
constant that cannot be re-assigned. In other words, interfaces can declare only
constants, not instance variables.
CONTENT BEYOND SYLLABUS
STATIC KEYWORD
class VarArgs3 {
for(int x : v)
System.out.println();
}
49
COURSE: OOPJ UNIT: I1 Pg. 49
static void vaTest(String msg, int ... v) {
msg + v.length +
for(int x : v)
System.out.println();
vaTest(1, 2, 3);