Tuesday, January 27, 2015

Java - Inheritance



\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

class A {
 //Members and methods declarations.
}

class B extends A {
 //Members and methods from A are inherited.
 //Members and methods declarations of B.
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super

Some questions to try out.



1) What is the result of attempting to compile & run the program?




2) 


3) 

class A {
A(int i) {
} // 1
}

class B extends A {
} // 2


4)

class A {
A(int i) {
System.out.print("Con A" + " ");
}
}

class B extends A {
B() {
System.out.print("Con B" + " ");
}
}

class Demo {
public static void main(String[] args) {
B b1 = new B();
}
}

5)

class First {
First(String s) {
System.out.println(s);
}
}

class Test extends First {
public static void main(String args[]) {
new Test();
}
}

6)


7)









No comments:

Post a Comment

Using Zotero for academic writing