Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: what is the use of interfaces and abstract classes?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is the use of interfaces and abstract classes?

    I am 5 years experienced in programming in C but Till I cant understand the languages like Java.
    what is the use of using interfaces and abstract class since they do nothing.
    Last edited by sagar474; September 18th, 2011 at 02:11 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the use of interfaces and abstract classes?

    Interfaces are a way to give a data type to an object. Java is strict on data typing compared to c.
    They allow the compiler to verify that a class object has the method(s) required for that data type.
    A common used interface is a listener. The compiler guarantees that any object that says it is a listener will have all the required methods defined by the interface.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the use of interfaces and abstract classes?

    public class Main {
     
     
     
        public static void main(String[] args) {
     
              shape circleshape=new circle();
     
                 circleshape.Draw();
        }
    }
     
     interface shape
     {
         public   String baseclass="shape";
     
         public void Draw();
     
     
     }
     class circle implements shape
     {
     
        public void Draw() {
            System.out.println("Drawing Circle here");
        }
     
     
     }

    both programs do the same!
    public class HelloWorld {
     
     
     
        public static void main(String[] args) {
     
              circle circleshape=new circle();
     
                 circleshape.Draw();
        }
    }
     
     
     class circle 
     {
     
        public void Draw() {
            System.out.println("Drawing Circle here");
        }
     
     
     }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the use of interfaces and abstract classes?

    What's the point?

    Where is the method that takes an argument of type shape?

    void SomeMethod(shape aShape) {
    }

    You could NOT pass an object of the circle class from your second code example to this method. It is not a shape.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the use of interfaces and abstract classes?

    Is interface shape can used as a data-type?
    and where as a circle can not be use as a data type when since it is not implemented shape?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the use of interfaces and abstract classes?

    Both shape and circle are data types.
    And so are Main and HelloWorld data types.

Similar Threads

  1. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:49 AM
  2. [SOLVED] abstract classes, can you explain?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 08:01 PM
  3. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  4. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM