0% found this document useful (0 votes)
0 views70 pages

Unit-2 Java Array,Inheritance,Interface

The document covers Object-Oriented Concepts in Java, focusing on arrays, inheritance, and interfaces. It explains how to declare and manipulate one-dimensional and two-dimensional arrays, as well as the principles of inheritance, including the use of the 'extends' keyword and the relationship between parent and child classes. Additionally, it discusses command line arguments and the differences between interfaces and abstract classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views70 pages

Unit-2 Java Array,Inheritance,Interface

The document covers Object-Oriented Concepts in Java, focusing on arrays, inheritance, and interfaces. It explains how to declare and manipulate one-dimensional and two-dimensional arrays, as well as the principles of inheritance, including the use of the 'extends' keyword and the relationship between parent and child classes. Additionally, it discusses command line arguments and the differences between interfaces and abstract classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object

Oriented
Concepts
With Java

Prof. Parthavi Varan


Unit-2 Array, Inheritance, Interface

Java Array
• One dimensional, Two dimensional
• Using For...each with array Java Interface
• Passing arrays to methods and • Variables in Interface
returning arrays from method • Extending Interfaces

• command line arguments • Interface vs Abstract class

Java Inheritance
• Deriving classes using extends keyword
• Overriding Method
• super keyword, final keyword
• Abstract class

Prof. Parthavi Varan


Java Arrays : One dimensional Array

Prof. Parthavi Varan


Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets [ ] :

1) Declare one Dimensional Array :-

Method-1:- declare array

• User can declare array first after allocate memory to it that is totally valid. After that size allocation means memory
allocation done using new keyword.

• Syntax :-

datatype[] arrayName;
Or
class Main class Main
datatype arrayName[];
{ {
public static void main(String[] args) public static void main(String[] args)
• Example :-
{ {
int[] arr; int arr[];
arr=new int[5]; arr=new int[5];
} }
} }
Prof. Parthavi Varan
Method-2:- declaration + memory allocation

• In this method array declaration and memory allocation done by writing single line.
• Syntax :-

datatype arrayName[]=new datatype[size];


Or
datatype[] arrayName=new datatype[size];

• Example :-

class
Class Main
Main class Main
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int[] arr=new int[5]; int arr[]=new int[5];
} }
} }

Prof. Parthavi Varan


Method-3 : declare array with initialization

• When initialization done with declaration then size is automatically decide by javac [Link] no need to define
size.
• Syntax:
datatype arrayName[]={value1,value2,value3};
Or
Datatype arrayName[]=new datatype[] { value1,value2,value3};

• Example :
class Main
{
public static void main(String[] args)
{
String sname[]=new String[] {“mahi”,”siya”,”jemika”};
}
}

class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
}
} Prof. Parthavi Varan
2) Access one Dimensional Array element :-

You can access an array element by referring to the index number.


Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
[Link](sname[1]);
}
}
3) Change one Dimensional Array element :-

To change the value of a specific element, refer to the index number:


Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
sname[0]=“sakshi”;
[Link](sname[0]);
}
}
Prof. Parthavi Varan
4) Array Length

To find out how many elements an array has, use the length property:
Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
[Link]([Link]);
}
}
5) Access All elements of Array
Using For Loop
Access all the elements of array,loop is required so use for loop to access all element of array.
Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
for(int i=0;i<[Link];i++)
{
[Link](sname[i]);
}
} Prof. Parthavi Varan
}
Using For Each Loop
Access all the elements of array,loop is required so use for each loop to access all element of array. for each loop is easy to
write compare to for [Link] for each loop one variable is declared which take reference of all value of array variable.
Syntax :-
for (type variable : arrayName)
{
//code
}

Example :-

class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
for(String s : sname)
{
[Link](s);
}
}
}

Prof. Parthavi Varan


Java Arrays : Two dimensional Array

Prof. Parthavi Varan


1) Declare Two Dimensional Array :-

Method-1:- declare array

• User can declare array first after allocate memory to it that


is totally valid. After that size allocation means memory
allocation done using new keyword.

• Syntax :-

datatype[][] arrayName;
Or
datatype arrayName[][];

• Example :-
class Main class Main
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int[][] arr; int arr[][];
arr=new int[5][2]; arr=new int[5][2];
} }
} }
Prof. Parthavi Varan
Method-2:- declaration + memory allocation

• In this method array declaration and memory allocation done by writing single line.
• Syntax :-

datatype arrayName[][]=new datatype[rowsize][columnsize];


Or
datatype[][] arrayName=new datatype[rowsize][columnsize];

• Example :-

