Академический Документы
Профессиональный Документы
Культура Документы
properties (methods and fields) of another. With the use of inheritance the
information is made manageable in a hierarchical order.
extends Keyword
extends is the keyword used to inherit the properties of a class. Following
is the syntax of extends keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
class Calculation {
int z;
z = x + y;
z = x - y;
}
public class My_Calculation extends Calculation {
z = x * y;
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
Output
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
If you consider the above program, you can instantiate the class as given
below. But using the superclass reference variable ( cal in this case) you
cannot call the method multiplication(), which belongs to the subclass
My_Calculation.
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and nested
classes) from its superclass. Constructors are not members, so they are not
inherited by subclasses, but the constructor of the superclass can be
invoked from the subclass.
Sample Code
This section provides you a program that demonstrates the usage of
the super keyword.
Example
Live Demo
class Super_class {
// Instantiating subclass
sub.display();
super.display();
obj.my_method();
}
Compile and execute the above code using the following syntax.
javac Super_Demo
java Super
Output
This is the display method of subclass
This is the display method of superclass
value of the variable named num in sub class:10
value of the variable named num in super class:20
Sample Code
The program given in this section demonstrates how to use the super
keyword to invoke the parametrized constructor of the superclass. This
program contains a superclass and a subclass, where the superclass
contains a parameterized constructor which accepts a integer value, and we
used the super keyword to invoke the parameterized constructor of the
superclass.
Copy and paste the following program in a file with the name Subclass.java
Example
Live Demo
class Superclass {
int age;
Superclass(int age) {
this.age = age;
}
public void getAge() {
System.out.println("The value of the variable named age in super class is: "
+age);
Subclass(int age) {
super(age);
s.getAge();
Compile and execute the above code using the following syntax.
javac Subclass
java Subclass
Output
The value of the variable named age in super class is: 24
IS-A Relationship
IS-A is a way of saying: This object is a type of that object. Let us see how
the extends keyword is used to achieve inheritance.
}
public class Mammal extends Animal {
With the use of the extends keyword, the subclasses will be able to inherit
all the properties of the superclass except for the private properties of the
superclass.
We can assure that Mammal is actually an Animal with the use of the
instance operator.
Example
Live Demo
class Animal {
}
Output
true
true
true
Example
Live Demo
interface Animal{}
}
}
Output
true
true
true
HAS-A relationship
These relationships are mainly based on the usage. This determines
whether a certain class HAS-A certain thing. This relationship helps to
reduce duplication of code as well as bugs.
Example
This shows that class Van HAS-A Speed. By having a separate class for
Speed, we do not have to put the entire code that belongs to speed inside
the Van class, which makes it possible to reuse the Speed class in multiple
applications.
Types of Inheritance
There are various types of inheritance as demonstrated below.
A very important fact to remember is that Java does not support multiple
inheritance. This means that a class cannot extend more than one class.
Therefore following is illegal −
Example
However, a class can implement one or more interfaces, which has helped
Java get rid of the impossibility of multiple inheritance
Types of inheritance
To learn types of inheritance in detail, refer: Types of Inheritance in Java.
Single Inheritance: refers to a child and parent class relationship where a class
extends the another class.
Multiple Inheritance: refers to the concept of one class extending more than
one classes, which means a child class has two parent classes. For example
class C extends both classes A and B. Java doesn’t support multiple inheritance,
read more about it here.
Another exmple
1. abstract class Bank{
2. abstract int getRateOfInterest();
3. }
4. class SBI extends Bank{
5. int getRateOfInterest(){return 7;}
6. }
7. class PNB extends Bank{
8. int getRateOfInterest(){return 8;}
9. }
10.
11. class TestBank{
12. public static void main(String args[]){
13. Bank b;
14. b=new SBI();
15. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
16. b=new PNB();
17. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
18. }}
Rule: If there is any abstract method in a class, that class must be abstract.
class Bike12
{
abstract void run();
}
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.
o In other words, Interface fields are public, static and final by default, and methods
are public and abstract.
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
File: TestInterface1.java
File: TestInterface2.java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10. class TestInterface2{
11. public static void main(String[] args){
12. Bank b=new SBI();
13. System.out.println("ROI: "+b.rateOfInterest());
14. }}
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void print();
6. }
7.
8. class TestInterface3 implements Printable, Showable{
9. public void print(){System.out.println("Hello");}
10. public static void main(String args[]){
11. TestInterface3 obj = new TestInterface3();
12. obj.print();
13. }
14. }
Interface inheritance
A class implements interface but one interface extends another interface .
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }
File: TestInterfaceDefault.java
1. interface Drawable{
2. void draw();
3. default void msg(){System.out.println("default method");}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class TestInterfaceDefault{
9. public static void main(String args[]){
10. Drawable d=new Rectangle();
11. d.draw();
12. d.msg();
13. }}
File: TestInterfaceStatic.java
1. interface Drawable{
2. void draw();
3. static int cube(int x){return x*x*x;}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8.
9. class TestInterfaceStatic{
10. public static void main(String args[]){
11. Drawable d=new Rectangle();
12. d.draw();
13. System.out.println(Drawable.cube(3));
14. }}
1. interface printable{
2. void print();
3. interface MessagePrintable{
4. void msg();
5. }
6. }
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since J
abstract methods. can have default and static methods also.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of a
implementation of interface. class.
5) The abstract keyword is used to declare The interface keyword is used to declare interface
abstract class.
6) Example: Example:
public abstract class Shape{ public interface Dr
public abstract void draw(); void
} }