Java Module3 BCS306A
Java Module3 BCS306A
Java Module3 BCS306A
INHERITANCE, INTERFACES
Inheritance is the mechanism through which we can derive classes from other classes.
It is process of deriving features from parent class
The derived class is called as child class or the subclass or we can say the extended class and the
class from which we are deriving the subclass is called the base class or the parent class.
Inheritance supports code reusability: Child class can reuse the methods and fields of parent class.
To derive a class in java the keyword extends is used.
Types of Inheritance
1. Single level/Simple Inheritance
2. Multi level Inheritance
3. Multiple Inheritance (Java doesn’t support Multiple inheritance but we can achieve this through
Interface)
Output:
Value of x is 10
30
Multilevel Inheritance
When a sub class is derived from a derived class then this mechanism is known as the multi
level inheritance.
The derived class is called the subclass or child class for it's parent class and this parent class
works as the child class for it's just above(parent) class.
Multi level inheritance can go up to any number of level.
class A
{
int x=10;
void get1()
{
System.out.println(“value of x is”+x);
}
}
class B extends A
{
int y=20;
void get2()
{
System.out.println(x+y);
}
}
class C extends B
{
Dept. of CSE/ISE/AIDS/AIML/CSD BGSCET, Bangalore Page 2
int z=30;
void get3()
{
System.out.println(z+y);
}
}
class Main
{
public static void main(String[] args)
{
C obj=new C();
obj.get1();
obj.get2();
obj.get3();
} }
Output
Value of x is 10
30
50
super keyword
The super is java keyword. As the name suggest super is used to access the members of the
super class.
Uses of super keyword are
class vehicle
{
int maxspeed=200;
}
class car extends vehicle
{
int maxspeed =150;
void display()
{
System.out.println(maxspeed); // 150
System.out.println(super.maxspeed); //200
}
}
class Main
{
public static void main(String[] args)
{
car obj=new car();
obj.display();
class A
{
A()
{
System.out.println(“parent class constructor”);
}
}
class B extends A
{
B()
{
super(); // call parent class constructor
System.out,println(“child class constructor”);
}
}
class Main
{
public static void main(String[] args)
Method Overriding
Method overriding in java means a subclass method overriding a super class method.
Super class method should be non-static. Subclass uses extends keyword to extend the super class.
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
In the above program, the display() method is present in both the superclass and the subclass.
When we call display() using the obj object (object of the subclass), the method inside the
subclass B is called. This will display welcome as output.
If we want to access parent class display function, then use super keyword.
In the above program, derived class C is extended from the class B, class B is extended from
class A. When object of child class C is created the constructor of the base classes i.e A() and B() is
executed first followed by the constructor of the derived class C().
The above program creates one superclass mobile and it’s two subclasses samsung and apple. These
subclasses overrides display( ) method.
1. Inside the main() method, initially objects of classes are declared.
2. Now a reference of type mobile, called ref, is also declared.
3. Now we are assigning a reference to each type of object to ref, one-by-one, and uses that reference
to invoke display( ). As the output shows, the version of display( ) executed is determined by the type
of object being referred to at the time of the call.
i. Abstract classes
A class which is declared with the abstract keyword is known as an abstract class. It can have
abstract and non-abstract methods (method with the body). It is not possible to create objects of abstract
class, its properties needs to be extended to derived class.
syntax:
abstract class A{}
A method which is declared as abstract and does not have implementation is known as an abstract
method.
syntax:
abstract void display();//no method body and abstract
Interfaces
The interface in Java is a mechanism to achieve abstraction. There can be only variables and
abstract methods in the Java interface, not the method body.
It is used to achieve abstraction and multiple inheritances in Java using Interface.
class B implements In {
public void display(int p) {
System.out.println("p squared is " + (p*p));
}
}
public class Main {
public static void main(String args[]) {
A o=new A();
B ob = new B();
In i;
i=o;
i.display(4);
i = ob;
i.display(4);
}
}
Output:
4
16
In the above program, after creating objects of class A and B, a reference variable of interface In is created.
Then Assign objects to reference variable, based on reference variable assigned methods can be accessed.
class Main {
public static void main(String[] args)
{
Test.Yes obj;
Testing t = new Testing();
obj = t;
obj.show();
}
}
Output
show method of interface
Variables in Interfaces
An interface is a container of abstract methods and static final variables. The interface contains the
static final variables. The variables defined in an interface can not be modified by the class that
implements the interface, but it may use as it defined in the interface.
The variable in an interface is public, static, and final by default.
If any variable in an interface is defined without public, static, and final keywords then, the
compiler automatically adds the same.
interface In{
int a = 100;
//int b; // Error - must be initialised
}
public class A implements In{
public static void main(String[] args)
{
System.out.println(a);
// a = 150; //Error! Can not be modified
}
Following is an example:
interface A
{
void meth1();
}
interface B extends A
{
void meth2();
}
class MyClass implements B
{
public void meth1()
{
System.out.println("Implement meth1().");
}
public void meth2()
{
System.out.println("Implement meth2().");
}
}
class Main
{
public static void main(String[] args)
interface In{
default void say()
{
saySomething();
}
private void saySomething()
{
System.out.println("Hello... I'm private method");
}
}
class A implements In {
public static void main(String[] args) {
A obj=new A();
In ref;
ref=obj;
ref.say();
}
}
Output:
Hello... I'm private method