class Main class Main


{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int[][] arr=new int[5][2]; int arr[][]=new int[5][2];
} }
} }

Prof. Parthavi Varan


Method-3 : declare array with initialization

• When initialization done with declaration then size is automatically decide by javac [Link] no need to define
size.
• Syntax:
datatype arrayName[][]={{value1,value2},{value3,value4}};
Or
Datatype arrayName[][]=new datatype[][] { {value1,value2},{value3,value4}};

• Example :
class Main
{
public static void main(String[] args)
{
int arr[][]=new int[][] { {2,3,4},{5,6,7,8} };
}
}

class Main
{
public static void main(String[] args)
{
Int arr[][]={ {2,3,4} , {5,6,7,8} };
}
} Prof. Parthavi Varan
2) Access Two Dimensional Array element :-

You can access an array element by referring to the row number and column number.
Example :-
class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
[Link](arr[1][1]);
}
}
3) Change Two Dimensional Array element :-

To change the value of a specific element, refer to the row number and column number.
Example :- class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
arr[1][1]=120;
[Link](arr[1][1]);

}
} Prof. Parthavi Varan
4) Array Length

To find out how many elements an array has, use the length [Link] gives number of [Link] find number of
column write arr[row].length it gives number of column in particular row suppose arr[0].length gives 3 because in 1 row 3
column are there.
Example :-
class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
[Link]([Link] + arr[0].length);
}
}

5) Access All elements of Array


Using For Loop
Access all the elements of array,loop is required so use for loop to access all element of array.
Example :-

Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
[Link](arr[i][j]);
}
}

}
}

Using For Each Loop


Access all the elements of array,loop is required so use for each loop to access all element of array. for each loop is easy to
write compare to for [Link] for each loop one variable is declared which take reference of all value of array variable.
Syntax :-
for (type variable : arrayName)
{
//code
}

Example :- Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int row[] : arr)
{
for(int x : row)
{
[Link](x);
}
}

}
}

Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row

Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int row[] : arr)
{
for(int x : row)
{
[Link](x);
}
}

}
}

Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row

Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int row[] : arr)
{
for(int x : row)
{
[Link](x);
}
}

}
}

Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row

Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int row[] : arr)
{
for(int x : row)
{
[Link](x);
}
}

}
}

Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row

Prof. Parthavi Varan


class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
for(int row[] : arr)
{
for(int x : row)
{
[Link](x);
}
}

}
}

Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row

Prof. Parthavi Varan


Passing arrays to methods and returning arrays from method

Prof. Parthavi Varan


You can pass an array to a method just like a normal variable. In method definition write datatype followed by [] and array name.

Example:1 Passing 1D Array to method

class Main
{
static void display(int[] arr)
{
for(int x : arr)
{
[Link](x);
}
}

public static void main(String args[])


{
int[] a = {10,20,30,40};
display(a);
}
}

Prof. Parthavi Varan


Example:2 Passing 2D Array to Method

class Main
{
static void display(int[][] arr)
{
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
[Link](arr[i][j]);
}
}
}

public static void main(String args[])


{
int[][] a = { {1,2,3},{4,5,6} };
display(a);
}
}

Prof. Parthavi Varan


Returning Array from Method

A method can also return an array.

class Main
{
static int[] getArray()
{
int[] arr = {10,20,30};
return arr;
}

public static void main(String args[])


{
int[] a;
a = getArray();
for(int x : a)
{
[Link](x);
}
}
}
Prof. Parthavi Varan
Command line arguments

Prof. Parthavi Varan


Command line arguments are values passed while running program.
They are stored inside : String args[] inside main() method.
Example 1 :-

class Main
{
public static void main(String args[])
{
for(int i=0;i<[Link];i++)
{
[Link](args[i]);
}
}
}

Output :-

D:\Lectures\BCA Sem-3\Java Theory\Practical> javac [Link]


D:\Lectures\BCA Sem-3\Java Theory\Practical>java Main parthavi varan 30
parthavi
Varan
30
Prof. Parthavi Varan
Example 2 :-
class Main
{
public static void main(String args[])
{
[Link](“first arguments”+ args[0]);
[Link](“first arguments”+ args[2]);
}
}

Output :-

D:\Lectures\BCA Sem-3\Java Theory\Practical> javac [Link]


D:\Lectures\BCA Sem-3\Java Theory\Practical>java Main parthavi varan 30
Parthavi
30

Prof. Parthavi Varan


Inheritance

Prof. Parthavi Varan


Inheritance is an important feature of Java that allows one class to acquire the properties and methods of another class.

