-6

im tring to make an interface that would serve as a short cut for System.out.print(); but when i compile i get abstract methods do not specify a body and i have no clue what this means

public interface Printing{   
    public  abstract void prt(String print,boolean line){ 
        if(line=true) {   
            System.out.println(print);
        }       
    }   

    public abstract void prt(String print){          
        System.out.print(print);       
    } 
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user2482844
  • 1
  • 1
  • 1
  • 1
  • 1
    http://docs.oracle.com/javase/tutorial/java/concepts/interface.html - and then on top of that .... abstract methods ... don't have bodies. – Brian Roach Jun 13 '13 at 15:38
  • 1
    You don't seem to understand what an interface is or what abstract means. It seems like what you want is simply to create a normal class with some static methods. – Dave Costa Jun 13 '13 at 15:40
  • 1
    `line=true` is an assignment not a comparison. Also, google would've answered all of this. – Sotirios Delimanolis Jun 13 '13 at 15:41

7 Answers7

3

Methods declared in an interface are automatically public and abstract. So you can start by ditching these two modifiers.

And abstract methods, by definition, do not have a body... So maybe an interface is not what you are looking for here.

If you DO NOT want to be able to instantiate your Printing but want "default implementations", use an abstract class which provides the base implementation for these two methods:

public abstract class Printing
{
    public void ptr(String print, boolean line) {
        // do stuff
    }

    public void ptr(String print) {
        // do stuff
    }
}

Implementations will then have to extends Printing and @Override the default methods if they want to.

fge
  • 119,121
  • 33
  • 254
  • 329
1

An Interface is a contract, you just specify the methods that should be implemented. An abstract method doesn't contain any body, just signature.

Tudor Zgureanu
  • 725
  • 7
  • 11
0

make this an abstract class instead of an interface.

Interfaces may not have ANY implementation of the methods. So the only thing you can have is.

void prt(String print,boolean line);
void prt(String print);
John B
  • 32,493
  • 6
  • 77
  • 98
0

Abstract methods never specify the body. Thats why they are made abstrat. If you are creating methods in interface, they will always be public and abstract.

Maybe in this case you want to write an abstract class and write one method with implementation and another as abstract one.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

Interfaces are like empty shells and members are not implemented.

Here is what you need it to change your code to:

void prt(String print,boolean line);

void prt(String print);

http://docs.oracle.com/javase/tutorial/java/concepts/interface.html http://csis.pace.edu/~bergin/papers/Interface.html

grepit
  • 21,260
  • 6
  • 105
  • 81
0

You can not specify a body to abstract methods. Abstract methods means they are incomplete.

I guess you need to understand more about interfaces and abstract classes.

To understand what Interfaces and Abstract Classes mean in Java or any other object oriented programming language you should first understand inheritance.

Inheritance

Consider a car and a bus. They are two different vehicles. But still they share some common properties like they have a steering, brakes, gears, engine etc.

So with the inheritance concept this can be represented as following..

public class Vehicle {

    public Driver driver;
    public Seat[] seatArray; //In java and most of the Object Oriented Programming(OOP) languages, square brackets are used to denote arrays(Collections).
    //You can define as many properties as you want here..
}

Now a Bicycle..

public class Bicycle extends Vehicle {

    //You define properties which are unique to bicycles here..
    public Pedal pedal;

}

And a Car..

public class Car extends Vehicle {

    public Engine engine;
    public Door[] doors;
}

That's all about Inheritance. We use them to classify objects into simpler Base forms and their children as we saw above.

Abstract Classes

Abstract classes are incomplete objects. To understand it further, let's consider the vehicle analogy once again.
A vehicle can be driven. Right? But different vehicles are driven in different ways.. For example, You cannot drive a car just as you drive a Bicycle.
So how to represent the drive function of a vehicle? It is harder to check what type of vehicle it is and drive it with its own function; you would have to change the Driver class again and again when adding a new type of vehicle.
Here comes the role of abstract classes and methods. You can define the drive method as abstract to tell that every inheriting children must implement this function.
So if you modify the vehicle class..

//......Code of Vehicle Class
abstract public void Drive();
//.....Code continues

The Bicycle and Car must also specify how to drive it. Otherwise the code won't compile and an error is thrown.

In short.. an abstract class is a partially incomplete class with some incomplete functions, which the inheriting children must specify their own.

Interfaces

Interfaces are totally incomplete. They do not have any properties. They just indicate that the inheriting children is capable of doing something..

Suppose you have different types of mobile phones with you. Each of them have different ways to do different functions; Ex: call a person. The maker of the phone specifies how to do it. Here the mobile phones can dial a number - that is, it is dial-able. Let's represent this as an interface.

public interface Dialable {
    public void Dial(Number n);
}

Here the maker of the Dialable defines how to dial a number. You just need to give it a number to dial.

Dialable myPhone1 = new Dialable() {
    public void Dial(Number n) {
        //Do the phone1's own way to dial a number
    }
}

Dialable myPhone2 = new Dialable() {
    public void Dial(Number n) {
        //Do the phone2's own way to dial a number
    }
}

Here by using interfaces instead of abstract classes, you need not worry about it's properties. Ex: Does it have a touch-screen or dial pad, Is it a fixed landline phone or mobile phone. You just need to know if it is dialable. Does it inherit(or implement) the Dialable interface.

Interfaces are commonly used by developers to ensure interoperability between objects, as far as they share a common function (just like you may change to a landline or mobile phone, as far as you just need to dial a number). In short, interfaces are much simpler version of abstract classes, without any properties.

Also note that that you may implement(inherit) as many interfaces as you want but you may only extend(inherit) a single parent class.

More Info

Abstract classes vs Interfaces
Another similar question in Stackoverflow

Community
  • 1
  • 1
itsfarseen
  • 1,109
  • 10
  • 25
0

If you just mean that to add a shortcut to the System.out.println() method,
There is no need to add the abstract keyword. Also change the interface into a class.

public class Printing{   
    public void prt(String print,boolean line){ 
        if(line=true) {   
            System.out.println(print);
        } else {
            System.out.print(print);
        }       
    }   

    public void prt(String print){          
        System.out.print(print);       
    } 
}

But here you first need to create an instance of Printing object. Just like:

Printing p = new Printing();
//Now you may call the functions on p.
p.prt("Without Line Terminator");
p.prt("Also Without Line Terminator",false);
p.prt("With Line Terminator",true);

If you need to call the functions without instantiating it, Just like Printing.prt("sample") instead of Printing p = new Printing(); p.prt("sample")

You need to define prt() method as static.

public class Printing{   
    public static void prt(String print,boolean line){ 
        if(line=true) {   
            System.out.println(print);
        } else {
            System.out.print(print);
        }       
    }   

    public static void prt(String print){          
        System.out.print(print);       
    }     
}

Now call..

//You may call the functions on Printing itself.
Printing.prt("Without Line Terminator");
Printing.prt("Also Without Line Terminator",false);
Printing.prt("With Line Terminator",true);

More Info
Static Methods
Oracle Javadoc about static classes

itsfarseen
  • 1,109
  • 10
  • 25