Core Java OOPS Programme2

package inht;
public class inheritaance
{
public static void main(String[] args)
{
Add obj = new Add();
obj.num1=5;
obj.num2=4;
obj.sum();
System.out.println(obj.result);
}
}
class Add{
int num1,num2,result;
public void sum()
{
result = num1+num2;
}
}

Output:-


package inht;
public class inheritt
{
public static void main(String[] args)
{
AddSub obj = new AddSub();
obj.num1=5;
obj.num2=4;
obj.sum();
System.out.println(obj.result);
obj.sub();
System.out.println(obj.result);
}
}
class Y{
int num1,num2,result;
public void sum()
{
result = num1+num2;
}
}
class AddSub extends Y{
public void sub()
{
result = num1-num2;
}
}



PolyMorPhism

MethodOverloading- In a Class , Same name of methods but with different type or number of parameters.

package inht;
public class Polymory{
public static void main(String[] args){
A obj = new A();
obj.show(5.5);
}
}
class A{
public void show(){
System.out.println("Hello");
}
public void show (int i){
System.out.println("Hello"+i);
}
public void show (double d){
System.out.println("Hello"+d);
}
}                                                 Output:-

Polymorphism

MethodOverloading

package inht;
public class Polymory{
public static void main(String[] args){
B obj = new B();
obj.show();
}
}
class A{
public void show(){
System.out.println("Showing A");
}
}

class B extends A{

}

https://www.youtube.com/watch?v=-Alpmo-OfJs&index=71&list=PLsyeobzWxl7oZ-fxDYkOToURHhMuWD1BK

https://www.youtube.com/watch?v=RcIsb9iFKH8&index=86&list=PLsyeobzWxl7oZ-fxDYkOToURHhMuWD1BK


We will Create Two Classes here by Name Employee and EmployeeTest

Make another class by Employee:-

package Empl;
public class Employee{

   String name;
   int age;
   String designation;
   double salary;
   // This is the constructor of the class Employee
   public Employee(String name){
      this.name = name;
   }
   // Assign the age of the Employee  to the variable age.
   public void empAge(int empAge){
      age =  empAge;
   }
   /* Assign the designation to the variable designation.*/
   public void empDesignation(String empDesig){
      designation = empDesig;
   }
   /* Assign the salary to the variable salary.*/
   public void empSalary(double empSalary){
      salary = empSalary;
   }
   /* Print the Employee details */
   public void printEmployee(){
      System.out.println("Name:"+ name );
      System.out.println("Age:" + age );
      System.out.println("Designation:" + designation );
      System.out.println("Salary:" + salary);
   }
}

Make another Class by name EmployeeTest:-

package Empl;
import java.io.*;
public class EmployeeTest{

   public static void main(String args[]){
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.printEmployee();

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}


Output:-



Encapsulation


package Empl;
public class Encapsul{
public static void main (String[] args)
{
Emp e1= new Emp();
e1.setEmpId(3);
e1.setEmpName("SystemWebTech");
//Emp e2= new Emp();
//e2.empId=4;
//e2.empName="SystemWebTechOnline";
System.out.println(e1.getEmpName());
}
}
class Emp
{
private int empId;
private String empName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
'
Output:-