Advertisement
khagaru

Java Basic Methods

Nov 29th, 2022 (edited)
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. 1. Java Return Methods-------------
  2.  
  3. public class main {
  4. public static void main (String[]arug){
  5.    
  6.     int Totalmember= ID(12,13);
  7.     System.out.println(Totalmember);
  8.     }
  9. static int ID(int person1, int person2){   // Return type methods/ void can not be use return type
  10.     return person1+person2;
  11.     }
  12. }
  13.  
  14. // returns an int value 5 -------------------
  15. static int returnFive() {
  16.   return 5;
  17. }
  18.  
  19. // has a parameter -----------------------
  20. static void sayHelloTo(String name) {
  21.   System.out.println("Hello " + name);
  22. }
  23.  
  24. // simply prints"Hello World!" --------------------
  25. static void sayHello() {
  26.   System.out.println("Hello World!");
  27. }
  28.  
  29.  
  30. 2. Creating class and object ---------------
  31.   public class Animal{
  32.     void berk(){
  33.     System.out.println("Woff Woff");
  34. }}
  35.  
  36. class Dog{
  37.  
  38.     public static void main( String[]arug ){
  39.      Animal dog = new Animal();
  40.         dog.berk();
  41.     }
  42.  
  43.  
  44. }
  45.  
  46. 3.Encapsulation -----------------------------
  47. import java.util.Scanner;
  48. public class Main
  49. {
  50.     public static void main(String[] args) {
  51.         Scanner read = new Scanner(System.in);
  52.         int a = read.nextInt();
  53.          Pupil pupil =  new Pupil();
  54.             pupil.setAge(a);            
  55.     }
  56. }
  57.  
  58. class Pupil{
  59.     private int age;
  60.    
  61.     //complete setter method
  62.     public void  setAge(int a){
  63.  
  64.         if(a>6){
  65.              this.age=a;
  66.              System.out.println("Welcome");
  67.         }else{
  68.             System.out.println("Sorry");
  69.         }
  70.             }
  71.      public int getAge(){
  72.         return age;
  73.     }
  74. }
  75.  
  76. 4.Inhertance -----------------------------
  77.   //  Recall the protected access modifier, which makes the members visible only to the subclasses.
  78.     //You can access the superclass from the subclass using the super keyword.
  79.    
  80.    
  81.   5. Polymorphism -----------------------------
  82.  
  83.     class Animal {
  84.     public void makeSound() {
  85.         System.out.println("Grr...");
  86.     }
  87. }
  88. class Cat extends Animal {
  89.     public void makeSound() {
  90.         System.out.println("Meow");
  91.     }
  92. }
  93. class Dog extends Animal {
  94.     public void makeSound() {
  95.         System.out.println("Woof");
  96.     }
  97. }
  98.  
  99. class Program {
  100.     public static void main(String args[ ]) {
  101.         Animal a = new Dog();
  102.         Animal b = new Cat();
  103.        
  104.         a.makeSound();
  105.         b.makeSound();
  106.     }
  107. }
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement