Results 1 to 2 of 2
  1. #1
    ConTec is offline Member
    Join Date
    Apr 2014
    Location
    Plattsburgh, NY
    Posts
    2
    Rep Power
    0

    Default Is this right? Implement and extend...

    So, I'm learning Java (obviously), and I just learned how to 'implement' an interface and 'extend' a class. Now I want to try and recall the information by memory without using any reference material. So if someone experienced could respond with a thumbs up that'd be great ;) And if I got it right, then we'll just leave the information for others.


    Implementing an interface...

    Java Code:
    //This interface will hold information for cell phones
    //Like saying... you can't BE a cell phone unless you have this information, at the very least
    
    public interface CellInfo {
         
         public void model();
         public void make();
         public void androidVer();
    
    }
    
    //Now I implement the interface for a class called Galaxy, which is a class about a specific phone
    
    public class Galaxy implements CellInfo {
    
         public void model() {
              System.out.println("I'm a Galaxy S5.");
         }
    
         public void make() {
              System.out.println("I'm made by Samsung.");
         }
    
         public void androidVer() {
              System.out.println("I run Android 4.4 KitKat.");
         }
    
    }


    Extending a superclass with a subclass...

    Java Code:
    //This superclass will hold basic information that EVERY cell phone, regardless of make or model, must have
    
    class Phone {
    
         int double screenSize = 5.3;
         int double weight = 10.2;
         int price = 300;
    
    }
    
    //Now I'll make a subclass that extends the Phone class, and this subclass of phones will be called iPhone
    
    public class iPhone extends Phone {
    
         boolean hasAppleLogo = true;
    
         void adjustPrice() {
              if (hasAppleLogo) {
                   //Slam on Apple...
                   price += 500;
              }
          }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Is this right? Implement and extend...

    You're correct in terms of how to use the keywords. But here are a couple of observations.

    1. If your CellInfo is required, then it should be part of a super class. Probably something like AbstractPhone where you can
    have default implementations where appropriate. AbstractPhone should have all the basic features of a typical phone without regard to
    make or model.
    2. There are other OS's besides Android so you wouldn't want that to be in your current interface or abstract class. It would be
    best to have simply a getOS() method and possibly a public Enum with popular OS"s from which to choose when you create your phone. The enum could be updated with new OS designators without affecting the published class.
    3. An interface could be used when you want to tailor some specific behavior(s) to your final phone class that is not part of the AbstractPhone class.

    You may also want to make your instance fields private and use getters and setters to access them. And your Phone class info clearly does not apply to all phones.
    Regards,
    Jim
    Last edited by jim829; 04-12-2014 at 09:37 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Extend enum
    By Dipke in forum New To Java
    Replies: 0
    Last Post: 05-13-2013, 08:51 PM
  2. what really happens with extend and inheritance?
    By willemjav in forum New To Java
    Replies: 3
    Last Post: 02-11-2013, 12:51 PM
  3. Replies: 15
    Last Post: 02-05-2012, 12:15 AM
  4. to Extend or not to Extend
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 12-21-2011, 05:28 PM
  5. Extend Jscroll
    By cowboy in forum New To Java
    Replies: 10
    Last Post: 12-13-2009, 09:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •