Sometimes we have an abstract class, with no concrete method
Interface:
Pure abstract class
No concrete method
The return types are incompatible for the inherited methods B.f(), A.f()
Return types are the same and in fact we recognize only one abstract method in TestClass
why is this an error?
In name collision between parent and interface, the method in parent should be public
Why TestClass is not abstract anymore?
Interfaces may inherit other interfaces
Code reuse in Is-a relationship
No member variable
Variables: implicitly final and static
Usually interfaces do not declare any variables
Access to parent members
The super reference can be used to refer to the parent class
super.f() invokes f() from parent class
Why we need it?
When the method is overridden in subclass
super is also used to invoke the parent's constructor
Constructors are not inherited
even though they have public visibility
A child’s constructor is responsible for calling the parent’s constructor and it is done using super keyword
The first statement of a child’s constructor should be the super reference to call the parent constructor
Otherwise, default constructor is implicitly invoked
What if default constructor does not exist?
A syntax error
You should explicitly call an appropriate parent constructor
public class Circle extends Shape{
public static void main(String[] args){
new Shape();
}
}
class Shape{
Shape(){
System.out.println("A Shape is created!");
}
}
public class Circle extends Shape{
public static void main(String[] args){
new Circle();
}
}
class Shape{
Shape(){
System.out.println("A Shape is created!");
}
}
public class Circle extends Shape{
public static void main(String[] args){
new Shape();
}
Circle(){
System.out.println("A Circle is created!");
}
}
class Shape{
Shape(){
System.out.println("A Shape is created!");
}
}
public class Circle extends Shape{
public static void main(String[] args){
new Circle();
}
Circle(){
System.out.println("A Circle is created!");
}
}
class Shape{
Shape(){
System.out.println("A Shape is created!");
}
}
class Circle extends Shape{
Circle(){
System.out.println("A Circle is created!");
}
}
public class Shape{
Shape(){
System.out.println("A Shape is created!");
}
public static void main(String[] args){
new Circle();
}
}
class Circle extends Shape{
Circle(){
super();
if(shapeIsCalled)
System.out.println("A Circle is created!");
}
}
public class Shape{
boolean shapeIsCalled;
Shape(){
shapeIsCalled = true;
System.out.println("A Shape is created!");
}
public static void main(String[] args){
new Circle();
}
}
class Circle extends Shape{
boolean circleIsCalled;
Circle(){
super();
circleISCalled = true;
if(shapeIsCalled)
System.out.println("A Circle is created!");
}
}
public class Shape{
Shape(){
if(circleIsCalled)
System.out.println("A Shape is created!");
}
public static void main(String[] args){
new Circle();
}
}
class Circle{
Circle(){
this(1);
System.out.println("A Circle is created by the 1st constructor!");
}
Circle(int x){
System.out.println("A Circle is created by the 2nd constructor!");
}
public static void main(String[] args){
Circle c = new Circle();
}
}
class Circle extends Shape{
Circle(){
if(shapeIsCalled)
System.out.println("A Circle is created!");
}
}
public class Shape{
boolean shapeIsCalled;
Shape(){
shapeIsCalled = true;
System.out.println("A Shape is created!");
}
public static void main(String[] args){
new Circle();
}
}
class Circle extends Shape{
Circle(){
super(1.1);
if(shapeIsCalled)
System.out.println("A Circle is created!");
}
}
public class Shape{
boolean shapeIsCalled;
Shape(double d){
shapeIsCalled = true;
System.out.println("A Shape is created! r = " + d);
}
public static void main(String[] args){
new Circle();
}
}
class Circle extends Shape{
Circle(){
this(1.1);
System.out.println("A Circle is created by the 1st constructor!");
}
Circle(double x){
System.out.println("A Circle is created by the 2nd constructor!");
}
public static void main(String[] args){
Circle c = new Circle();
}
}
class Shape{
Shape(){
System.out.println("A Shape is created!");
}
}
class Circle extends Shape{
Circle(){
this(1.1);
System.out.println("A Circle is created by the 1st constructor!");
}
Circle(double x){
System.out.println("A Circle is created by the 2nd constructor!");
}
public static void main(String[] args){
Circle c = new Circle();
}
}
class Shape{
Shape(double d){
System.out.println("A Shape is created!");
}
}
class Circle extends Shape{
Circle(){
super(1.1);
this(1.1);
System.out.println("A Circle is created by the 1st constructor!");
}
Circle(double x){
System.out.println("A Circle is created by the 2nd constructor!");
}
public static void main(String[] args){
Circle c = new Circle();
}
}
class Shape{
Shape(double d){
System.out.println("A Shape is created! r = " + d);
}
}
class Circle extends Shape{
Circle(){
this(1.1);
System.out.println("A Circle is created by the 1st constructor!");
}
Circle(double x){
super(x);
System.out.println("A Circle is created by the 2nd constructor!");
}
public static void main(String[] args){
Circle c = new Circle();
}
}
class Shape{
Shape(double d){
System.out.println("A Shape is created " + d);
}
}
class Circle extends Shape{
Circle(double x){
super(x);
System.out.println("A Circle is created.");
}
void callMe(){
System.out.println("Circle's call me.");
}
public static void main(String[] args){
Circle c = new Circle(2.2);
}
}
class Shape{
double r;
Shape(double d){
this.r = d;
callMe();
System.out.println("A Shape is created " + d);
}
void callMe(){
System.out.println("Shape's call me.");
}
}
class Circle extends Shape{
Circle(double x){
super(x);
System.out.println("A Circle is created.");
}
void callMe(){
System.out.println("Circle's call me.");
}
public static void main(String[] args){
Shape s = new Shape(2.2);
}
}
class Shape{
double r;
Shape(double d){
this.r = d;
callMe();
System.out.println("A Shape is created " + d);
}
void callMe(){
System.out.println("Shape's call me.");
}
}
class Circle extends Shape{
double r = 3.3;
Circle(double x){
super(x);
}
void callMe(){
System.out.println("Circle's call me.");
}
public static void main(String[] args){
Shape s = new Shape(2.2);
}
}
class Shape{
double r = 2.2;
Shape(double d){
System.out.println("r is " + this.r);
this.r = d;
System.out.println("r is " + this.r);
callMe();
}
void callMe(){
System.out.println("Shape's call me.");
}
}
Section 10.3: super keyword