Java protected access specifier

Protected Access Specifier

Consider the following example -

package com.c4learn.inheritance;
public class ProtectedExample {
   public void publicMethod(){
   }
   private void privateMethod(){
   }
   protected void protectedMethod(){
   }
   public static void main(String[] args) {
       ChildClass c1 = new ChildClass();
       c1.testMethods();
   }
}
class ChildClass extends ProtectedExample {
   public void testMethods() {
      publicMethod();
      protectedMethod();
      privateMethod();  //Error
   }
}

Output :

Compile Time Error

Explanation :

  1. From subclass we cannot access the private members of the superclass.
  2. subclass you can access its superclass’s public and protected methods and fields
  3. If the subclass and the superclass are in the same package, you can also access the superclass’s default methods and fields

Java HAS-A relationship : aggregation

Consider we are storing the information of the student, then we may create a class like below -

Class Student {
   int roll;
   String sname;
   Address address;
}

In the above class we have entity reference “Address” which stores again its own information like street,city,state,zip. like below -

Class Address {
   String street;
   String state;
   String zip;
   String city;
}

so we can say that Student HAS-A Address Thus if the class has entity reference then entity will represent the HAS-A relationship.

Consider the following example -

Address.java

public class Address {
    String street;
    String city;
    String state;
    String zip;
   public Address(String street, String city, 
		      String state, String zip) {
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
   }
}

Student.java

public class Student {
   int roll;
   Address address;
   Student(int rollNo,Address addressDetail){
      roll = rollNo;
      address = addressDetail;
   }
    void printStudentDetails(Address address1) {
       System.out.println("Roll   : " + roll);
       System.out.println("Street : " + address1.street);
       System.out.println("City   : " + address1.city);
       System.out.println("State  : " + address1.state);
       System.out.println("Zip    : " + address1.zip);
    }	
    public static void main(String[] args) {
       Address address1;
       address1 = new Address("1-ST","PN","Mah","41");
       Student s1 = new Student(1,address1);
       s1.printStudentDetails(address1);
    }
}

Output :

Roll   : 1
Street : 1-ST
City   : PN
State  : Mah
Zip    : 41

Explanation : Advantages of Using Aggregation

  1. You can see the above code in which we already have the class “Address” which is used to store the details of address. Thus using aggregation we have reused the existing class.
  2. Inheritance can be achieved only using the IS-A relationship but using this HAS-A we can use existing code more efficiently.

Java extends keyword

Suppose we are having the one already existing class, we call it as “Parent Class”. Suppose we need to create another class having exactly same attributes to that of the parent class and some extra attributes then we use “extend” keyword to extend the class.

Extends Keyword :

We can extend a class by using the extends keyword in a class declaration after the class name and before the parent class.

public class ParentClass {
}

and we define child class like shown below -

public class ChildClass extends ParentClass {
}
TypeNameExplanation
Super ClassParentClassThe class from which another class is derived is called the superclass
Sub ClassChildClassThe derived class (the class that is derived from another class) is called a subclass

Important Note : Extends Keyword

  1. In the Java All the classes are child classes of java.lang.Object
  2. Object is the Super Class of all the classes in Java
  3. Like C++ multiple inheritance is not allowed in Java. Class can only extend one class.

Extends Keyword : Object is Superclass

Java instanceOf keyword

In the previous chapter we have learnt about the IS-A relationship between subclass and superclass. In this chapter we will be learning the programatic implementation of IS-A relationship.

Example : IS-A Relationship Verification

class FourWheeler extends Vehicle{}
class TwoWheeler extends Vehicle{}
class WagonR extends FourWheeler{}
public class Vehicle 
{
    public static void main( String[] args )
    {
    	FourWheeler v1 = new FourWheeler();
    	TwoWheeler v2 = new TwoWheeler();
    	WagonR v3 = new WagonR();
        System.out.println(v1 instanceof Vehicle);
        System.out.println(v2 instanceof Vehicle);
        System.out.println(v3 instanceof Vehicle);
        System.out.println(v3 instanceof FourWheeler);
    }
}

Output :

true
true
true
true

InstanceOf Operator :

In the above example we have used this following statement -

System.out.println(v1 instanceof Vehicle);

the instanceof operator is used to check whether TwoWheeler is actually a Vehicle, and FourWheelet is actually a Vehicle.

Invalid Use of InstanceOf Operator :

System.out.println(v3 instanceof TwoWheeler);

Java IS-A relationship

Java - Inheritance Basics :

NoTermDefinition
1InheritanceInheritance is a process where one object acquires the properties of another object
2SubclassClass which inherits the properties of another object is called as subclass
3SuperclassClass whose properties are inherited by subclass is called as superclass
4Keywords Usedextends and implements

IS-A Relationship with Example :

IS-A is a way of saying : This object is a type of that object.

public class Vehicle{
}
public class FourWheeler extends Vehicle{
}
public class TwoWheeler extends Vehicle{
}
public class WagonR extends FourWheeler{
}

Conclusions from above Example :

From the above example we can say that -
[box]

  1. Vehicle is the superclass of TwoWheeler class.
  2. Vehicle is the superclass of FourWheeler class.
  3. TwoWheeler and FourWheeler are sub classes of Vehicle class.
  4. WagonR is the subclass of both FourWheeler and Vehicle classes.

[/box]

IS-A relationship of above example is -

TwoWheeler  IS-A Vehicle
FourWheeler IS-A Vehicle
WagonR 	    IS-A FourWheeler

Hence

WagonR 	    IS-A Vehicle