Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Interfaces' - 8 code snippet(s) found

 Sample 1. Class Nested within Interface / Static Inner class within Interface

public interface SampleInterface {
   public int sampleMethod(List sampleList);

   static class Impl implements SampleInterface {

      @Override
      public int sampleMethod(List sampleList) {
         return 0;
      }
   }
}

   Like      Feedback     nested classes  nested class  inner class  inner classes  static inner class  static inner class within interface  @override


 Sample 2. Interface Default Methods ( Base Class Definition has precedence over Interface Default Method if both are being extended and implemented and have common method definition )

HelloJava8Base 

public class HelloJava8Base {
public void defaultMethod() {
System.out.println("Default Method Base Class Implementation");
}
}

DefaultMethodInterface

public interface DefaultMethodInterface {
default public void defaultMethod() {
System.out.println("Default Method Interface Implementation");
}
}

HelloJava8

public class HelloJava8 extends HelloJava8Base implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface dmi = new HelloJava8();
dmi.defaultMethod(); // Prints "Default Method Base Class Implementation"
}
}

   Like      Feedback     interface default methods  java 8  interfaces


 Sample 3. Write a program to show thread usage in Java by implementing runnable interface

public class MyClass {
   static class MyThreadClass implements Runnable{

      public void start() {
         Thread t = new Thread(this,"threadName");
         t.start();
      }
      
      @Override
      public void run() {
         System.out.println("Hello");
         try {
            Thread.sleep(1000);
            System.out.println("Hello Again");
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         
      }
      
   }
   
   public static void main(String[] args){
      MyThreadClass myThreadClass = new MyThreadClass();
      myThreadClass.start();
      
      MyThreadClass myThreadClass2 = new MyThreadClass();
      myThreadClass2.start();
   }
}

   Like      Feedback     Threads  runnable interface


 Sample 4. Interface Default Methods

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}

public void defaultMethod(){
System.out.println("HelloJava8");
}
}

   Like      Feedback     interface default methods   interfaces  java 8


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Check if an object is an instanceOf class or derived class or implementing interface

DerivedClass dc = new DerivedClass();

if (dc instanceof BaseClass) {
}

   Like      Feedback     instanceof


 Sample 6. Get the properties of class (i.e package name , interfaces , subclasses etc ) using ClassUtils( Apache Commons )

Class class1 = ClassUtils.getClass("BuggyBreadTest"); 
System.out.println(ClassUtils.getPackageName(class1));
System.out.println(ClassUtils.getAllInterfaces(class1));

   Like      Feedback     ClassUtils  Apache Commons  Get Class Properties  java.lang.Class


 Sample 7. Usage of Java 8 Consumer interface

Consumer<String> consumer = s->{
   System.out.println(s);
};

consumer.accept("BuggyBread"); // prints BuggyBread

   Like      Feedback     Java 8 Consumer  Consumer interface


 Sample 8. Update Google Adwords Bids for a particular keyword using Keyword Id

Usage of

import com.google.api.ads.adwords.axis.v201802.cm.AdGroupCriterion;
import com.google.api.ads.adwords.axis.v201802.cm.AdGroupCriterionOperation;
import com.google.api.ads.adwords.axis.v201802.cm.AdGroupCriterionReturnValue;
import com.google.api.ads.adwords.axis.v201802.cm.AdGroupCriterionServiceInterface;
import com.google.api.ads.adwords.axis.v201802.cm.BiddableAdGroupCriterion;
import com.google.api.ads.adwords.axis.v201802.cm.BiddingStrategyConfiguration;
import com.google.api.ads.adwords.axis.v201802.cm.Bids;
import com.google.api.ads.adwords.axis.v201802.cm.CampaignCriterionServiceInterface;
import com.google.api.ads.adwords.axis.v201802.cm.CpcBid;
import com.google.api.ads.adwords.axis.v201802.cm.Keyword;
import com.google.api.ads.adwords.axis.v201802.cm.Money;

public void updateBidForKeyword(Long adGroupId, Long keywordId, Long bidAmount) {
      
AdWordsSession adwordSession = null;

// initialize AdWords session

try {
            // Generate a refreshable OAuth2 credential
            Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS)
                  .fromFile().build()
                  .generateCredential();

            // Construct an AdWordsSession.
            adwordSession = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();

         } catch (Exception ex) {

         }

// Get CampaignCriterionService using AdWordsSession

AdWordsServices adWordsServices = new AdWordsServices();

      CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(adwordSession, CampaignCriterionServiceInterface.class);

      AdGroupCriterionServiceInterface adGroupCriterionService = GoogleAuthenticationService
            .getAdGroupCriterionService();

      Keyword keyword = new Keyword();
      keyword.setId(keywordId);

      BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
      keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
      keywordBiddableAdGroupCriterion.setCriterion(keyword);

      BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
      CpcBid bid = new CpcBid();
      bid.setBid(new Money(null, bidAmount));
      biddingStrategyConfiguration.setBids(new Bids[] { bid });
      keywordBiddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);

      AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
      keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
      keywordAdGroupCriterionOperation.setOperator(Operator.SET);

      AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { keywordAdGroupCriterionOperation };

      AdGroupCriterionReturnValue result = null;

      try {
         result = adGroupCriterionService.mutate(operations);
      } catch (Exception e) {
         e.printStackTrace();
      }
      
      // Display campaigns.
      for (AdGroupCriterion campaignCriterion : result.getValue()) {
         System.out.printf("Campaign criterion with criterion ID %d, " + "and type '%s' was added.%n",
               campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
      }

   }

   Like      Feedback     Google Adwords  Adwords  Adwords Java Api  Update Adwords Bids  Update Adwords Bids for keyword



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus