Core Java Tutorial

 
 

OOPs Concepts in java

As we already seen that java is a Object Oreinted Programming language.Here we are going to know about Java core concepts(i.e OOPs concepts).There are mainly 4 core concepts are there.They are:

       1.Inheritance
       2.Polymorphism
       3.Encapsulations
       4.Abstraction

Inherirance

Inheritance is a concept of "one Object aquiring properties(fileds and functions) of another object(like child getting his father habits)".The object which is aquiring the properties of another object is called Sub class and the object from which Sub class getting properties is called Super class.Inheritance is achieved by using keyword "extends".Super class have many Sub classes but Sub class will have only one Super calss and Sub class may have all the properties of Super class and also it may decalre some extra if required.Inheritance is used in

   1. Method Overriding
   2. Code reusability
   Inheeritance Ex:
                   // This is super class
                 public class Example{
                    String name ="Hello";
                    int number=25;

                     public void output(){
                      System.out.println("name and numbers are: "+name+""+number);
                       }
                 }

                   // This is sub class
                 public class InherDemo extends Example{
                     String name ="Sub Hello";
                     int number=25;
                      // Method Overriding concept
                      public void output(){ //Here ouput() method in subclass ovrrides super class output() method
                      System.out.println("name and numbers are: "+name+" "+number);
                       }

                     public void output1(){
                      System.out.println("name and numbers are: "+name); // here i am ussing same variables which are present in super class(i.e using inheritance concept)
                       } 
                  public static void main(String[] args){
                     
              InherDemo  inheritanceDemo = new InherDemo ();
                               inheritanceDemo.output();
                               inheritanceDemo.output1();

          }
     }
  
    
   Output = name and numbers are: Sub Hello 25
            name and numbers are: Sub Hello
Key Points
1.Inheritance is achiebed by using extends keyowrd.
2.Super class can have multiple sub classes but sub class will have only one super class.
3.Multiple inhritance is not possible in java.
4.In exception handling, if sub class want to throw an exception then it must be declared in super class before sub class throwing that exception.
5.Inheritance is used for code reusabilitty and overriding.

Ploymorphism

Polymorphism in java is one of the important concept.Polymorphism means something which can take many forms.i.e In java Object will acts in different forms

  EX:
      class BookPerson; 
      class SalesPerson;
      interface AutoPerson;
      class extends SalesPerson implements AutoPerson;
  Here Person is one of the Object and It was takimg different forms.
      Peron Object is a SalesPerson, Person Object is a AutoPerson, Persond object is a Person

Java supports Polymorphism in Method Overloading(Click here to know about Method Overloading) and Method Overriding(This is covered in Inheritance).

Method Overloading vs Method Overriding

  1. Method Overloading will occur in same class but Method Overriding will occur in two classes(Super class and Sub class)
  2. In Method Overloading, two methods will have same method name but different parameters where as in Method overriding two methods will have exactle same name and same parameters.
  3. Method Overloading is Compiletime Polymorphism, Method Overriding is Runtime Polymorphism.
  4. Static binding is being used in Overloading and Dynamic binding is being used in Overriding.
  5. Ovelloading gives more perforace than Overriding

Encapsulation

Encapsulation in java, is defined as wrapping data(variables and methods) into single unit.The main purpose of encapsulation in java is to protect data.In java Encapsulation is achieved by declaring variables as a private and declaring corresponding methods as a public to call that particular variable.

     Ex:
         public class EncapsDemo{
            private String uniName="ABC";
            private int uniId = 123; 
          
            // declaring getters and setters
            public String getUniName(){
               return uniName;
               }  

            public String getUniName(){
               return uniId;
               } 

            public String setUniName(String newUniName){
               uniName = newUniName;
               } 

            public String setUniName(int newUniId){
               uniId=newUniId;
               } 

Here uniName nad uniId are private so these variables are not visible out side the class or inside a method. If you want to use them you need to call them using methods.

Abstraction

Java supports abstraction by using interface and abstraction classes.Abstraction is nothing but hiding the unnecessary data and showing only those functionalities.abstract classes are declared by using abstract keyword.Interfaces will give 100% Abstraction in java but Abstraction classes will give partial Abstraction.Abstract classes can't be instantiated.

     Ex:   
         // this is abstract class , you can't istantiate this
         // Demo.java
        public abstract class Demo {
	private String name;
	private int number;
	public Demo(String name, int number) {
		super();
		this.name = name;
		this.number = number;
	}
	public void  info(){
		System.out.println("My name is: "+name+"and my number is: "+number);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	
}

      // DemoTest.java
     public class DemoTest extends Demo {
     private int id;
     
	public DemoTest(String name, int number, int id) {
		super(name, number);
		this.id=id;
		
	}
	public void  info(){    // here i am implementing the abstract class method
		System.out.println("My name is: "+getName()+" and my number is: "+getNumber()+ " id is: "+id);
	}
	
      public static void main(String[] args) {
	Demo demo = new DemoTest("Hello",123,456);
	   demo.info();
}
}

Key Points

  1. Abstract classes and methods are declared with abstract keyword
  2. If any of method in a class don't have the body then it is abstract class
  3. Abstract class may or may not have the abstract methods