Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Java Inheritance Part 1: Inheriting Public Members

Inheritance:

Inheritance is a process where one class acquires the properties (variable and methods) of another class.

Importance:

  • For code reusability.
  • For method overloading.
  • Implement parent-child relationship.


Code:

  1. // Base class  
  2.   
  3. public class StudentI {  
  4.       
  5.     int id;  
  6.     String name;  
  7.     String dept;  
  8.       
  9.     public void dispInfo() {  
  10.         System.out.println("Id : "+id);  
  11.         System.out.println("Name : "+name);  
  12.         System.out.println("Department : "+dept);  
  13.     }  
  14.   
  15. }  
  16.   
  17. // Derived class  
  18.   
  19. public class TeacherI extends StudentI { //inherit class studenti  
  20.       
  21.     int age;  
  22.       
  23.     public void disp() {  
  24.         dispInfo();  
  25.         System.out.println("Age : "+age);  
  26.     }  
  27.   
  28. }  
  29.   
  30. // Main class  
  31.   
  32.   
  33. public class InheritanceDemo {  
  34.   
  35.     public static void main(String[] args) {  
  36.         // TODO Auto-generated method stub  
  37.           
  38.         StudentI s1=new StudentI();  
  39.         s1.id=1001;  
  40.         s1.name="Sujan Hasan";  
  41.         s1.dept="CSE";  
  42.         s1.dispInfo();  
  43.         System.out.println();  
  44.           
  45.         StudentI s2=new StudentI();  
  46.         s2.id=1002;  
  47.         s2.name="Shakil Sorker";  
  48.         s2.dept="CSE";  
  49.         s2.dispInfo();  
  50.         System.out.println();  
  51.           
  52.         TeacherI t1=new TeacherI();  
  53.         t1.id=7001;  
  54.         t1.name="Alamgir Hossain";  
  55.         t1.dept="CSE";  
  56.         t1.age=30;  
  57.         t1.disp();  
  58.         System.out.println();  
  59.           
  60.         TeacherI t2=new TeacherI();  
  61.         t2.id=7003;  
  62.         t2.name="Shamol Pal";  
  63.         t2.dept="CSE";  
  64.         t2.age=37;  
  65.         t2.disp();  
  66.         System.out.println();  
  67.           
  68.   
  69.     }  
  70.   
  71. }  
Share:

Java Encapsulation

Encapsulation:

Encapsulation is a process of packaging variables and method into a single unit and protecting data by declaring them as private. Where private is hidden from the other classes and the can only be accessed through the method of their current class. This is known as data hiding.

Process:

  • Declare the variables as private.
  • Provide public setter and getter method to modify and get the variables value.
Benefits:

  • Provides data hiding.
  • Reusability
  • Code can be modified without breaking the code.
  • Reduce complexity.
Code:

  1. public class PersonE {  
  2.       
  3.     private String name;  
  4.     private String gender;  
  5.     private int age;  
  6.       
  7.     public String getName() { //Getter method  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) { //Setter method  
  11.         this.name = name;  
  12.     }  
  13.     public String getGender() {  
  14.         return gender;  
  15.     }  
  16.     public void setGender(String gender) {  
  17.         this.gender = gender;  
  18.     }  
  19.     public int getAge() {  
  20.         return age;  
  21.     }  
  22.     public void setAge(int age) {  
  23.         this.age = age;  
  24.     }  
  25.       
  26.       
  27.   
  28. }  
  29.   
  30. // Main class  
  31.   
  32.   
  33. public class EncapsulationDemo {  
  34.   
  35.     public static void main(String[] args) {  
  36.         // TODO Auto-generated method stub  
  37.           
  38.         PersonE p1=new PersonE();  
  39.         p1.setName("Sujan Hasan");  
  40.         p1.setGender("Male");  
  41.         p1.setAge(21);  
  42.         print(p1);  
  43.         System.out.println();  
  44.           
  45.         PersonE p2=new PersonE();  
  46.         p2.setName("Shimul Hossain");  
  47.         p2.setGender("Male");  
  48.         p2.setAge(22);  
  49.         print(p2);  
  50.         System.out.println();  
  51.           
  52.         PersonE p3=new PersonE();  
  53.         p3.setName("Shakil Sorker");  
  54.         p3.setGender("Male");  
  55.         p3.setAge(22);  
  56.         print(p3);  
  57.         System.out.println();  
  58.           
  59.           
  60.   
  61.     }  
  62.     public static void print(PersonE obj) {  
  63.         System.out.println(obj.getName());  
  64.         System.out.println(obj.getGender());  
  65.         System.out.println(obj.getAge());  
  66.     }  
  67.   
  68. }  
Share:

Java Constructor

Constructor:

Constructor is a special type of method that is used to initialize the object.

Properties of constructor:

  • Has the same name that the class belong to.
  • Has no return type.
  • Called automatically.
  • Default constructor (no parameter) , Parametrized constructor
Code:

  1. public class EmployeeC {  
  2.       
  3.     int eid;  
  4.     String ename;  
  5.     String egender;  
  6.     String edept;  
  7.     //Parameterized Constructor implementation  
  8.     public EmployeeC(int id,String name,String gender,String dept) {  
  9.         eid=id;  
  10.         ename=name;  
  11.         egender=gender;  
  12.         edept=dept;  
  13.     }  
  14.       
  15.     public void showData() {  
  16.         System.out.println("\nEmployee_id: "+eid+"\nEmployee_name: "+ename+"\nEmployee_gender: "+egender+"\nEmployee_department: "+edept);  
  17.     }  
  18.   
  19. }  
  20.   
  21.   
  22. // Main class  
  23.   
  24.   
  25. public class ConstractorDemo {  
  26.   
  27.     public static void main(String[] args) {  
  28.         // TODO Auto-generated method stub  
  29.         EmployeeC emp1=new EmployeeC(101,"Sujan Hasan","Male","IT");  
  30.         emp1.showData();  
  31.           
  32.         EmployeeC emp2=new EmployeeC(100,"Shimul Hossain","Male","Marketing");  
  33.         emp2.showData();  
  34.           
  35.         EmployeeC emp3=new EmployeeC(102,"Shakil Sorker","Male","Networking");  
  36.         emp3.showData();  
  37.   
  38.     }  
  39.   
  40. }  
Share: