fork download
  1.  
  2. // Encapsulation : binding varibles and method into single class .
  3. class Company{
  4. private String name;
  5. private int id;
  6.  
  7. public Company(String EmpName, int Empid){
  8. name=EmpName;
  9. id=Empid;
  10. }
  11.  
  12. public String getName(){
  13. return name;
  14. }
  15.  
  16. public int getID(){
  17. return id;
  18. }
  19.  
  20. void setID(int Empid){
  21. id=Empid;
  22. }
  23. };
  24.  
  25.  
  26. //inheritdance : inheriting the behaviour of base class
  27.  
  28. class Animal{
  29.  
  30. void food(){
  31. System.out.println("Anu food");
  32. }
  33. };
  34. class Cat extends Animal{
  35. void catFood(){
  36. System.out.println("cat food");
  37. }
  38.  
  39. };
  40.  
  41.  
  42. //Compile time polymorphism
  43. class compiletime{
  44. void sum(int a , int b){
  45. System.out.println( a+b);
  46. }
  47. void sum(double a , double b){
  48. System.out.println (a+b);
  49. }
  50.  
  51. }
  52.  
  53. // run time polymorphism
  54.  
  55. class math{
  56. void sum(int a , int b){
  57. System.out.println( a+b);
  58. }
  59. }
  60.  
  61. class cal extends math{
  62. @Override
  63. void sum(int a , int b){
  64. System.out.println(a*b);
  65. }
  66. }
  67.  
  68. // Abstraction : hiding complex implementation details and exposing only the essential features
  69.  
  70. // Abstraction is achieved using:Abstract classes:
  71.  
  72. abstract class Animalpark{
  73. abstract void food();
  74. }
  75.  
  76. class dog extends Animalpark{
  77. void food(){
  78. System.out.println("Dog food");
  79. }
  80. }
  81.  
  82. class cat extends Animalpark{
  83. void food(){
  84. System.out.println("cat food");
  85. }
  86. }
  87.  
  88. public class Main
  89. {
  90. public static void main(String[] args) {
  91. // 1.
  92. // Company com = new Company("Aman",1);
  93. // com.setID(123);
  94. // System.out.println(com.getName()+" "+com.getID());
  95. // 2.
  96. // Cat ani= new Cat();
  97. // ani.catFood();
  98. // ani.food();
  99.  
  100. // 3.
  101. // compiletime ct= new compiletime();
  102. // ct.sum(1,2);
  103. // ct.sum(1.2,2.3);
  104.  
  105. // 4.
  106. // cal s=new cal();
  107. // s.sum(1,2);
  108.  
  109. // 5.
  110. // cat a1=new cat();
  111. // a1.food();
  112.  
  113. // dog b1=new dog();
  114. // b1.food();
  115. }
  116. }
  117.  
Success #stdin #stdout 0.08s 54624KB
stdin
Standard input is empty
stdout
Standard output is empty