Core Java - OOPS


Java is an object-oriented programming language. OOPS stands for (Object Oriented Programming System).
Everything in Java is associated with classes and objects, along with its attributes and methods.
For eg. in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such as drive and brake.

Class :
A class is defined as a collection of objects. You can also think of a class as a blueprint from which you can create an individual object.
Syntax :
    class ClassName {
        // states (or) non-static variable
        // behaviours (or) non-static methods
    }
In the above syntax, we have states (also called variables) and methods, which represent the state and behavior of the object, respectively.
Note : that in Java, we use fields to store data, while we use methods to perform operations.

Object :
An object is an entity in the real world that can be distinctly identified. Objects have states and behaviors. In other words, they consist of methods and properties to make a particular type of data useful.

An object consists of:
  • A unique identity : Each object has a unique identity, even if the state is identical to that of another object.
  • State/Properties/Attributes : State tells us how the object looks or what properties it has.
  • Behaviours : Behaviour tells us what the object does.

  • Example of object states and behaviors in Java :
    Let's look at some real-life examples of the states and behaviors that objects can have.
    Example 1:
    • Object : car.
    • State : color, brand, weight, model.
    • Behavior : break, accelerate, turn, change gears.
    Example 2:
    • Object : house.
    • State : address, color, location.
    • Behavior : open door, close door, open blinds.
    Syntax :
        public class Number{
            int a = 10;
            public static void main(String[] args){
            Number myObj = new Number();
            System.out.println(myObj.a);
            }
        }
    

    Principles of OOPS :
  • Encapsulation
  • Inheritance
  • Abstraction and Interface
  • Polymorphism

  • 1. Abstraction :
    Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
    If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

    Abstract Class :
    Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.
    Eg:
        public abstract class Employee{
            private String name;
            private String address;
            private int number;
            public Employee(String name,String address,int number){
            System.out.println("Constructing an Employee");
            this.name = name;
            this.address = address;
            this.number = number;
            }
            public double Payment(){
            System.out.println("Inside Employee Payment");
            return 0.0;
            }
            public void mailCheck(){
            System.out.println("Mailing a check to "+ this.name +" "+ this.address);
            }
            public String toString(){
            return name +" "+ address +" "+ number;
            }
            public String getName(){
            return name;
            }
            public String getAddress(){
            return address;
            }
            public void setAddress(String newAddress){
                address = newAddress;
            }
            public int getNumber(){
            return number;
            }
        }
    
    Notice that nothing is different in this Employee class. The class is now abstract, but it still has three fields, seven methods, and one constructor.
    Now if you would try as follows:
        public class DemoEmployee {    
            public static void main(String[] args){
            /* Following is not allowed and would raise error */
            Employee e =new Employee("Smith","Syndey, Aus",66);
            System.out.println("\n Call mailCheck usingEmployee reference--");
            e.mailCheck();
            }
        }
    
    When you would compile above class, then you would get the following error
        Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
            Cannot instantiate the type Employee
            at com.sdbt.happy.DemoEmployee.main(DemoEmployee.java:9)
    
    
    Extending Abstract Class :
    We can extend Employee class in normal way as follows:
        /* File name : Salary.java */
        public class Salary extends Employee{
            
        private double salary;//Annual salary
    
        //helper methods
            public double getSalary(){
            return salary;
            }
            public void setSalary(double newSalary){
            if(newSalary >=0.0)
             salary = newSalary;
            }
        //methods	
            public Salary(String name,String address,int number,double salary){
            super(name, address, number);
            setSalary(salary);
            }
            public void mailCheck(){
            System.out.println("Within mailCheck of Salary class ");
            System.out.println("Mailing check to "+ getName()+" with salary "+ salary);
            }
            public double Payment(){
            System.out.println("Payment salary pay :"+ getName());
            return salary/52;
            }
        }
    
    Here, we cannot instantiate a new Employee, but if we instantiate a new Salary object, the Salary object will inherit the three fields and seven methods from Employee.
        public class DemoSalaryEmployee {
            /* File name : AbstractDemo.java */
            public static void main(String[] args){
            Salary s =new Salary("Smith","Syndey, Aus",66,1500000.00);
            Employee e =new Salary("Kohli","Delhi, India",18,2000000.00);
            
            System.out.println("-----------------------------------------");
            System.out.println("Call mailCheck using Salary reference :");
            s.mailCheck();
    
            System.out.println("-----------------------------------------");
            System.out.println("Call mailCheck using Employee reference :");
            e.mailCheck();
            }
        }
    
    Output :
    Constructing an Employee
    Constructing an Employee
    -----------------------------------------
    Call mailCheck using Salary reference :
    Within mailCheck of Salary class
    Mailing check to Smith with salary 1500000.0
    -----------------------------------------
    Call mailCheck usingEmployee reference :
    Within mailCheck of Salary class
    Mailing check to Kohli with salary 2000000.0

    Abstract Methods :
    If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.
    The abstract keyword is also used to declare a method as abstract. An abstract method consists of a method signature, but no method body.
    Abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows:
        public abstract class Employee{
        private String name;
        private String address;
        private int number;
        public abstract double Payment();
        //Remainder of class definition
        }
    
    Declaring a method as abstract has two results :
  • The class must also be declared abstract. If a class contains an abstract method, the class must be abstract as well.
  • Any child class must either override the abstract method or declare itself abstract.

  • A child class that inherits an abstract method must override it. If they do not, they must be abstract and any of their children must override it.
    Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.
    If Salary is extending Employee class, then it is required to implement computePay() method as follows:
        public class Salary extends Employee{
        private double salary;// Annual salary
        public double Payment(){
        System.out.println("Payment salary  for :"+ getName());
        return salary/52;
        }
        //Remainder of class definition
        }
    

    Interface :
    An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
    An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts.
    A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

    An interface is similar to a class in the following ways:
  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The bytecode of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

  • However, an interface is different from a class in several ways, including:
  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

  • Declaring Interfaces :
    The interface keyword is used to declare an interface. Here is a simple example to declare an interface:
    Eg:
        import java.lang.*;
        //Any number of import statements
        public interface NameOfInterface{
        //Any number of final, static fields
        //Any number of abstract method declarations
        }
    
    Interfaces have the following properties :
  • An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

  • Eg:
        interface Animal{
        public void eat();
        public void travel();
        }
    
    Implementing Interfaces :
    When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.
    A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
    Eg:
        public class MammalInt implements Animal{
            public void eat(){
            System.out.println("Mammal eats");
            }
            public void travel(){
            System.out.println("Mammal travels");
            }
            public int noOfLegs(){
            return 0;
            }
            public static void main(String args[]){
            MammalInt m =new MammalInt();
             m.eat();
             m.travel();
            }
        }
    
    Output :
    Mammal eats
    Mammal travels

    When overriding methods defined in interfaces there are several rules to be followed:
  • Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
  • The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
  • An implementation class itself can be abstract and if so interface methods need not be implemented.

  • When implementation interfaces there are several rules:
  • A class can implement more than one interface at a time.
  • A class can extend only one class, but implement many interfaces.
  • An interface can extend another interface, similarly to the way that a class can extend another class.


  • Extending Interfaces :
    An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
    The following Sports interface is extended by Hockey and Football interfaces.
    Eg:
        //Filename: Sports.java
        public interface Sports{
            public void setHomeTeam(String name);
            public void setVisitingTeam(String name);
            }   
            //Filename: Cricket.java
            public interface Cricket extends Sports{
            public void homeTeamScored(int points);
            public void visitingTeamScored(int points);
            public void endOfQuarter(int quarter);
            }   
            //Filename: Hockey.java
            public interface Hockey extends Sports{
            public void homeGoalScored();
            public void visitingGoalScored();
            public void endOfPeriod(int period);
            public void overtimePeriod(int ot);
        }
    
    The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Cricket needs to define the three methods from Cricket and the two methods from Sports.

    Extending Multiple Interfaces :
    A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.
    The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
    Eg:
        public interface Hockey extends Sports,Event
    
    Therefore interface extended with both Sports and Event, it would be declared as above example showed.


    2. Polymorphism :
    Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP, occurs when a parent class reference is used to refer to a child class object.
    Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
    It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed. The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.
    A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

    Eg:
        public interface Vegetarian{}
        public class Animal{}
        public class Deer extends Animal implements Vegetarian{}
    
    Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:
  • A Deer IS-A Animal
  • A Deer IS-A Vegetarian
  • A Deer IS-A Deer
  • A Deer IS-A Object

  • When we apply the reference variable facts to a Deer object reference, the following declarations are legal:
        Deer d =new Deer();
        Animal a = d;
        Vegetarian v = d;
        Object o = d;
    
    All the reference variables d,a,v,o refer to the same Deer object in the heap.

    Virtual Methods :
    In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage of polymorphism when designing your classes. We already have discussed method overriding, where a child class can override a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.
    Eg:
        /* File name : Employee.java */
        public class Employee{
            private String name;
            private String address;
            private int number;
    
            public Employee(String name,String address,int number){
            System.out.println("Constructing an Employee");
            this.name = name;
            this.address = address;
            this.number = number;
            }
            
            public String getName(){
            return name;
            }
            public String getAddress(){
            return address;
            }
            public int getNumber(){
            return number;
            }
            
            public void mailCheck(){
            System.out.println(" Mailing a check to "+ this.name + " " + this.address);
            }
            public void setAddress(String newAddress){
            address = newAddress;
            }
            
            public String toString(){
            return name +" "+ address +" "+ number;
            }    
        }
    
    Now, you study the following program carefully and try to determine its output:
        /* File name : VirtualDemo.java */
        public class VirtualDemo{
            public static void main(String[] args){
            Salary s =new Salary("Smith","Syndey, Aus",66,1500000.00);
            Employee e =new Salary("Kohli","Delhi, India",18,2000000.00);
            
            System.out.println("-----------------------------------------");
            System.out.println("Call mailCheck using Salary reference :");
            s.mailCheck();
    
            System.out.println("-----------------------------------------");
            System.out.println("Call mailCheck using Employee reference :");
            e.mailCheck();
            }
        }
    
    Output :
    Constructing an Employee
    Constructing an Employee
    -----------------------------------------
    Call mailCheck using Salary reference :
    Within mailCheck of Salary class
    Mailing check to Ramesh with salary 15000.0
    -----------------------------------------
    Call mailCheck usingEmployee reference :
    Within mailCheck of Salary class
    Mailing check to Suresh with salary 20000.0


    Here, we instantiate two Salary objects, one using a Salary reference s, and the other using an Employee reference.
    While invoking s.mailCheck() the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time.
    Invoking mailCheck() on e is quite different because e is an Employee reference. When the compiler seese.mailCheck(), the compiler sees the mailCheck() method in the Employee class.
    Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however, the JVM invokes mailCheck() in the Salary class.
    This behaviour is referred to as virtual method invocation, and the methods are referred to as virtual methods. All methods in Java behave in this manner, whereby an overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.


    3. Inheritance :
    Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance, the information is made manageable in a hierarchical order.
    When we talk about inheritance, the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

    IS-A Relationship :
    IS-A is a way of saying : This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
    Eg:
        public class Animal{
            //statements
        }
        public class Mammal extends Animal{
            //statements
        }
        public class Dog extends Mammal{
            //statements
        }    
    
    Now, based on the above example, In Object Oriented terms the following are true:
  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are subclasses of Animal class.
  • Dog is the subclass of both Mammal and Animal classes.

  • Now, if we consider the IS-A relationship, we can say:
  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal
  • Hence : Dog IS-A Animal as well

  • With use of the extends keyword the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.
    We can assure that Mammal is actually an Animal with the use of the instance operator
    Eg:
        public class Dog extends Mammal{
            public static void main(String args[]){
            Animal a =new Animal();
            Mammal m =new Mammal();
            Dog d =new Dog();
            System.out.println(m instanceof Animal);
            System.out.println(d instanceof Mammal);
            System.out.println(d instanceof Animal);
            }
        }
    
    Output :
    true
    true
    true
    Since we have a good understanding of the extends keyword, let us look into how the implementskeyword is used to get the IS-A relationship. The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended by the classes.
    Eg:
        //interface
        public interface Animal{
            //statements
        }
        //class 1
        public class Mammal implements Animal{
            //statements
        }
        //class 2
        public class Dog extends Mammal{
            //statements
        }
    


    The instanceof Keyword :
    Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal
        //interface
        interface Animal{}
        //class 1
        public class Mammal implements Animal{}
        //class 2
        public class Dog extends Mammal{
            public static void main(String args[]){
            Mammal m =new Mammal();
            Dog d =new Dog();
            System.out.println(m instanceof Animal);
            System.out.println(d instanceof Mammal);
            System.out.println(d instanceof Animal);
            }
        }
    
    Output :
    true
    true
    true

    HAS-A relationship :
    These relationships are mainly based on the usage. This determines whether a certain class HAS-Acertain thing. This relationship helps to reduce duplication of code as well as bugs.
    Eg:
        //class 1
        public class Vehicle{
            //statements
        }
        //class 2
        public class Speed{
            //statements
        }
        //class 3
        public class Van extends Vehicle{
        private Speed sp;
        }
    
    This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class which makes it possible to reuse the Speed class in multiple applications.
    In Object-Oriented feature, the users do not need to bother about which object is doing the real work. To achieve this, the Van class hides the implementation details from the users of the Van class. So basically what happens is the users would ask the Van class to do a certain action and the Van class will either do the work by itself or ask another class to perform the action.
    A very important fact to remember is that Java only supports only single inheritance. This means that a class cannot extend more than one class. Therefore following is illegal:
        public class extends Animal,Mammal{}
    
    However, a class can implement one or more interfaces. This has made Java get rid of the impossibility of multiple inheritance.


    4. Encapsulation :
    Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
    Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
    Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
    The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
    Eg:
        public class BasicEncap{
            private String name;
            private String idNum;
            private int age;
            //helper methods
            public String getName(){
            return name;
            }
            public String getIdNum(){
            return idNum;
            }
            public int getAge(){
                return age;
                }
            public void setName(String newName){
                name = newName;
            }    
            public void setAge(int newAge){
                age = newAge;
            }
            //methods
            public void setIdNum(String newId){
                idNum = newId;
            }	
        }
    
    The public methods are the access points to this class fields from the outside java world. Normally, these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters. The variables of the EncapsTest class can be access as below:
        /* File name : RunEncap.java */
        public class RunEncap{
            public static void main(String args[]){
            BasicEncap encap =new BasicEncap();
            encap.setName("Rohit");
            encap.setAge(36);
            encap.setIdNum("45");
            System.out.println("Name : "+ encap.getName());
            System.out.println("Age : "+ encap.getAge());
            System.out.println("Id : "+encap.getIdNum());
            }
        }
    
    Output :
    Name : Rohit
    Age : 36
    Id : 45

    Benefits of Encapsulation :
    The fields of a class can be made read-only or write-only.
    A class can have total control over what is stored in its fields.
    The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.


    (Core Java - Package)