unit2
unit2
UNIT-II
INHERITANCE
Inheritance is the mechanism of deriving new class from old one, old class is
known as superclass and new class is known as subclass. The subclass
inherits all of its instances variables and methods defined by the superclass
and it also adds its own unique elements. Thus we can say that subclass are
specialized version of superclass.
Types of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid inheritance
6. Multipath inheritance
1. Single inheritance:
Here One subclass is deriving from one super class.
A SUPER CLASS A
EXTENDS
B SUB CLASS B
2. Multilevel Inheritance:
In multilevel inheritance the class is derived from the derived class.
SUPER-CLASS
A
EXTENDS
C
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
SUB-CLASS
EXTENDS
SUB-SUBCLASS
3. Hierarchical Inheritance:
Only one base class but many derived classes.
SUPERCLASS
Figure
EXTENDS
SUBCLASS
Rectangle Triangle
4. Multiple Inheritance:
Deriving one subclass from more than one super classes is called multiple
inheritance.
INTERFACE1 INTERFACE2
A B
(Animal) (Bird)
IMPLEMENTS
SUBCLASS
5. Hybrid Inheritance:
It is a combination of multiple and hierarchical inheritance.
HIERARCHICAL INHERITANCE
B C
D
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
MULTIPLE INHERITANCE
6. Multipath Inheritance:
B C
Benefits of Inheritance:
Increased reliability
Software reusability
Code sharing
To create software components
Consistency of interface
Polymorphism
Information hiding
Rapid prototyping
Code sharing: At one level of code sharing multiple projects or users can use
a single class.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Rapid prototyping: By using the same code for different purposes, we can
reduce the lengthy code of the program.
Cost of inheritance:
Program size: If the cost of memory decreases, then the program size
does not matter. Instead of limiting the program sizes, there is a need
to produce code rapidly that has high quality and is also error-free.
Execution speed: The specialized code is much faster than the
inherited methods that manage the random subclasses.
Program complexity: Complexity of a program may be increased if
inheritance is overused.
Message-passing: The cost of message passing is very less when
execution speed is considered.
Classes and packages are both means of encapsulating and containing the
name space and scope of variables and methods. Packages act as containers
for classes and other subordinate packages.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
We can protect the data from unauthorized access. To do this ,we are using
access specifiers.
private: private members of a class are not available outside the class.
public: public members of a class are available anywhere outside the class.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Class Hierarchy
Super Uses:
super can be used to refer super class constructor as: super (values)
super can be used to refer super class constructor as: super (values)
class Figure
double dim1;
double dim2;
Figure(double a,double b)
dim1=a;
dim2=b;
double area()
{
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
return 0;
Rectangle(double a,double b)
super(a,b);
double area()
return dim1*dim2;
Triangle(double a,double b)
super(a,b);
double area()
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
return dim1*dim2/2;
class FindAreas
Figure figref;
figref=r;
System.out.println("area is"+figref.area());
figref=t;
System.out.println("area is"+figref.area());
figref=f;
System.out.println("area is"+figref.area());
OUTPUT:
area is 45
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
area is 40
area is 0
super.member;
class A {
int i;
class B extends A {
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
void show() {
class UseSuper {
subOb.show();
i in superclass: 1
i in subclass: 2
import java.io.*;
class A
void display()
System.out.println("hi");
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
class B extends A
super.display();
System.out.println("hello");
B b=new B();
b.display();
Output: hi
Hello
Base class
super-class
extends
} }
One baseobj;// base class object.
super class object baseobj can be used to refer its sub class objects.
Most people naturally view the world as made up of ojects that are related
to each other in a hierarchical way.
Inheritance: A new class (subclass, child class) is derived from the existing
class(base class, parent class).
1. Reusability
2. Abstraction
Syntax:
Declaration of variables;
Declaration of methods;
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Super class: In Java a class that is inherited from is called a super class.
Sub class: The class that does the inheriting is called as subclass.
Extends: To inherit a class, you simply incorporate the definition of one class
into another by using the extends keyword.
The ―extends‖ keyword indicates that the properties of the super class name
are extended to the subclass name. The sub class now contain its own
variables and methods as well those of the super class. This kind of situation
occurs when we want to add some more properties to an existing class
without actually modifying the super class members.
To see how, let‘s begin with a short example. The following program creates
a super class called A and a subclass called B. Notice how the keyword
extends is used to create a subclass of A.
// Create a superclass.
class A {
int i, j;
void showij() {
class B extends A {
int k;
void showk() {
void sum() {
class SimpleInheritance {
superOb.i = 10;
superOb.j = 20;
superOb.showij();
System.out.println();
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
subOb.showij();
subOb.showk();
System.out.println();
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
subOb.sum();
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
i+j+k: 24
As you can see, the subclass B includes all of the members of its super class,
A. This is why subOb can access i and j and call showij( ). Also, inside sum(
), i and j can be referred to directly, as if they were part of B. Even though A
is a super class for B, it is also a completely independent, stand-alone class.
Being a super class for a subclass does not mean that the superclass cannot
be used by itself. Further, a subclass can be a super class for another
subclass.
Multilevel Inheritance:
SUPER-CLASS
B
EXTENDS
C SUB-CLASS
EXTENDS
SUB-SUBCLASS
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 =
new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "
+ shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is "
+ shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
The output of this program is shown here:
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $1.28
When a class hierarchy is created, in what order are the constructors for the
classes that make up the hierarchy executed? For example, given a subclass
called B and a superclass called A, is A‘s constructor executed before B‘s, or
vice versa? The answer is that in a class hierarchy, constructors complete
their execution in order of derivation, from superclass to subclass.
occurs, each subclass inherits all of the traits found in all of its supe rclasses.
In this case, C inherits all aspects of B and A. To see how a multilevel
hierarchy can be useful, consider the following program.
class A {
A() {
class B extends A {
B() {
class C extends B {
C() {
class CallingCons {
C c = new C();
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
As you can see, the constructors are called in order of derivation. If you
think about it, it makes sense that constructors are executed in order of
derivation. Because a super class has no knowledge of any subclass, any
initialization it needs to perform is separate from and possibly prerequisite to
any initialization performed by the subclass. Therefore, it must be executed
first.
Method Overriding: Writing two or more methods in super & sub classes with
same name and same signatures is called method overriding. In
method overriding JVM executes a method depending on the type of the
object.
Write a program that contains a super and sub class which contains a
method
with same name and same method signature, behavior of the method is
dynamically decided.
class Animal
{ void move()
{ void move()
Method overriding forms the basis for one of Java‘s most powerful concepts:
dynamic method dispatch. Dynamic method dispatch is the mechanism by
which a call to an overridden method is resolved at run time, rather than
compile time. Dynamic method dispatch is important because this is how
Java implements run-time polymorphism.
A super class reference variable can refer to a subclass object. Java uses this
fact to resolve calls to overridden methods at run time. Here is how. When
an overridden method is called through a super class reference, Java
determines which version of that method to execute based upon the type of
the object being referred to at the time the call occurs. Thus, this
determination is made at run time. When different types of objects are
referred to, different versions of an overridden method will be called. In
other words, it is the type of the object being referred to (not the type of the
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Abstract classes:
The abstract methods and abstract class should be declared using the
keyword abstract.
We cannot create objects to abstract class because it is having
incomplete code. Whenever an abstract class is created, subclass
should be created to it and the abstract methods should be
implemented in the subclasses, then we can create objects to the
subclasses.
An abstract class is a class with zero or more abstract methods
An abstract class contains instance variables & concrete methods in
addition to abstract
methods.
Object-oriented concepts form the heart of Java just as they form the basis
for human understanding. It is important that you understand how these
concepts translate into programs. As you will see, object-oriented
programming is a powerful and natural paradigm for creating programs that
survive the inevitable changes accompanying the life cycle of any major
software project, including conception, growth, and aging. For example,
once you have well-defined objects and clean, reliable interfaces to those
objects, you can gracefully decommission or replace parts of an older system
without fear.
Abstract class: Any class that contains one or more abstract methods must
also be declared abstract.
To declare a class abstract, you simply use the abstract keyword in front of
the class keyword at the beginning of the class declaration. There can be no
objects of an abstract class. That is, an abstract class cannot be directly
instantiated with the new operator. Such objects would be useless, because
an abstract class is not fully defined. Also, you cannot declare abstract
constructors, or abstract static methods. Any subclass of an abstract class
must either implement all of the abstract methods in the super class, or be
itself declared abstract.
There are situations in which you will want to define a super class that
declares the structure of a given abstraction without providing a complete
implementation of every method. That is, sometimes you will want to create
a super class that only defines a generalized form that will be shared by all
of its subclasses, leaving it to each subclass to fill in the details. Such a class
determines the nature of the methods that the subclasses must implement.
One way this situation can occur is when a super class is unable to create a
meaningful implementation for a method. This is the case with the class
Figure used in the preceding example. The definition of area( ) is simply a
placeholder. It will not compute and display the area of any type of object.
As you will see as you create your own class libraries, it is not uncommon for
a method to have no meaningful definition in the context of its super class.
You can handle this situation two ways. One way, as shown in the previous
example, is to simply have it report a warning message. While this approach
can be useful in certain situations—such as debugging—it is not usually
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Abstract method: A method that is declared but not implemented (no body).
Abstract methods are used to ensure that subclasses implement the method.
{ double dim1;
double dim2;
{ dim1 = a;
dim2 = b;
class AbstractAreas
output:
Area is 45.0
Area is 40.0
Although it is not possible to create an object of type Figure, you can create
a reference variable of type Figure. The variable figref is declared as a
reference to Figure, which means that it can be used to refer to an object of
any class derived from Figure. As explained, it is through superclass
reference variables that overridden methods are resolved at run time.
Final variables work like const of C-language that can‘t be altered in the
whole program. That is, final variables once created can‘t be changed and
they must be used as it is by all the program code.
Example program:
import java.io.*;
class FinalVar
static
int x=10;
System.out.println("x is:"+x);
System.out.println("y is:"+y);
x=30;
y=40;
System.out.println("x is:"+x);
System.out.println("y is:"+y);
Output:
Example program:
import java.io.*;
class A
System.out.println("hi");
class B extends A
void display()
super.display();
System.out.println("hello");
{
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
B b=new B();
b.display();
Output:
Example program:
import java.io.*;
System.out.println("hi");
d.display();
Output:
DEFINING PACKAGE:
This chapter examines two of Java‘s most innovative features: packages and
interfaces.
Packages are containers for classes that are used to keep the class name
space compartmentalized. Packages are stored in a hierarchical manner and
are explicitly imported into new class definitions.
import java.io.*;
ADVANTAGES OF PACKAGES:
the classes and interfaces which perform input and output operations
are stored in java.io. package.
Packages hide the classes and interfaces in a separate sub directory,
so that accidental deletion of classes and interfaces will not take
place.
The classes and interfaces of a package are isolated from the classes
and interfaces of another package. This means that we can use same
names for classes of two different classes. For example, there is a
Date class in java.util package and also there is another Date class in
java.sql package.
A group of package called a library. The classes and interfaces of a
package are likes books in a library and can be reused several times.
This reusability nature of packages makes programming easy. Just
think, the package in Java are created by JavaSoft people only once,
and millions of programmers all over the world are daily by using
them in various programs.
Built-in packages
User-defined packages
Built-in packages:
These are the packages which are already available in Java language. These
packages provide all most all necessary classes, interfaces and methods for
the programmer to perform any task in his programs. Since, Java has an
extensie library of packages, a programmer need not think about logic for
doing any task. For everything, there is a method available in Java and that
method can be used by the programmer without developing the logic on his
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
own. This makes the programming easy. Here, we introduce some of the
important packages of Java SE:
Java.lang: lang stands for language. This package got primary classes and
interfaces essential for developing a basic Java program. It consists of
wrapper classes(Integer, Character, Float etc), which are useful to convert
primitive data types into objects. There are classes like String, SttringBuffer,
StringBuilder classes to handle strings. There is a thread class to create
various individual processes. Runtime and System classes are also present in
java.lang package which contain methods to execute an application and find
the total memory and free memory available in JVM.
Java.util: util stands for utility. This package contains useful classes and
interfaces like Stack, LinkedList, Hashtable, Vector, Arrays, etc. thses
classes are collections. There are also classes for handling Date and Time
operations.
Java.io: io stands for input and output. This package contains streams. A
stream represents flow of data from one place to another place. Streams are
useful to store data in the form of files and also to perform input-output
related tasks.
Java.awt: awt stands for abstract window toolkit. This helps to develop
GUI(Graphical user Interfaces) where programs with colorful screens,
paintings and images etc., can be developed. It consists of an important sub
package, java.awt.event, which is useful to provide action for components
like push buttons, radio buttons, menus etc.
Javax.swing: this package helps to develop GUI like java.awt. The ‗x‘ in
javax represents that it is an extended package which means it is a package
developed from another package by adding new features to it. In fact,
javax.swing is an extended package of java.awt.
Java.applet: applets are programs which come from a server into a client
and get executed on the client machine on a network. Applet class of this
package is useful to create and use applets.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
User-Defined packages:
Just like the built in packages shown earlier, the users of the Java language
can also create their own packages. They are called user-defined packages.
User-defined packages can also be imported into other classes and used
exactly in the same way as the Built-in packages.
//creating a package
package pack;
{ d1 = a;
d2 = b;
The –d option tells the Java compiler to create a separate directory and
place the .class file in that directory (package). The (.) dot after –d
indicates that the package should be created in the current directory.
So, out package pack with Addition class is ready.
import pack.Addition;
class Use
ob1.sum();
Output:
Program 3: Write a program to add one more class Subtraction to the same
package pack.
package pack;
{ d1 = a;
d2 = b;
Program 4: Write a program to access all the classes in the package pack.
//To import all the classes and interfaces in a class using import pack.*;
import pack.*;
class Use
ob1.sum();
ob2.difference();
UNDERSTANDING CLASSPATH:
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
If our package exists in e:\sub then we need to set class path as follows:
package packagename.subpackagename;
import pack1.pack2;
package pack1.pack2;
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
ACCESSING A PACKAGES:
Access Specifier: Specifies the scope of the data members, class and
methods.
package same;
public class A
public int b = 2;
protected int c = 3;
int d = 4;
package same;
import same.A;
public class B
System.out.println(obj.a);
System.out.println(obj.b);
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
System.out.println(obj.c);
System.out.println(obj.d);
package another;
import same.A;
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
Classes Interfaces
Classes have instances as variables Interfaces have instances as abstract
and methods with body methods and final constants
variables.
Inheritance goes with extends Inheritance goes with implements
keyword keywords.
The variables can have any acess The Variables should be public,
specifier. static, final
Multiple inheritance is not possible It is possible
Classes are created by putting the Interfaces are created by putting the
keyword class prior to classname. keyword interface prior to
interfacename(super class).
Classes contain any type of Interfaces contain mustly abstract
methods. Classes may or may not methods. Interfaces are exhibit the
provide the abstractions. fully abstractions
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
// ...
return-type method-nameN(parameter-list);
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Once an interface has been defined, one or more classes can implement that
interface. To implement an interface, include the implements clause in a
class definition, and then create the methods defined by the interface. The
general form of a class that includes the implements clause looks like this:
// class-body
If a class implements more than one interface, the interfaces are separated
with a comma. If a class implements two interfaces that declare the same
method, then the same method will be used by clients of either interface.
The methods that implement an interface must be declared public. Also, the
type signature of the implementing method must match exactly the type
signature specified in the interface definition.
Partial Implementations
If a class includes an interface but does not fully implement the methods
defined by that
int a, b;
void show() {
// ...
Here, the class Incomplete does not implement callback( ) and must be
declared as abstract.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Nested Interfaces
used outside of its enclosing scope, it must be qualified by the name of the
class or interface
class A {
class NestedIFDemo {
if(nif.isNotNegative(10))
if(nif.isNotNegative(-12))
A.NestedIF
Notice that the name is fully qualified by the enclosing class‘ name. Inside
the main( ) method, an A.NestedIF reference called nif is created, and it is
assigned a reference to a B object. Because B implements A.NestedIF, this is
legal.
interface Shape
double pi = 3.14;
{ double r;
{ r = radius;
{ double l,b;
{ l = length;
b = breadth;
class InterfaceDemo
ob1.area ();
ob1.volume ();
ob2.area ();
ob2.volume ();
Output:
APPLYING INTERFACE:
First, here is the interface that defines an integer stack. Put this in a file
called IntStack.java.
interface IntStack {
Applications are:
Abstractions
Multiple Inheritance
VARIABLES IN INTERFACES:
You can use interfaces to import shared constants into multiple classes by
simply declaring an interface that contains variables that are initialized to
the desired values. When you include that interface in a class (that is, when
you ―implement‖ the interface), all of those variable names will be in scope
as constants. (This is similar to using a header file in C/C++ to create a
large number of #defined constants or const declarations.) If an interface
contains no methods, then any class that includes such an interface doesn‘t
actually implement anything.
It is as if that class were importing the constant fields into the class name
space as final variables. The next example uses this technique to implement
an automated ―decision maker‖:
import java.util.Random;
interface SharedConstants {
int NO = 0;
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
int ask() {
else
return NEVER; // 2%
switch(result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
answer(q.ask());
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
answer(q.ask());
answer(q.ask());
answer(q.ask());
Notice that this program makes use of one of Java‘s standard classes:
Random. This class provides pseudorandom numbers. It contains several
methods that allow you to obtain random numbers in the form required by
your program. In this example, the method nextDouble( ) is used. It returns
random numbers in the range 0.0 to 1.0.
In this sample program, the two classes, Question and AskMe, both
implement the SharedConstants interface where NO, YES, MAYBE, SOON,
LATER, and NEVER are defined. Inside each class, the code refers to these
constants as if each class had defined or inherited them directly. Here is the
output of a sample run of this program. Note that the results are different
each time it is run.
Later
Soon
No
Yes
EXTENDING INTERFACES:
One interface can inherit another by use of the keyword extends. The syntax
is the same as
it must provide implementations for all methods defined within the interface
inheritance
interface A {
void meth1();
void meth2();
interface B extends A {
void meth3();
System.out.println("Implement meth1().");
System.out.println("Implement meth2().");
System.out.println("Implement meth3().");
class IFExtend {
ob.meth1();
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
ob.meth2();
ob.meth3();
Although the examples we‘ve included in this book do not make frequent use
of packages or interfaces, both of these tools are an important part of the
Java programming environment.
Virtually all real programs that you write in Java will be contained within
packages. A number will probably implement interfaces as well. It is
important, therefore, that you be comfortable with their usage.
explicitly provided. Thus, the addition of a default method will not cause
preexisting code to break.
Another motivation for the default method was the desire to specify methods
in an interface that are, essentially, optional, depending on how the interface
is used. For example, an interface might define a group of methods that act
on a sequence of elements.
One of these methods might be called remove( ), and its purpose is to
remove an element from the sequence. However, if the interface is intended
to support both modifiable and nonmodifiable sequences, then remove( ) is
essentially optional because it won‘t be used by nonmodifiable sequences. In
the past, a class that implemented a nonmodifiable sequence would have
had to define an empty implementation of remove( ), even though it was
not needed. Today, a default implementation for remove( ) can be specified
in the interface that does nothing (or throws an exception). Providing this
default prevents a class used for nonmodifiable sequences from having to
define its own, placeholder version of remove( ).
Thus, by providing a default, the interface makes the implementation of
remove( ) by a
class optional.
An interface default method is defined similar to the way a method is defined
by a class. The primary difference is that the declaration is preceded by the
keyword default. For example, consider this simple interface:
public interface MyIF {
// This is a "normal" interface method declaration.
// It does NOT define a default implementation.
int getNumber();
// This is a default method. Notice that it provides
// a default implementation.
default String getString() {
return "Default String";
}
}
MyIF declares two methods. The first, getNumber( ), is a standard
interface method declaration. It defines no implementation whatsoever. The
second method is getString( ), and it does include a default
implementation. In this case, it simply returns the string "Default String".
Pay special attention to the way getString( ) is declared. Its declaration is
preceded by the default modifier. This syntax can be generalized. To define
a default
method, precede its declaration with default.
Because getString( ) includes a default implementation, it is not necessary
for an implementing class to override it. In other words, if an implementing
class does not provide its own implementation, the default is used. For
example, the MyIFImp class shown next is perfectly valid:
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
// Implement MyIF.
class MyIFImp implements MyIF {
// Only getNumber() defined by MyIF needs to be implemented.
// getString() can be allowed to default.
public int getNumber() {
return 100;
}
}
The following code creates an instance of MyIFImp and uses it to call both
getNumber( ) and getString( ).
// Use the default method.
class DefaultMethodDemo {
}
}
Now, when getString( ) is called, a different string is returned.
class or a subinterface.