BCS306A Module-3
BCS306A Module-3
The idea behind inheritance in Java is that you can create new
classes that are built upon existing classes. When you inherit from an
existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class
also.
The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is to
increase the functionality.
class Employee{
float salary=40000;
int bonus=10000;
public static void main(String
Programmer();
}
Programmer salary is:40000.0
Bonus of programmer is:10000
File: TestInheritance.java
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
class TestInheritance{
d.bark();
d.eat();
}}
Output:
barking...
eating...
File: TestInheritance2.java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
void weep(){System.out.println("weeping...");}
class TestInheritance2{
BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
File: TestInheritance3.java
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
class TestInheritance3{
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing... eating...
Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have
same method or different, there will be compile time error.
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
}
Compile Time Error
class Animal{
String color="white";
Animal{ String
color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
class TestSuper1{
d.printColor();
}}
Output:
black
white
class Animal{
void eat(){System.out.println("eating...");}
void eat(){System.out.println("eating
{System.out.println("barking...");} void
work(){
super.eat();
bark();
}
}
class TestSuper2{
d.work();
}}
Output:
eating...
barking...
In the above example Animal and Dog both classes have eat()
method if we call eat() method from Dog class, it will call the eat()
method of Dog class by default because priority is given to local.
{System.out.println("animal is created");}
Animal{ Dog(){
super();
System.out.println("dog is created");
class TestSuper3{
Output:
animal is created
dog is created
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable
that have no value it is called blank final variable or uninitialized final
variable. It can be initialized in the constructor only. The blank
final variable can be static also which will be initialized in the static
block only. We will have detailed learning of these. Let's first learn the
basics of final keyword.
class Bike9{
void run(){
speedlimit=4
00;
obj.run();
}//end of class
Output:Compile Time Error
Honda(); honda.run();
}
}
Output:Compile Time Error
Honda1(); honda.run();
}
Output:Compile Time Erro
class Bike{
new Honda2().run();
}
Output:running...
Q) What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is
known as blank final variable.
int id;
String name;
...
}
Que) Can we initialize blank final variable?
class Bike10{
Bike10(){
speedlimit=7
0;
System.out.println(speedlimit);
new Bike10();
}
Output: 70
static blank final variable
A static final variable that is not initialized at the time of
declaration is known as static blank final variable. It can
be initialized only in static block.
static{ data=50;}
main(String args[])
{ System.out.printl
n(A.data);
If you declare any parameter as final, you cannot change the value
of it.
class Bike11{
n=n+2;//can't be
changed as n is
final n*n*n;
main(String
args[]){ Bike11
b=new Bike11();
b.cube(5);
}
Output: Compile Time Error
Q) Can we declare a constructor final?
No, because constructor is never inherited.
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be
only abstract methods in the Java interface, not method body. It is used to
achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.
Java Interface also represents the IS-A
relationship. It cannot be instantiated just like
the abstract class.
Declaring an interface
An interface is declared by using the interface keyword. It provides total
abstraction; means all the methods in an interface are declared with the
empty body, and all the fields are public, static and final by default. A class
that implements an interface must implement all the methods declared in the
interface.
Syntax:
interface interface_name
{
// declare constant fields
// declare methods that abstract
// by default.
}
Example
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("Hello");
}
}
class MB
{
public static void main(String args[])
{
B obj = new
B();
obj.display();
}
}
Output:
Hello
Interface Example:
In this example, the interface A has only one method. Its implementation is
provided by B and C classes. In a real scenario, an interface is defined by
someone else, but its implementation is provided by different implementation
providers. Moreover, it is used by someone else. The implementation part is
hidden by the user who uses the interface.
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("Display method in B class");
}
}
class C implements B
{
public void display()
{
System.out.println("display method in C class");
}
}
class MainClass
{
public static void main(String args[])
{
D obj=new
D(); obj.draw();
}
}
interface A
{
void display();
}
interface B
{
void show();
}
class C implements A,B
{
public void display()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
}
class MainClass
{
public static void main(String args[])
{
C obj = new
C();
obj.display();
obj.show();
}
}
Output:
Hello
Welcom
e
Interface inheritance
A class implements an interface, but one interface extends another
interface. interface A
{
void display();
}
interface B extends A
{
void show();
}
class C implements B
{
public void display()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
}
Class MainClass
{
public static void main(String args[])
{
C obj = new
C();
obj.display();
obj.show();
}
}
Output:
Hello
Welcome
Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change
the body of the method.
Example
abstract class
{ }
Abstract Method
A method which is declared as abstract and does not have implementation is
known as an abstract method.
Example
abstract void display(); //no method body and abstract
1) Abstract class can have abstract Interface can have only abstract methods.
and non- abstract methods. Since Java 8, it can have default and static
methods also.
2) Abstract class doesn't support Interface supports multiple inheritance.
multiple inheritance.
3) Abstract class can have final, non- Interface has only static and final variables.
final, static and non-static variables.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }