Unit 2 P1
Unit 2 P1
Presented By:
Deepak Kumar Sharma
Asst. Professor
SoCS UPES Dehradun
UNIT-II
WHY INHERITANCE?
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
class Employee{
int id;
String name;
Address address;//Address is a class
...
}
2.The method must have the same parameter as in the parent class.
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper{
public static void main(String args[]){ Output:
Dog d=new Dog(); Black
d.printColor(); white
}}
Deepak Sharma, Asst. Professor UPES Dehradun
eating... barking...
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper{
public static void main(String args[]){ Output:
Dog d=new Dog(); animal is created
}} dog is created
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
System.out.println("dog is created");
}
}
class TestSuper4{
public static void main(String args[]){
Dog d=new Dog(); Output:
}} animal is created
dog is created
class Bike{
int speed;
Bike(){System.out.println("speed is "+speed);}
{speed=100;}
Output:
speed is 100
public static void main(String args[]){
speed is 100
Bike b1=new Bike();
Bike b2=new Bike();
}
}
Deepak Sharma, Asst. Professor UPES Dehradun
Instance initializer block
It run each time when object of the class is created.
There are three places in java where you can perform operations:
• method
• constructor
• block
Bike8(){System.out.println("constructor is invoked");}
class Bike{
final void run(){System.out.println("running");}
}
class A{
static final int data;//static blank final variable
static{ data=50;}
public static void main(String args[]){
System.out.println(A.data);
}
}
class A{}
class B extends A{}
A a=new B();//upcasting
class Bike{
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");}
class Bike{
int speedlimit=90;
} Output:
class Honda3 extends Bike{
int speedlimit=150; 90
class Animal{}
class Dog extends Animal{
public static void main(String args[]){
Dog d1=new Dog();
} }
class Dog{
private void eat(){System.out.println("dog is eating...");}
Note:
public static void main(String args[]){ Dog extends Animal therefore
Dog d=new Dog(); object of Dog can be referred by
System.out.println(d instanceof Animal);//true either Dog or Animal class.
}
}
class Animal { }
class Dog4 extends Animal {
static void method(Animal a) {
Dog4 d=(Dog4)a;//downcasting
System.out.println("ok downcasting performed");
}
public static void main (String [] args) {
Output:
Animal a=new Dog4();
ok downcasting performed
Dog4.method(a);
}
}
• Interface (100%)
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
}}
Output:
Hello
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle(); Output:
d.draw(); drawing rectangle
System.out.println(Drawable.cube(3)); 27
}
4) Abstract class can provide the implementation Interface can't provide the implementation of
of interface. abstract class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare interface.
class.
6) An abstract class can extend another Java class An interface can extend another Java interface only.
and implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members Members of a Java interface are public by default.
like private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Now, you can't change the value of the college data member which is “UPES".
s.setCollege(“UPES DDN");//will render compile time error
Now, you can't get the value of the college, you can only change the value of
college data member.
Output: