Why do we use this code in JAVA? [duplicate] - java

This question already has answers here:
"Program to an interface". What does it mean? [duplicate]
(8 answers)
Closed 6 years ago.
I am now studying JAVA.
When we make interface like this, sometimes we create object like this
Sample code :-
interface test01{
public boolean A(int i);
public int B(int a);
public String C (String c);
}
class ttt implements test01{
public boolean A(int a){
return true;
}
public int B(int a){
return a;
}
public String C(String c){
return c;
}
}
public class Interface_test {
public static void main(String[] args) {
ttt t1 = new ttt();
System.out.println(t1.A(1));
System.out.println(t1.B(1));
System.out.println(t1.C("C"));
test01 tt = new ttt();
System.out.println(tt.A(1));
System.out.println(tt.B(1));
System.out.println(tt.C("C"));
}
}
This code's result is same
but I was wondering why we use the pattern like this
"test01 tt = new ttt();"
instead of
"ttt t1 = new ttt();"
Please let me know...
Thank you!

Interfaces are used as types to allow implementations to be swapped in the future without having to worry about changing method signatures or implementations in other classes that are using your interface.
If you didn't use an interface, you had to revisit all classes using your old implementation and change it to use the new implementation.
Interfaces are also used as an abstraction to hide logic of the implementing class from users of the interface.

Related

Using of methods instead of constructor [duplicate]

This question already has answers here:
Purpose of a constructor in Java?
(12 answers)
Closed 2 years ago.
When we can use methods instead of constructor for any operation then what is the use of constructor in java or c++.
//Program of Division using constructor:-
class Hey {
Hey() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main ( String[] args ) {
Hey ob = new Hey();
}
}
//Program of division using method:-
class Hey {
public void disp() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main( String[] args ) {
Hey ob = new Hey();
ob.disp();
}
}
As, we can see that both will have same output. So, now I am bit confuse that when to use constructor.
Constructor is used to initialize objects in java. Even if you don't provide constructor in your code, java compiler will automatically add a default constructor.
Whereas Methods are used to exhibits functionalities to object. You will have to invoke methods explicitly in your code.
In the example you shared, you are creating object of Hey class Hey ob=new Hey() in order to call its method disp. So if you want to define object in your class, you will use constructors, and if you want to write some functionality of object, you can use Methods.

Static / Compile-time Polymorphism [duplicate]

This question already has answers here:
What is the difference between dynamic and static polymorphism in Java?
(14 answers)
Closed 3 years ago.
Why method overloading called as static or compile-time polymorphism
sample in Java.
class StaticPolymorphismSample {
void polymorphicMethod(int a) {
}
void polymorphicMethod(int a, int b) {
}
void polymorphicMethod(String a) {
}
void nonPolymorphicMethod(int a) {
}
void nonPolymorphicMethod1(int a) {
}
}
so my question is.
Why we say that method overloading ( in this case polymorphicMethod methods ) are static polymorphism , but another methods( nonPolymorphicMethod(int a) nonPolymorphicMethod1(int a) ) are not polymorphism.
technically I cannot see different between method with same name and different parameters and method with different,
all answers in here and topics in google is not applicable for my question.
For nonPolymorphicMethod1(int a) the reason this wouldn't be considered polymorphic is because it has a different name from the other nonPolymorphicMethods.
For nonPolymorphicMethod( int a, int b ) and nonPolymorphicMethod( int a ) these aren't considered polymorphic as they don't take the same parameters. Edit This is Wrong See Next Line
The other methods you've shown are polymorphic due to their sharing of a name, but differing parameter types or number of parameters.
A better example of polymorphism in methods would be :
public abstract class ClassA
{
public Object getObject()
{
return new Object();
}
}
public class ClassB extends ClassA
{
#Override
public ClassB getObject()
{
return new ClassB();
}
}
public class ClassC extends ClassA
{
#Override
public ClassC getObject()
{
ClassC example = new ClassC();
example.doStuff();
return example;
}
private void doStuff()
{
// Do Something To Change The Object
}
}

Why can I define only default and static methods inside a java interface? [duplicate]

This question already has answers here:
What is the purpose of the default keyword in Java?
(8 answers)
Closed 3 years ago.
Why can I define only default and static methods inside a java interface,while other access modifiers like protected and public having much more privilege than default can't be used?
interface int1
{
default void add(int a, int b)
{
}
static void sub(int a, int b)
{
}
}
interface int1
{
public void add(int a, int b)
{
}
protected void sub(int a, int b)
{
}
}
--shows error message at compile time "Abstract methods do not specify a body"
The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without affecting the classes that implements these interfaces.
Here is link for complete article.

How interface support multiple inheritance [duplicate]

This question already has answers here:
Multiple inheritance on Java interfaces
(5 answers)
Closed 7 years ago.
public class Test implements X, Y { //X.Y interface are shown below
public void myMethod() {
System.out.println(" Multiple inheritance example using interfaces");
}
public static void main(String[]args) {
Test t=new Test();
t.myMethod();
System.out.println(t.a); //compile time error ambigious field
}
}
Please help me to solve this issue
interface X {
public void myMethod();
int a = 0;
}
interface Y {
int a = 9;
public void myMethod();
}
Any variable defined in an interface is, by definition, public static final, in other words it's just a constant, it's not really a field (since there are no fields in interfaces).
So the compilation error you get points out that the compiler doesn't know witch constant you refer to.
You have 2 options here:
change the name of the constant in one of the interfaces, for example in interface Y declare int b = 9;
inside the main method point to a concrete constant: System.out.println(X.a);
Adding to one of the answer already provided.
If a class implements two interfaces and each interface have method with same signature and same name, then you are effectively defining only one method and they are same. If you have two methods of same name but different return types then there will be a compilation error.
Example ->
public interface A {
int a = 0;
void myMethod();
}
public interface B {
int a = 0;
void myMethod();
}
public class Test implements A, B {
#Override
public void myMethod() {
// My method is defined for both A and B
System.out.println(" Multiple inheritance example using interfaces");
}
public static void main(String[] args) {
Test object = new Test();
((A)(object)).myMethod();
((B)(object)).myMethod();
System.out.println(((A)object).a); //To print constant of A
System.out.println(((B)object).a); //To print constant of B
}
}
//Let's see other example
public interface A {
void myMethod();
}
public interface B {
boolean myMethod(); //changed void to boolean
}
public class Test implements A, B {
#Override
public void myMethod() { //Compilation error here, return type is incompatible
}
}
myMethod implements both myMethod declarations. I believe your problem is that in the two separate interfaces a has different values and is before and after the declaration of myMethod. The order is not important, since myMethod will surely be called after the declaration of a, but the difference of a values might cause some logical discrepancies. Maybe you could implement a getter for it as well, to handle the situation.

what is use of referring object to an interface? [duplicate]

This question already has answers here:
What does it mean to program to an interface?
(17 answers)
Closed 9 years ago.
i have searched a lot but did not get exact answer for this Question hope i will get it from here.
what is exactly the use of referring object of a class to an interface instead of referring to the same class ??
void foo(list l){}
public static void main(String a[]){
List l = new ArrayList(); // why use this?
ArrayList a = new ArrayList(); //instead of this?
foo(l);
foo(a);
}
Maybe because an interface is something like a contract, which can be fullfilled by an implementation. You shouldn't depend on an implementation, because its about to change constantly, instead an interface/contract should not. Referring to an interface makes your implementation more robust.
Sometimes, the actual class of an instance does not matter, but only the interface it implements.
Also sometimes, it is impossible to know the actual class of an instance in compile time, but we only know the interface this class will implement.
For example, you have an interface called GeometricFigure
public static interface GeometricFigure {
public Double getGirth();
public String getName();
}
And you use this interface in a real class, for example, Canvas. Canvas has a list of GeometricFigures.
public class Canvas {
private List<GeometricFigure> figures;
public void printAllFigures() {
for (GeometricFigure figure : figures) {
System.out.println(figure.getName() + " " + figure.getGirth());
}
}
}
Now, your Canvas is independent of actual implementations of GeometricFigure. Canvas does not care how you is GeometricFigure is implemented, is it a square, circle or whatever. It only that this class can return a name and a girth so it can print them. So, the actual implementations can be Square, Circle, Triangle, etc. Only important thing is that these future classes implement the GemometricFigure interface.
For example:
public class Square implements GeometricFigure {
private String name;
private Double sideLength;
public Square(String name, Double sideLength) {
this.name = name;
this.sideLength = sideLength;
}
#Override
public Double getGirth() {
return 4 * sideLength;
}
#Override
public String getName() {
return name;
}
}

Categories