It helps in:
• Code reusability
• Reducing duplicate code
• Creating parent-child relationships between classes

Inheritance means creating a new class from an existing class.


• Existing class → Parent class / Superclass
• New class → Child class / Subclass

The child class can use:


• Variables
• Methods
• constructors (through super())of the parent class.
Syntax of Inheritance
class Parent
{
// parent properties
}
class Child extends Parent
{
// child properties
}
Prof. Parthavi Varan
Here, extends keyword is used for inheritance.
Example :-
class Animal
{
void sound()
{
[Link]("Animal makes sound");
}
}

class Dog extends Animal


{
void bark()
{
[Link]("Dog barks");
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
[Link]();
[Link]();
}
}
Prof. Parthavi Varan
Types of Inheritance in Java

1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance

1. Single Inheritance
One child class inherits one parent class.
class Animal
{
void sound()
{
[Link]("Animal makes sound");
}
}

class Dog extends Animal


{
void bark()
{
[Link]("Dog barks");
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
[Link]();
[Link]();
}
} Prof. Parthavi Varan
2. Multilevel Inheritance

In multilevel inheritance:one class inherits another class,then another class inherits that inherited class.

class Animal
{

void eat()
{
[Link]("Animal eats");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("Dog barks");
}
}
class Puppy extends Dog
{
void weep()
{
[Link]("Puppy weeps");
}
}
public class Main
{
public static void main(String[] args)
{
Puppy p = new Puppy(); Prof. Parthavi Varan
[Link]();
3. Hierarchical Inheritance

In hierarchical inheritance: multiple child classes inherit one parent class.


class Animal
{
void eat()
{
[Link]("Animal eats");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("Dog barks");
}
}
class Cat extends Animal
{
void meow()
{
[Link]("Cat meows");
}
}
public class Main
{

public static void main(String[] args)


{
Dog d = new Dog();
[Link]();
Prof. Parthavi Varan
[Link]();
4. Multiple Inheritance

One child inherits multiple parents.


Java does NOT support multiple inheritance using classes because it creates ambiguity problem (Diamond Problem).
A B
\/
C

❌ Not allowed:

class A { }
class B { }

// ERROR
class C extends A, B { }

Prof. Parthavi Varan


5. Hybrid Inheritance

Combination of two or more inheritance types.


Java does not support hybrid inheritance using classes [Link] it can be achieved using interfaces.

Inheritance Type Supported in Java Classes?


Single Yes
Multilevel Yes
Hierarchical Yes
Multiple No (with classes)
Hybrid Partial supported

Prof. Parthavi Varan


Overriding Method

Prof. Parthavi Varan


Runtime polymorphism is an OOP concept where an overridden method call is resolved during program execution based on the
actual object type.

Runtime polymorphism means:


“Method call is decided during program execution (runtime).”

Prof. Parthavi Varan


▪ Example 1 ☺ PS D:\Lectures\BCA Sem-3\Java Theory\Practical> javac
class Animal Runtime_polymorphism.java
{ PS D:\Lectures\BCA Sem-3\Java Theory\Practical>java
public void animalSound()
{
Runtime _polymorphism
[Link]("The animal makes a sound"); The animal makes a sound
} The pig says: wee wee
}

class Pig extends Animal


{
public void animalSound()
{
[Link]("The pig says: wee wee");
}
}

public class Runtime_polymorphism


{
public static void main(String[] args)
{
Animal myAnimal = new Animal();
Animal myPig = new Pig();
[Link]();
[Link]();
}
}

Prof. Parthavi Varan


What is difference between Animal myPig=new Pig() and Pig myPig=new Pig()?

The difference is in: class Animal


reference type {
and object type void eat()
{
[Link]("Animal eats");
1. Pig myPig=new Pig() }
}
Here: class Pig extends Animal
{
Part Type void Sound()
{
Reference Type Pig [Link](“Wee wee");
}
Object Type Pig }
public class Main
{
Both are same. public static void main(String[] args)
So myPig can access : {
Dog methods Pig myPig = new Pig();
inherited Animal methods. [Link]();
[Link]();
}
}

Prof. Parthavi Varan


2. Animal myPig = new Pig();
class Animal
Part Type {
Reference Type Animal void eat()
{
Object Type Pig [Link]("Animal eats");
}
}
This is called: class Pig extends Animal
Upcasting {
void sound()
Runtime polymorphism {
[Link](“Wee wee");
What Can Be Accessed? }
}
public class Main
Using myPig, you can access only: {
methods available in Animal public static void main(String[] args)
even though object is Pig. {
Animal myPig = new Pig();
[Link]();
Why Error? [Link]();//ERROR
}
Because compiler checks using reference type. }
Reference type is:Animal
and Animal class has no Sound() method.
Prof. Parthavi Varan
But Overridden Methods Work

[Link]();
calls Pig’s method if overridden.

Because:method overriding works at runtime based on actual object


type
class Animal
{
void sound()
{
[Link]("Animal Sound");
}
} Pig sound - wee wee
class Pig extends Animal
{
void sound()
{
[Link](“Pig sound - Wee wee");
}
}
public class Main
{
public static void main(String[] args)
{
Animal myPig = new Pig();
[Link]();
}
} Prof. Parthavi Varan
▪ Example 2 ☺
PS D:\Lectures\BCA Sem-3\Java Theory\Practical> javac
class Vehicle
{ [Link]
void start()
{ PS D:\Lectures\BCA Sem-3\Java Theory\Practical>java
[Link]("Vehicle starts");
}
Main
} Car starts with key
class Car extends Vehicle Bike starts with self button
{

void start()
{
[Link]("Car starts with key");
}
}

class Bike extends Vehicle


{
void start()
{
[Link]("Bike starts with self button");
}
}

public class Main


{

public static void main(String[] args)


{

Vehicle v;
v = new Car();
[Link]();
v = new Bike();
[Link]();
}
} Prof. Parthavi Varan
Super Keyword

Prof. Parthavi Varan


super keyword refers to the parent class object. class Animal
It is used inside child class to: {
Access parent variable String color = "White";
Access parent method }
Access parent constructor class Dog extends Animal
{
1) Access Parent Variable String sound = “Bhow Bhow";
void display()
In this example , Animal class has: {
one string variable color [Link](color);
Dog inherits Animal class Dog class has : [Link](sound);
one string variable sound }
one method display which display color and sound }
variable value. public class Main
{
Here inside display method: public static void main(String[] args)
[Link](color); {
Dog d = new Dog();
This line search color value first in child class and if method [Link]();
not found in child class then it searches inside parent class. }
}
Here inside Dog class no variable like color so searches inside
Animal class and found color variable so print it’s value. White
Prof. Parthavi Varan
Bhow Bhow
In this example , Animal class has: class Animal
one string variable color {
Dog inherits Animal class Dog class has : String color = "White";
one string variable color }
one method display which display color and [Link] class Dog extends Animal
variable value. {
String color = “Black”;
Here inside display method: void display()
[Link](color); {
[Link](color);
This line search color value first in child class and if method not [Link]([Link]);
found inside child class then it searches inside parent class. }
}
Here inside Dog class color variable is there so it print Black. public class Main
{
[Link]([Link]); public static void main(String[] args)
{
If user only writes color then it print Dog class color Dog d = new Dog();
[Link] if I want to print color value of Animal class then I [Link]();
have to write [Link] Here,super means parent class’s color }
variable so it prints White }

Black
White Prof. Parthavi Varan
2) Access Parent Method

class Animal
Here Animal class has one method sound and Dog
{
void sound() class has also same method name sound.
{
[Link]("Animal sound"); If user want to access Animal class sound method
} then [Link] is used to access parent class
} method inside child class.
class Dog extends Animal
{
void sound()
{
[Link](); Animal sound
[Link]("Dog barks"); Dog barks
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
[Link]();
}
}
Prof. Parthavi Varan
class Animal
{ 3) Access Parent Constructor
Animal()
{
[Link]("Animal Constructor"); Parent constructor only called inside
} child constructor means inside any
} method of child class you can’t call
class Dog extends Animal parent constructor.
{

Dog()
{
super();
Animal Constructor
[Link]("Dog Constructor");
} Dog Constructor
void display()
{
super();
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
}
} Prof. Parthavi Varan
Final Keyword

Prof. Parthavi Varan


The final keyword means something cannot be changed further.

It is used to restrict:
Variables
Methods
Classes

1. Final Variable
Value cannot change once assigned.
class Demo
{
final int speed = 90;
void show()
{
[Link]("Speed : " + speed);
// speed = 100; // ERROR
}
public static void main(String[] args)
{
Demo d = new Demo();
[Link]();
}
} Prof. Parthavi Varan
2. Final Method

A final method cannot be overridden.

class Bank
{
final void interestRate()
{
[Link]("Interest Rate is Fixed");
}
}
class SBI extends Bank
{
// void interestRate() {} // ERROR
}
public class Main
{
public static void main(String[] args)
{
SBI s = new SBI();
[Link]();
}
}
Prof. Parthavi Varan
3. Final Class

A final class cannot be inherited.

final class Mobile


{
void call()
{
[Link]("Calling...");
}
}
// class Samsung extends Mobile {} // ERROR
public class Main
{
public static void main(String[] args)
{
Mobile m = new Mobile();
[Link]();
}
}

Prof. Parthavi Varan


Abstract Class

Prof. Parthavi Varan


Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces..

The abstract keyword is a non-access modifier, used for classes and methods:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another
class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass
(inherited from).

An abstract class can have:


Variables
Static variables
Constructors
Normal methods
Abstract methods
Static methods

Prof. Parthavi Varan


abstract class Animal
{
abstract void animalSound(); From the example , it is not possible
public void sleep() to create an object of the Animal
{ class.
[Link]("Zzz");
}
}
class Pig extends Animal
{
void animalSound()
{
[Link]("The pig says: wee wee");
}
}
class Main
{
public static void main(String[] args)
{
Animal a=new Animal();
}
}
Prof. Parthavi Varan
abstract class Animal
{
abstract void animalSound(); To access the abstract class, it
public void sleep() must be inherited from another
{ class.
[Link]("Zzz");
}
}
class Pig extends Animal
{
void animalSound()
{
[Link]("The pig says: wee wee");
}
}
class Main
{
public static void main(String[] args)
{
Pig p = new Pig();
[Link]();
[Link]();
}
}
Prof. Parthavi Varan
abstract class Animal
{
// Instance variable
String name;
// Static variable
static int count = 0;
// Constructor
Animal(String name)
{
[Link] = "mahi";
count++;
[Link]("Animal constructor called");
}
// Normal method
void eat()
{
[Link](name + " is eating");
}
// Static method
static void showCount()
{
[Link]("Count = " + count);
}
// Abstract method
abstract void sound();
}

class Dog extends Animal


{
Interface

Prof. Parthavi Varan


An interface is used to achieve:
Abstraction
multiple inheritance
loose coupling

Interface contains:
abstract method
constants (public static final variables)
Normal method (must be with default keyword)
Static method

interface Animal
{
int x=10;
void sound();
}
Java automatically treats method as:
interface Animal
{
public static final int x=10;
public abstract void sound();
} Prof. Parthavi Varan
Variable in Interface

Prof. Parthavi Varan


All variables inside interface are automatically : public static final
So they become constants.

interface Demo
{
int x = 10;
}
public class Main
{
public static void main(String[] args)
{
[Link](Demo.x);
// Demo.x = 20; // ERROR
}
}

Prof. Parthavi Varan


Interface Animal
{
int AGE=10; // public static final
void sound(); // public abstract
default void eat()
{
[Link]("Animal is eating");
}
static void show()
{
[Link]("Static method in interface");
}
}
class Dog implements Animal
{
public void sound()
{
[Link]("Dog barks");
eat();
[Link]();
}
}
class Main
{
public static void main(String[] args)
{
Dog a = new Dog();
[Link]();
[Link]();
[Link]("Age = " + [Link]);
[Link]();
}
Extending Interfaces

Prof. Parthavi Varan


One interface can inherit another interface using extends.
interface A
{
void show();
}
interface B extends A
{
void display();
}
class Test implements B
{
public void show()
{
[Link]("Show Method");
}

public void display()


{
[Link]("Display Method");
}
}
public class Main
{
public static void main(String[] args)
{
Test t = new Test();
[Link]();
[Link]();
}
} Prof. Parthavi Varan
Multiple Inheritance

Prof. Parthavi Varan


Java does not support multiple inheritance using classes.
❌ Not Allowed:

class A
{
}

class B
{
}

// class C extends A, B {} // ERROR

Prof. Parthavi Varan


interface Father
{
void money();
}
interface Mother
{
void care();
}
class Child implements Father, Mother
{
public void money()
{
[Link]("Father gives money");
}
public void care()
{
[Link]("Mother gives care");
}
}
public class Main
{
public static void main(String[] args)
{

Child c = new Child();


[Link]();
[Link]();
}
} Prof. Parthavi Varan
Interface V/S Abstract Class

Prof. Parthavi Varan


Feature Interface Abstract Class
Methods Mostly abstract Abstract + normal
Variables public static final Any type
Constructor Not allowed Allowed
Multiple inheritance Yes No
Object creation No No
Keyword used implements extends

Example Difference
Interface Abstract Class

interface Animal abstract class Animal {


{
void sound(); abstract void sound();
}
void eat() {
[Link]("Eating");
}
} Prof. Parthavi Varan

You might also like