4. What is an abstract class? Can you instantiate objects of an abstract class?
5. Give an example of an Inheritance (is-a) relationship. No code needed just give an example in a simple English sentence.
6. Give an example of a Composition (has-a) relationship. No code needed just give an example in a simple English sentence.
JAVA
4. Abstract comes from the concept of abstraction which means only showing only functionality to the user and hiding internal details.
Abstract Class
JAVA uses abstract keyword for abstract class.
may contain abstract and non abstract methods.
may contain final and non-final, static non- static methods.
should be extended using extends keyword.
A Java abstract class also cannot be instantiated, that is it's object can not be created but can be invoked through main() method.
5)
Inheritance (is -a) relationship which works on concept of base class and derived class where base class have common functionalities and derived class comes with distinct specific feature.
Supports code re-usability.
Derived class can use data members(instance variables) and methods of the base class.
Example:
Base Class - Person with attributes ( name, age, phone, weight, height. many features)
Derived Classes - Employee can have additional specific attributes (profession, qualification)
where Employee is -a person with distinct attributes
and every Person need not be an Employee but every Employee is a Person.
In above example, Person class contains all the data members and methods common to all Person
But Employee class will have specific feature of it's own and common feature of a person can be accessed in Person class.
(Code re-usability)
We have different type of inheritance in java
1) Single - A single base class and derived class.
2) Multilevel - A base class , derived class and derived class of derived class.
3) Hierarchical - A base class having more than one derived classes.
6) Composition
designed to implement has-a relationship in classes. Polymorphic behavior to handle many forms.
A class reuses the functionality by creating a reference to the object of the class it wants to reuse.
Example :
Base Class - Shape
Derived Classes - Triangle , Rectangle
Explanation :
Shape class can have its common feature of Shape .
Data member :- Area, Perimeter
Methods :- getArea(), getPerimeter()
Further, Derived classes can use these data member and methods to implement its own specific feature.
As Rectangle - 4 sides, Triangle - 3 sides
Formula to calculate area and perimeter of above two is different.
this implies, they can use methods of base class and implement own distinct definition to calculate
Area and Perimeter using getArea() and getPerimeter().
Thus here, Rectangle and Triangle has method of Shape class.
4. What is an abstract class? Can you instantiate objects of an abstract class? 5. Give...
Consider the following Scala code that uses classes, objects, inheritance and dynamic method binding abstract class A { def m) Int 1 val a Int = 2 class B extends A { val b: String "one" override def m() : Int = 3 def n(): Int 4 class C extends A { val x : Int 5 override def m() : Int = 6 val array an array of ten A objects array (5) val obj: A obj.m() = (a)...
Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...
creat a simple theater booking system with java language using: 1. oop objects - classes 2. encapsulation 3. inheritance 4. polymorphism 5. abstract class
what is abstract class in java? for example, abstract class animal and subclass cat which extends abstract class, and the test class which will call cat class to show if it yawns, scratch and whatnot. I used to create class animal but it was not abstract, so I am not sure the purpose of it. Another question, I created interface, by right clicking the package, I think the teacher saying interface is acts independent? and not part of hierarchy like...
What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...
JAVA CODING:
(14 points) Abstract Class exercise You are given two abstract classes with no abstract method in the NoGo.java file. The purpose of this exercise is to get familiar with abstract class and its object creation 4. Follow the steps below: (be sure common on each to score points) 1) 2) 3 points) Create a subclass Go1 with Nogo1 as its super class and create Go1() constructor 3) (2 points) Now inside the NoGo class create an instance of...
For Java, 1. What is an abstract method? How is an abstract method created? 2. What is an abstract class? 3. Can an object of an abstract class be instantiated? 4. Does a superclass have access to the members of subclass? Does a subclass have access to the members of the superclass? 5. How do you prevent a subclass from having access to a member of a superclass? 6. Given the following hierarchy: class Alpha{ … class Beta extends Alpha...
You are given a specification for some Java classes as follows. A building has a number of floors, and a number of windows. A house is a building. A garage is a building. (This isn’t Florida-like … it’s a detached garage.) A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space,...
using java File Details – Build a class called FileDetails.java. When you instantiate this class and give it a filename, it will report back the size of the file, whether the file is Readable and whether the file is Writeable; plus any other file information that you might deem important. This cd goes in main FileDetails fd=newFileDetails(“anyfile.doc”); All other code goes in the constructor. Write a String to a File using PrintStream – This time build a class WriteString. This...
In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...