Advance Computer Programming: by Hasnat Ali

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Advance Computer Programming

By

Hasnat Ali
Inheritance, Polymorphism, and
Encapsulation
Inheritance
Inheritance in JAVA
public class A{
private int var1;
public int var2;
A(){ System.out.println(“Constructor A"); }
}
public class B extends A{
private int var3;
B(){ System.out.println(“ Constructor B"); }
}
public class C extends B{
private int var4;
C(){ System.out.println(“ Constructor C"); }
}

public class CheckConsCall{


public static void main(String[] args) {
C c = new C();
}
}
Declaring a Subclass

 A subclass extends properties and methods from the


superclass. You can also:

 Add new properties

 Add new methods

 Override the methods of the superclass


About Super Class’s Constructor

 Super class’s constructors are not inherited

 They are invoked explicitly or implicitly.


 Explicitly using the super keyword.

 A constructor is used to construct an instance of a class. Unlike


properties and methods, a superclass's constructors are not
inherited in the subclass.

 They can only be invoked from the subclasses' constructors,


using the keyword super. If the keyword super is not explicitly
used, the superclass's no-arg constructor is automatically
invoked.
About Super Class’s Constructor

 A constructor may invoke an overloaded constructor or its


superclass’s constructor. If none of them is invoked explicitly,
the compiler puts super() as the first statement in the
constructor. For example,

public A() { public A() {


is equivalent to
} super();
}

public A(double d) { public A(double d) {


// some statements is equivalent to
super();
} // some statements
}
About Super Class’s Constructor

class A{
A(){ System.out.println("A"); }
}
class B extends A{
B(int i){
// by default, here the first statement is super();
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}
About Super Class’s Constructor

class A{
A(int i){
System.out.println("A");
}
}
class B extends A{
B(int i){
// by default, here the first statement is super();
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}

Output:
Error, Required int argument, Found no arguments
About Super Class’s Constructor

class A{
A(int i){
System.out.println("A");
}
}
class B extends A{
B(int i){
super(i); // explicitly pass integer argument
System.out.println("B");
}
}
class Test{
public static void main(String args[]){
B b = new B(5);
}
}
Use of super Keyword

 The keyword super refers to the superclass of the class in


which super appears. This keyword can be used in two ways:

 To call a superclass constructor


 To call a superclass method

Caution
 You must use the keyword super to call the superclass
constructor. Invoking a superclass constructor’s name in a
subclass causes a syntax error.

 Java requires that the statement that uses the keyword super
appear first in the constructor.
Constructor Chaining

 Constructing an instance of a class invokes all the superclasses’ constructors


along the inheritance chain. This is called constructor chaining.
public class A{
A() { System.out.println(“ Constructor A"); }
}
public class B extends A{
B() { System.out.println(“ Constructor B"); }
}
public class C extends B{
C() { System.out.println(“ Constructor C"); }
}

public class CheckConsCall{


public static void main(String[] args) {
C c = new C();
}
}
Constructor Chaining - Example
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
1. Start from the main
new Faculty();
} method
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
2. Invoke Faculty
}
public Faculty() { Constructor
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 3. Invoke Employee’s No-
} arg Constructor
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
} 4. Invoke Person’s
class Employee extends Person { constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
} 5. Execute Statement
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
} 6. Calling Employee’s arg
class Employee extends Person { constructor
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
} 7. Execute Statement
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person { 8. Execute Statement
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Constructor Chaining – Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty(); 9. Execute Statement
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee’s overloaded constructor");
System.out.println(“(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Find Error?

public class Mammal extends Animal {


}

class Animal {
public Animal(String name) {
System.out.println(“Animal Class Constructor");
}
}
Object Class in Java

 Every class in Java is descended from the


java.lang.Object class. If no inheritance is specified when
a class is defined, the superclass of the class is Object.

public class Circle { public class Circle extends Object {


... Equivalent
...
} }
Methods of Object Class in Java

 List of methods in Object class of Java


 clone()

 equals()

 wait()

 finalize()

 getClass()

 hashCode()

 notify()

 notifyAll()

 toString()
Polymorphism
(Dynamic Method Dispatch)
Polymorphism and Method Overriding
public class A{
void callMe() { System.out.println(“ A’s callMe() Method”); }
}
public class B extends A{
void callMe() { System.out.println(“ B’s callMe() Method"); }
}
public class C extends B{
void callMe() { System.out.println(“ C’s callMe() Method"); }
}

public class MehodCallCheck{


public static void main(String[] args) {
A a = new A(); B b = new B(); C c = new C();
A ref;
ref = a; Object creation
ref.callMe();
of A, B, and C
ref = b;
ref.callMe();
ref = c;
ref.callMe();
}
}
Dynamic Method Dispatch

 Method overriding forms the basis of method dispatch

 Dynamic method dispatch is the mechanism by which a


call to an overridden function is resolved at runtime,
rather than compile time. (run-time polymorphism)

 Java determines which version of that method to execute


based upon the type of the object being referred to at
the time the call occurs
Dynamic Method Dispatch

 Dynamic binding works as follows: Suppose an object o is an


instance of class C1, where C1 is a subclass of C2, C2 is a subclass of
C3, ..., and Cn-1 is a subclass of Cn.
 That is, Cn is the most general class, and C1 is the most specific class.
 In Java, Cn is the Object class. If o invokes a method p, the JVM
searches the implementation for the method p in C1, C2, ..., Cn-1 and
Cn, in this order, until it is found. Once an implementation is found,
the search stops and the first-found implementation is invoked.

Cn Cn-1 ..... C2 C1

Since o is an instance of C1, o is also an


Object instance of C2, C3, …, Cn-1, and Cn
Dynamic Method Dispatch

public class A{
void callMe() { System.out.println(“ A’s callMe() Method”); }
}
public class B extends A{
void callMe() { System.out.println(“ B’s callMe() Method"); }
}
public class C extends B{
// void callMe() { System.out.println(“ C’s callMe() Method"); }
}

2. Try to find callMe()


in B
public class MehodCallCheck{
public static void main(String[] args) {
A a = new A(); B b = new B(); C c = new C();
A ref;
ref = c;
ref.callMe();
} 1. Try to find callMe()
} in C
Generic Programming
public class PolymorphismDemo {
public static void main(String[] args)
{
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());  Polymorphism allows methods to be
} used generically for a wide range of
object arguments. This is known as
public static void m(Object x) {
System.out.println(x.toString());
generic programming.
}  If a method’s parameter type is a
} superclass (e.g., Object), you may pass
class GraduateStudent extends Student {
} an object to this method of any of the
class Student extends Person { parameter’s subclasses (e.g., Student
public String toString() { or String).
return "Student";
}  When an object (e.g., a Student object
} or a String object) is used in the
method, the particular
class Person extends Object {
public String toString() { implementation of the method of the
return "Person"; object that is invoked (e.g., toString) is
} determined dynamically.
}
Important Note

 An instance method can be overridden only if it is


accessible. Thus a private method cannot be overridden,
because it is not accessible outside its own class. If a
method defined in a subclass is private in its superclass,
the two methods are completely unrelated.

 Like an instance method, a static method can be


inherited. However, a static method cannot be
overridden. If a static method defined in the superclass
is redefined in a subclass, the method defined in the
superclass is hidden.
Overriding vs Overloading

public class Test { public class Test {


public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }

class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }

class A extends B { class A extends B {


// This method overrides the method in B // This method overloads the method in B
public void p(double i) { public void p(int i) {
System.out.println(i); System.out.println(i);
} }
} }
Encapsulation
(Access Modifiers)
The Access Modifiers

 private
 default (none)
 protected
 public

Visibility increases

private, none (if no modifier is used), protected, public


Accessibility Summary

Modifier Accessed Accessed Accessed Accessed


on members from the from the from a from a different
in a class same class same package subclass package

public

protected -

default - -

private - - -
package p1; package p2;
public class C1 { public class C2 { public class C3 {
public int x; void aMethod() { void aMethod() {
int y; C1 o = new C1(); C1 o = new C1();
private int z; can access o.x; can access o.x;
can access o.y; cannot access o.y;
public void m1() { cannot access o.z; cannot access o.z;
}
void m2() { can invoke o.m1(); can invoke o.m1();
} can invoke o.m2(); cannot invoke o.m2();
private void m3() { cannot invoke o.m3(); cannot invoke o.m3();
} } }
} } }

package p1; package p2;


class C1 { public class C2 { public class C3 {
... can access C1 cannot access C1;
} } can access C2;
}

The private modifier restricts access to within a class, the default


modifier restricts access to within a package, and the public
modifier enables unrestricted access.
Accessibility Summary

package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }

package p2;

public class C3 public class C4 public class C5 {


extends C1 { extends C1 { C1 o = new C1();
can access x; can access x; can access o.x;
can access y; can access y; cannot access o.y;
can access z; cannot access z; cannot access o.z;
cannot access u; cannot access u; cannot access o.u;
can invoke m(); can invoke m(); cannot invoke o.m();
} } }
Visibility Modifiers and
Accessor/Mutator Methods
 default
By default, the class, variable, or method can be accessed by any
class in the same package.
 public
The class, data, or method is visible to any class in any package.
 private
The data or methods can be accessed only by the declaring class.
 protected
The data or methods can be accessed only by the declaring and
inherited class.

Note: The getter and setter methods are used to read and
modify private properties.
Note
An object cannot access its private members, as shown in (b). It is correct,
however, if the object is declared in its own class, as shown in (a).

public class Foo { public class Test {


private boolean x; public static void main(String[] args) {
Foo foo = new Foo();
public static void main(String[] args) { System.out.println(foo.x);
Foo foo = new Foo(); System.out.println(foo.convert(foo.x));
System.out.println(foo.x); }
System.out.println(foo.convert()); }
}

private int convert(boolean b) {


return x ? 1 : -1;
}
}
(b) This is wrong because x and convert are private in Foo.
(a) This is OK because object foo is used inside the Foo class
The protected Modifier

 The protected modifier can be applied on data and


methods in a class.
 A protected data or a protected method in a public class
can be accessed by any class in the same package or its
subclasses, even if the subclasses are in a different
package.
 private, default, protected, public

Visibility increases

private, default (if no modifier is used), protected, public

You might also like