Slide10_InheritancePart1.pptx

Full Transcript

Inheritance Part 1 COP 3330 Department of Computer Science University of Central Florida Dr. Bacanli What is inheritance? All of us inherit qualities from our parents. Genes Qualities Etc… Object-Oriented Languages allows classes to follow this behavio...

Inheritance Part 1 COP 3330 Department of Computer Science University of Central Florida Dr. Bacanli What is inheritance? All of us inherit qualities from our parents. Genes Qualities Etc… Object-Oriented Languages allows classes to follow this behavior in implementation. Inheritance is when a new class is created by acquiring an existing class’s members and possibly embellishing them with new or modified capabilities. How does this work in OOP? First lets understand some terminology. The newly created class we create comes from an existing class. This existing class is known as the super class or parent class. The newly created class is known as the sub class or child class. Many languages use different terminologies. C++ uses derived class as the child class and base class as the super class. Java only supports single inheritance, in which each class is derived from EXACTLY one super class. The child class inherits ALL attributes and ALL methods that are defined in the class. Inheritance example package carEx; package carEx; public class Car extends Vehicle{ public class Vehicle{ private String brandName; private int wheelNumber; public Car(String bname){ public Vehicle(){ super(); wheelNumber=4; brandName=bname; System.out.println(“Vehicle is created”); System.out.println(“Car is created”); } } public int getWheelNumber(){ public void go(){ return wheelNumber; System.out.println(brandName+” is } going on ” +getWheelNumber()+” wheels”); } } } package carEx; public class vehiclesTester{ public void go(){ public static void main(String[] args){ System.out.println(brandName+ Car myCar = new Car(“Ford”); ” is going on ” + super.getWheelNumber() myCar.go(); +” wheels”; } } } Vehicle is created Car is created Ford is going on 4 wheels Method overriding Method overriding is when we have defined methods with the same and same signature When we create the the same parent method in the child class, we are using “method overriding” methods should have @Override before definition Not obligatory Can we override private method of the parent class? Do private methods inherit? Method overriding rules The parameter list must not change: the overriding method must take the same number and type of parameters as the overridden method – otherwise, you would just be overloading the method. The return type must not change (Note: if the method returns an object, a subclass of that object is allowed as the return type). The access modifier must be either the same or a less restrictive one (for example, if the overridden method is protected, you can declare the overriding method as public, but not private). Method overriding example package carEx; package carEx; public class Car extends Vehicle{ public class Vehicle{ private String bname; private int wheelNumber; public Car(String brandName){ public Vehicle(){ super(); wheelNumber=4; bname=brandName; } } public void go(){ @Override System.out.println(“Vehicle going”); public void go(){ } System.out.println(bname +” is going”); } } package carEx; } public class vehiclesTester{ public static void main(String[] args){ public void go(){ Car myCar = new Car(“Ford”); super.go(); // method of super class Vehicle veh = new Vehicle(); } veh.go(); myCar.go(); } } Method overriding example package carEx; package carEx; public class Car extends Vehicle{ public class Vehicle{ private String brandName; private int wheelNumber; public Car(String bname){ public Vehicle(){ super(); wheelNumber=4; brandName=bname; } } public Vehicle getSelf(){ @Override return this; public Car getSelf(){ } return this; } } package carEx; } public class vehiclesTester{ public static void main(String[] args){ Car myCar = new Car(“Ford”); Vehicle veh = new Vehicle(); veh.go(); myCar.go(); } } Method overloading Method overloading is when we have defined methods with the same name but different signature In fact we are creating a different method We call it override because the name is same The method parameters must change: either the number or the type of parameters must be different in the two methods. The return type can be freely modified. The access modifier (public, private, and so on) can be freely modified. Can we overload private method of the parent class? Method overloading example package carEx; package carEx; public class Car extends Vehicle{ public class Vehicle{ private String brandName; private int wheelNumber; public Car(String bname){ public Vehicle(){ super(); wheelNumber=4; brandName=bname; } } public void go(){ protected void go(int numberOfCars){ System.out.println(“Vehicle going”); System.out.println(numberOfCars+” cars } are going”); } } package carEx; } public class vehiclesTester{ public static void main(String[] args){ Car myCar = new Car(“Ford”); Vehicle veh = new Vehicle(); veh.go(); myCar.go(4); } } final method and final class A final class cannot be extended by any other class Your final class can not have children If all the constructors are private, the class will be final. Why? If the constructor is final, super() will not work! Check Java API, there are some final classes A final method cannot be overridden You can still overload them They are still inherited. Access Modifiers and statics All methods/variables except private are inherited. For static methods, They can NOT be overriden The parent static methods can still be called How can you determine if inheritance is even needed? Is-a relationship represents inheritance Dog is a Animal Bird is a Animal Cat is a Animal Student is a Human Employee is a Human In fact it is more like is a kind of relationship, an object of a subclass can also be treated as an object of its super class. If you feel like overriding methods will work nicely In a has-a relationship, an object contains as members references to other objects. This we have seen already! static method inheritance package carEx; example package carEx; public class Car extends Vehicle{ public class Vehicle{ private String brandName; private int wheelNumber; public Car(String bname){ public Vehicle(){ super(); wheelNumber=4; brandName=bname; } } public static void foo() { public static void foo() { System.out.println("Vehicle static"); System.out.println("Car static"); } } } } package carEx; public class vehiclesTester{ Car static public static void main(String[] args){ Car static Car myCar = new Car(2); Vehicle static myCar.foo(); Car.foo(); Vehicle.foo(); //this will still work } } CommunityMember void go() void Employee Alumnus void go() go() No multiple inheritance! Staff Faculty void go() Administrat or Teacher The Super Class of them All All Classes are derived from the Object Class. Think about it? How were we able to call the toString method? Clone, equals,toString,hashCode etc.. are all inherited We were overriding them toString We have seen this method on countless occasions now. String representation of the object – brand year is 1999, Car is not runnable Makes our job easy if we want to debug Always write toString() method for your class Make it always public String toString() DO NOT MAKE IT VOID! – What happens if you do? when you don’t write toString() Car class again inside print, java does that for // Car.java you! // CarTest.java package testCars; package testCars; public class Car extends Object{ public class CarTest{ private int year; //instance variable public static void main(String[] args){ private String brand; //instance variable Car mycar = new Car(); System.out.println(mycar.toString()); public Car(){ System.out.println(mycar); super(); }//end of main year=1999; } brand="Toyota"; } public String toString(){ Brand is Toyota Year is 1999 return "Brand is " +brand+ Brand is Toyota Year is 1999 " Year is " + year; } }

Use Quizgecko on...
Browser
Browser