9.Working with inheritance-I Flashcards
There are two things that a class contains …
There are two things that a class contains :
- the instance fields defined in the class, i.e., state,
- the instance methods that provide behaviour to the class, i.e., implementation.
A subclass can inherit
Inheritance could be of state, it could be of implementation, and it could be of type.
- state -> instance fields
- implementation -> the instance methods that provide behaviour to the class
- type -> parent class type
Inheritance of state
Only a class can contain state and therefore, only a class can extend another class. Furthermore, Java restricts a class from extending more than one class and that is why it is often said that Java does not support multiple inheritance. However, technically, it would be more precise to say that Java does not support multiple inheritance of state.
Inheritance of implementation
A class can inherit implementation by extending a class and/or by implementing interfaces. This allows a class to inherit implementations from more than one types. This is one form of multiple implementation inheritance. However, due to the way default methods are inherited in a class, it is still not possible for a class to inherit more than one implementation of a method in Java.
Inheritance of type
Java allows you to dene a type using an interface as well as a class (and an enum but that is not relevant to this discussion). Thus, a class can inherit behavior by extending a class and/or by implementing an interface. Since Java allows a class to implement multiple interfaces, it can be said that Java supports multiple inheritance of type.
What will be the output of this code? public class Person{ public String name; public String getName() { return name; } } public class Employee extends Person{ public String employeeId; public static void main(String args[]){ Person ee = new Employee(); ((Employee) ee).employeeId = "111"; ee.name = "Amy"; System.out.println(ee.getName()); System.out.println (((Employee) ee).employeeId); } }
If a parent class references to a subclass and you want to access the fields or methods in the subclass, an explicit cast must be made to subclass object: Example below:
public class Person{ public String name; public String getName() { return name; } } public class Employee extends Person{ public String employeeId; public static void main(String args[]){ Person ee = new Employee(); ((Employee) ee).employeeId = "111"; ee.name = "Amy"; System.out.println(ee.getName()); // prints "Amy" System.out.println (((Employee) ee).employeeId); // prints "111" } }
What will be the output of this code? public class Person{ public String name; public String getName() { return name; } public static int personCount; public static int getPersonCount(){ return personCount; } } public class Employee extends Person{ public String employeeId; public static void main(String args[]){ Employee ee = new Employee(); ee.employeeId = "111"; ee.name = "Amy"; System.out.println(ee.getName()); System.out.println (ee.employeeId); } }
Since Employee extends Person, it automatically "gets" the name field as well the getName method. The Employee class also "gets" the static variable personCount and the static method getPersonCount from Person class. Thus, it is possible to access personCount and getPersonCount in an Employee class as if they were defined in the Employee class itself. Members inherited by a class from its super class are as good as the members defined in the class itself and are therefore, passed on as inheritance to any subclass. For example, if you have a Manager class that extends an Employee class, the Manager class will inherit all members of the Employee class, which includes the members defined in Employee class as well as members inherited by Employee class from Person class. Do not confuse the static access to the static. members with the instance access. The static members will be accessible either as :
Employee.personCount
Employee.getPersonCount()
Person.personCount
Person.getPersonCount()
Are static fields an methods inherit by the subclass?
True, as long as the access modifiers allow..
If they are private, they wont be accessible..
Access modiers on inheritance
private - As explained above, private members are not inherited by a subclass.
default - Since a member with default access is visible only to a class in the same package, it can only be inherited in the subclass if the subclass belongs to the same package.
protected - This access modier is actually built to overcome the restriction imposed by the default modier. Recall that in addition to being visible to all classes of the same package, a protected member of a class is visible to another class that belongs to a dierent package if that class is a subclass of this class. Thus, even a subclass that belongs to another package inherits protected members of the super class.
public - public members are always inherited by a subclass.
Access modifiers in inheritance
Bookmark on the book section 9 in case of doubts
Access modifiers also apply to classes, and follow the same access rules. As an example, if a class is declared public and is extended, all classes that extend its child class will have access to all of its members, (conditional to their access modifiers). If a class as a default access modifier (no modifier declared), it will be visible inside the package in the child classes chain.
Inheritance of instance members vs static members
Inheritance of instance members vs static members