0% found this document useful (0 votes)
98 views2 pages

07 Handout 1 (OOP)

This document defines and explains the rules for abstract classes and abstract methods in Java. It states that abstract classes cannot be instantiated directly, may contain non-abstract methods and variables, and cannot be marked as final. Abstract methods can only be defined in abstract classes, cannot be marked as final or private, and must be implemented by subclasses. The first concrete subclass of an abstract class must implement all inherited abstract methods.

Uploaded by

See Horse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views2 pages

07 Handout 1 (OOP)

This document defines and explains the rules for abstract classes and abstract methods in Java. It states that abstract classes cannot be instantiated directly, may contain non-abstract methods and variables, and cannot be marked as final. Abstract methods can only be defined in abstract classes, cannot be marked as final or private, and must be implemented by subclasses. The first concrete subclass of an abstract class must implement all inherited abstract methods.

Uploaded by

See Horse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

IT1908

ABSTRACT CLASSES 2. An abstract class may include non-abstract methods and


variables.
Fundamentals Example:
• An abstract class is created to ensure that subclasses will public abstract class Pet {
implement necessary methods. public abstract void eat();
Example 1: public void drink() {
public abstract class Pet { }
public abstract void eat(); }
}
3. An abstract class cannot be marked as final because a final
public class Cat extends Pet {
class cannot be extended by another class.
public void eat() {
} Example: public final abstract class Pet { }
} 4. An abstract class that extends another abstract class inherits
Explanation: The Cat class extends its parent class, Pet. all of its abstract methods as its own abstract methods.
Hence, it has to use the eat() method. 5. The first concrete class (non-abstract class that extends an
Example 2: abstract class) is required to implement all inherited abstract
public abstract class Pet { methods.
public abstract void eat(); Example:
} public abstract class Animal {
public class Cat extends Pet { //does not compile public abstract void sleep();
} }
public abstract class Pet extends Animal {
Explanation: The Cat class will not compile because it has to public abstract void eat();
use the eat() method since it is declared in its parent class, }
Pet. public class Cat extends Pet {
public void sleep() {
• An abstract method has no body and is required to be
}
implemented by subclasses. It is declared without braces but
public void eat() {
with a semicolon.
}
Example: abstract void draw(); }

Rules in Defining Abstract Classes Rules in Defining Abstract Methods


1. An abstract class cannot be instantiated directly.
1. An abstract method may only be defined in an abstract class.
public abstract class Pet {
Example:
public static void main(String[] args) {
public class Pet {
Pet p = new Pet();
public abstract void eat(); //does not compile
}
}
}

07 Handout 1 *Property of STI


[email protected] Page 1 of 2
IT1908

2. An abstract method cannot be marked as final because a final


method cannot be overridden in a subclass.
Example: public final abstract void draw();
3. An abstract method cannot be marked as private since that
method is only accessible to the class where it belongs to.
Example: private abstract void eat();
4. To override an abstract method, declare a new method with
the same name, parameter list, and return type as the method
in the parent class. The method in the subclass must be at
least as accessible as the method in the parent class.
Example:
public abstract class Pet {
public abstract int getAge();
}
public class Cat extends Pet {
public double getAge() {
return 5.0;
}
}

References:
Oracle Docs (n.d.). Citing sources. Retrieved from
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
Savitch, W. (2014). Java: An introduction to problem solving and programming (7th ed.).
New Jersey: Pearson Education, Inc.

07 Handout 1 *Property of STI


[email protected] Page 2 of 2

You might also like