Class Inheritance with Java Examples

inheritance n.w
1 / 21
Embed
Share

Explore the concept of class inheritance in Java through examples of creating Student and Professor classes. Learn how properties and methods are defined, and see how common code can be factored into a superclass for efficient code organization.

  • Java Basics
  • Class Inheritance
  • Object-Oriented Programming
  • Java Examples
  • Superclass

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Inheritance COMP 301 ( adapted from Drs. K. Mayer-Patel and Aaron Smith )

  2. A few formal definitions for this class A property of an object is a value that can be strictly computed from its fields. A method is a getter if it retrieves a property value and adheres to the getter conventions: type getPropertyName() A method is a setter if it updates a property value and adheres to the setter conventions: void setPropertyName(type new_value); Properties can be direct or derived. Direct if the value is stored in a single field of the same type. Derived if the value must be computed from one or more field values. May be implementation specific. The members of a class are its fields and methods. Constructors are NOT members This slide is law. Even if an I or an LA say or accidentally say or write something to the contrary.

  3. Inheritance Factoring common code into a superclass

  4. Motivation Let s design a university management system Make a Student class and a Professor class Track attributes about each person Name Address Birthday Credit hours Student rank (freshman, sophomore, junior, senior) Professor rank (assistant, associate, full)

  5. Model a university student public class Student { private String name; private int credits; public Student(String name) { credits = 0; this.name = name; } We re going to omit the address and birthday fields because they don t fit on the slide public String getName() { return name; } public int getCredits() { return credits; } public void takeCredits(int credits) { this.credits += credits; } public String getStatus() { if (credits < 30) { return "Freshman"; } else if (credits < 60) { return "Sophomore"; } else if (credits < 90) { return "Junior"; } else { return "Senior"; } } }

  6. Model a professor public class Professor { private String name; private int status; public Professor(String name) { status = 0; this.name = name; } public String getName() { return name; } public void promote() { if (status == 2) { throw new RuntimeException("Can't promote"); } status++; // Adds 1 to the value of status } public String getStatus() { if (status == 0) { return "Assistant"; } else if (status == 1) { return "Associate"; } else { return "Full"; } } }

  7. public class Student { private String name; private int credits; public class Professor { private String name; private int status; public Student(String name) { credits = 0; this.name = name; } public Professor(String name) { status = 0; this.name = name; } public String getName() { return name; } public String getName() { return name; } public int getCredits() { return credits; } public void promote() { if (status == 2) { throw new RuntimeException("Can't promote"); } status++; // Adds 1 to the value of status } public void takeCredits(int credits) { this.credits += credits; } public String getStatus() { if (status == 0) { return "Assistant"; } else if (status == 1) { return "Associate"; } else { return "Full"; } } } public String getStatus() { if (credits < 30) { return "Freshman"; } else if (credits < 60) { return "Sophomore"; } else if (credits < 90) { return "Junior"; } else { return "Senior"; } } }

  8. Inheritance diagram Person contains the members common to both Student and Professor Person superclass Student subclass Professor subclass Student and Professor inherit all Person members

  9. 1. Superclass public class Person { private String name; It s just a regular class containing the common fields and methods public Person(String name) { this.name = name; } public String getName() { return name; } }

  10. 2. Subclass public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } } Use extends to declare a subclass Even though the class body is empty, a Student is a Person, so it automatically inherits all Person members public class Student extends Person { }

  11. 3. Subclass constructors public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } } Use super() as the first line in the subclass constructor to call the superclass constructor public class Student extends Person { public Student(String name) { super(name); } } In other words, super(name) is analogous to calling new Person(name)

  12. 4. Inheritance public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } } Every Student instance automatically has a name field and a getName() method Every Professor instance automatically has a name field and a getName() method public class Student extends Person { public Student(String name) { super(name); } } public class Professor extends Person { public Professor(String name) { super(name); } }

  13. Complete Student subclass public class Student extends Person { private int credits; public Student(String name) { super(name); credits = 0; } Use super() to construct the Person portion of the object public int getCredits() { return credits; } public void takeCredits(int credits) { this.credits += credits; } public String getStatus() { if (credits < 30) { return "Freshman"; } else if (credits < 60) { return "Sophomore"; } else if (credits < 90) { return "Junior"; } else { return "Senior"; } } }

  14. Complete Professor subclass public class Professor extends Person { private int status; public Professor(String name) { super(name); status = 0; } Use super() to construct the Person portion of the object public void promote() { if (status == 2) { throw new RuntimeException("Can't promote"); } status++; // Adds 1 to the value of status } public String getStatus() { if (status == 0) { return "Assistant"; } else if (status == 1) { return "Associate"; } else { return "Full"; } } }

  15. Using subclasses Make new Professor and Student instances Professor emily = new Professor("Emily"); Student alex = new Student("Alex"); Both instances have a public getName() method System.out.println(emily.getName()); System.out.println(alex.getName()); alex.takeCredits(30); System.out.println(alex.getStatus()); // "Sophomore" Student instances can takeCredits() emily.promote(); System.out.println(emily.getStatus()); // "Associate" Professor instances can promote()

  16. Subclasses in memory Heap program memory Professor instance What happens in memory when the Professor constructor is called? Person instance 1. Allocate memory on the heap to store instance fields 2. Construct Person with super() 3. Construct Professor in memory after Person name new Professor("kmp"); status 0 public Professor(String name) { super(name); status = 0; } String instance k m p public Person(String name) { this.name = name; }

  17. Subclasses in memory Heap program memory kmp Professor instance Person instance The returned reference could be declared as type Professor name Professor kmp = new Professor("kmp"); status 0 String instance k m p

  18. Subclasses in memory Heap program memory Professor instance kmp Person instance name Person kmp = new Professor("kmp"); status 0 String instance or it could be declared as type Person k m p This is an example of subtypepolymorphism

  19. Extending interfaces Similar idea applies to interfaces An extended interface is one that adds methods to an existing interface. A class that implements an extended interface is required to provide methods that implement the interface AND its parent(s) Interface extension appropriate when additional methods make no sense without methods of the parent interface. public interface CompressedMedia extends Media { int getCompressedSize(); int getUncompressedSize(); Media uncompress(); } public interface Media { int getLengthInSeconds(); double getLengthInMinutes(); int getRating(); void setRating(int new_rating); String getName(); }

  20. Multiple Inheritance For Interfaces Subclasses can only have one parent class This is a Java restriction. Other OOP languages like C++ support multiple inheritance. Multiple inheritance for interfaces IS allowed. Subinterface is a union of all methods declared in all of the parent interfaces. Plus, of course, any additional ones added by the subinterface. Useful when you want to program to a type that is a combination of interfaces. The subinterface provides a type name for the combination.

  21. public interface Trackable { PointInSpace getPosition(); Vector getVelocity(); Vector getAcceleration(); } public interface Tossable { public void tossTo(PointInSpace target); } Suppose we want to write a function that juggles three objects. What does that function declaration look like? static void juggle(Tossable obj1, Tossable obj2, Tossable obj3); static void juggle(Trackable obj1, Trackable obj2, Trackable obj3); public interface Jugglable extends Tossable, Trackable { } static void juggle(Jugglable obj1, Jugglable obj2, Jugglable obj3);

Related


More Related Content