Interface Price:
interface Price {
void printPrice(); }
AbstractTest class:
abstract class AbstractTest implements Price {
private String name, surname;
AbstractTest (String name, String surname){
this.name=name;
this.surname=surname; }
public String getName() {
return this.name; }
public String getSurname() {
return this.surname; } }
TestAbstractTest class:
class TestAbstractTest extends AbstractTest{
int price;
TestAbstractTest(int price, String name, String surname){
super(name, surname);
this.price=price; }
void printPrice() {
System.out.println("price:"+ price)// HOW CAN I ACCESS TO NAME AND SURNAME FROM AbstractTest CLASS }
Main class:
class Main {
public static void main(String args[]) {
AbstractTest[] abstracttest= new AbstractTest[2];
abstracttest[0]=new TestAbstractTest(5, xxx, yyy);
abstracttest[1]=new TestAbstractTest(10, aaa, zzz); }
I want to access the name and the surname from subclasses method printPrice.
I cant create an abstract method. I have to use printPrice from the interface Price. I've rearranged the constructor of TestAbstractTest like this (price, name, surname, AbstractTest abstracttest) and i've tried this.
abstracttest[0]= new AbstractTest(5, "xxx", "00yy", abstracttest[0])
But its obviously wrong. How can I access name and surname and print it from printPrice method?
When declaring or requesting a variable's value you can use get() or set() methods.
abstracttest[0]= new AbstractTest(5, this.getName(), this.getSurname(), abstracttest[0]);
System.out.println("price:"+ this.getPrice() );
You need to implement getPrice() of course.
Related
I want to program a factory that creates two types of People which are "Employee" and "Entrepreneur". They both share the same basic "Person" attributes but they also implements their unique ones.
The problem is that if i want to return a method or an attribute that is not declared in the "Person" abstract class the program doesn't find it (since obviously I'm generating an object that is type "Person" and not specifically "Employee" or "Entrepreneur" ).
How do i tackle this problem?
This is the Demo class
public class Demo{
public static void main(String[] args){
PersonFactory pf = new PersonFactory();
Person p1 = pf.getPerson("Employee");
p1.presentation();
System.out.println(p1.getComplanyName());
}
}
This is the abstract class
public abstract class Person{
String name;
String surname;
abstract void presentation();
}
Those are the two concrete classes that extend Person
public class Entre extends Person{
int licenseNumber;
#Override
public void presentation(){
System.out.println("hi i'm an Entrepreneur");
}
public int licenseNumber(){
return licenseNumber;
}
}
public class Empl extends Person{
String companyName;
#Override
public void presentation(){
System.out.println("hi i'm an employee");
}
public String getCompanyName(){
return companyName;
}
}
Finally the Factory
public class PersonFactory{
public Person getPerson(String type){
if(type.equalsIgnoreCase("ENTREPRENEUR")){
return new Entre();
}
else if(type.equalsIgnoreCase("Employee")){
return new Empl();
}
return null;
}
}
lets suppose i have this Parent Class
public abstract class Parent
{
private String name;
private String surname;
public Parent(String name, String surname)
{
this.name=name;
this.surname=surname;
}
and lets suppose i have many child classes like that and everyone of them has it's own different attributes to add to their parent ones
public class Child extends Parent
{
private String favColor;
public Child(String name,String surname,String favColor)
{
super(name,surname);
this.favColor=favColor;
}
public getFavColor()
{
return this.favColor
}
now i'm in this situation
Parent parent = new Child(name,surname,favColor);
and what i want to do is calling the method getFavColor() on the object parent like this
parent.getFavColor();
is this working? i guess not, so how could i be able to call such method on such object? i thought of declaring abstract getters of childs attributes on the superclass but that doesn't sound very prone to the open/closed principle, because in a time in future when i will want to add more child-like classes i will have to declare every getters of the child attributes in the superclass which is not supposed to know about his childrens.
thank you very much :)
In this case you can't call the getFavColor() method. The method is defined only in Child class and your reference is Parent. For this, is necessary the definition the getFavColor() method in Parent Class.
You would create a abstract method fav() in Parent class:
public abstract class Parent
{
private String name;
private String surname;
public Parent(String name, String surname)
{
this.name=name;
this.surname=surname;
}
public abstract String fav();
}
So called:
parent.fav();
Thus, you can implement the method in different ways on your children, such as:
public class Child extends Parent
{
private String favColor;
public Child(String name,String surname,String favColor)
{
super(name,surname);
this.favColor=favColor;
}
public String fav()
{
return this.favColor;
}
}
And:
public class SecondChild extends Parent
{
private String favSport;
public Child(String name,String surname,String favColor)
{
super(name,surname);
this.favColor=favColor;
}
public String fav()
{
return this.favSport;
}
}
Use this only if the signature of methods are equals in all children (in your case, if all children methods return a String).
You would need an abstract method to do that. Your parent is already abstract so that's good. It would go something like this:
public abstract class Parent {
private String name;
private String surname;
public Parent(String name, String surname) {
this.name = name;
this.surname = surname;
}
public void showFavColor() {
system.print.ln(this.getFavColor());
}
abstract string getFavColor();
}
public class Child extends Parent {
private String favColor;
public Child(String name, String surname, String favColor) {
super(name, surname);
this.favColor = favColor;
}
#Override
public String getFavColor() {
return this.favColor
}
}
Every child of the parent MUST extends the abstract function. Since the function is technically declared in the parent, it is accessible from it.
This means, you could do
Parent parent = new Child(name,surname,favColor);
parent.showFavColor();
package practice;
class person{
private String firstname;
private String lastname;
public person(String firstname,String lastname){
set_first(firstname);
set_last(lastname);
}
public String get_first() {
return firstname;
}
public void set_first(String firstname) {
this.firstname=firstname;
}
public void set_last(String lastname) {
this.lastname=lastname;
}
public String get_last() {
return lastname;
}
}
class employee extends person {
private int empid;
public employee(String firstname, String lastname, int empid) {
super(firstname,lastname);
set_empid(empid);
}
public void set_empid(int empid) {
this.empid=empid;
}
public int get_empid() {
return empid;
}
}
class testing_super_keyword {
public static void main(String args[]) {
employee emp=new employee("Paul","Anderson",1234);
System.out.println(emp.get_first()+" "+emp.get_last());
System.out.println(emp.get_empid());
}
}
I got two classes here person superclass and employee subclass. So i just wanted to know this code isn't supposed to work as the firstname and lastname variable is private in superclass? But how the subclass i.e employee is inheriting those members and using it??
I thought private variable of superclass cant be inherited so how come it works fine here?
I am totally confused please help......
Although, the private variables of parent class are not inherited by child class i.e employee but there are public functions that are called getter and setter that allows to access the private members of class from its sub-class.
public String get_first() {
return firstname;
}
public void set_first(String firstname) {
this.firstname=firstname;
}
public void set_last(String lastname) {
this.lastname=lastname;
}
public String get_last() {
return lastname;
}
You see when you want to access firstname from parent, you'll call get_first() from employee object to get the firstname. If you want to set the firstname you'll call set_first("name") to set the name. Hope it might help.
private variable of superclass cant be inherited
Yes, you are absolutely right, they won't be inheriting. But in your code, you are not accessing those fields directly right?
public employee(String firstname,String lastname,int empid){
super(firstname,lastname);
set_empid(empid);
}
Here you are passing parameters (from the main method) to your employee constructor, the names of parameters are similar to the fields in person, but they are not the same. You can change parameters names like this and it will still work fine.
public employee(String fName, String lName,int empid){
super(fName,lName);
set_empid(empid);
}
Here the parameter values are taken into super class constructor and its private fields are initialized and then empid of employee is initialized.
System.out.println(emp.get_first()+" "+emp.get_last());
System.out.println(emp.get_empid());
Here also you are not accessing the private fields directly, you are calling the public methods which will be inherited to the employee and can be called on its reference.
Keeping members of a class as private and their behaviours (methods) as public is a part of encapsulation, so that you cannot directly access then but can set and get its value using public methods.
PS : Try to make the getters and setters using the IDE, if it gives the option and try to follow naming conventions for classes and methods.
lets review how firstname and lastname are being initialized here:
in subclass constructor firstname, lastname has been taken
then in the body of constructor by super(firstname,lastname), firstname and lastname pass to the parent (i.e. Person) to be taken care of.
so assume we are now in the parent constructor (Person) so, By calling set_first(firstname) and set_last(lastname) , parameters firstname and lastname is being set in parent class itself with the values which passed from the child constructor (i.e. Employee)
Regarding this description there is no violation.
The violation happens if you want to initialize private variable directly in Employee class as :
public employee(String firstname,String lastname,int empid){
this.firstname=firstname; //Violation
this.lastname=lastname; //Violation
set_empid(empid);
}
So I have created a superclass called "Food" which takes a String called "name" as a parameter, I now want to created an inherited class called "Meat" and give that class the name of "Meat" which it inherits from the Food class. Here is my current attempt:
public class Meat extends Food
{
public Meat(String name) {
String meat = name;
}
public String getName() {
return name;
}
}
Here is the Food class:
public class Food {
//field that stores the name of the food
public String name;
//constructor that takes the name of the food as an argument
public Food(String name){
this.name = name;
}
public String getName() {
return name;
}
}
I'm not currently sure how to write out the Meat constructor to assign the name. Any help is appreciated, thanks.
You need to call the super constructor from the child constructor:
public class Meat extends Food {
public Meat(String name) {
super(name);
}
}
Read the tutorial about super.
I don't see the added value of having the Meat class, unless you override methods or add fields or methods. You could just define your meat as Food:
Food meat = new Food("Meat");
But if you do extend the Food class, your Meat class will have to call one of Food's constructors. The easiest way to do that is by having a Constructor with the same signature:
public Meat(String food){
super(food);
}
Note that every Java class has a constructor. If you don't define one explicitly, the compiler inserts a constructor with public visibility and without parameters.
You won't need to do anything else with the getName().
In the Meat class you can create the constructor like this:
public Meat(String name) {
super(name);
}
But if you want to override it then you can do something like this:
#Override
public String getName() {
String name = "Meat" + this.name;
return name;
}
If I understand correctly, you want all your Meat objects to have "Meat" as name. You should do this:
public class Meat extends Food {
public Meat() {
super("Meat");
}
}
I have a Java program where I have a main class and another class called Person (that makes a 'person' class) with 2 methods. The methods are non-static and are called getName and getAge that stores this info a person element of an ArrayList.
How do I call these in the main program? I know I have to declare an instance of the class but I'm still not sure how to actually do it.
Considering Person a Class with 2 methods:
public class Person{
private String mName;
private int mAge;
public Person(String name, String age){
this.mName = name;
this.mAge = age;
}
//If you want this class to be Immutable please remove the setter methods()//
public void setName(String name){
this.mName = name;
}
public void setAge(String age){
this.mAge = age;
}
public String getName(){
return this.mName ;
}
public String getAge(){
return this.mAge ;
}
}
The Class containing main() method:
public class TestMain{
public static ArrayList<Person> aList = new ArrayList<Person>();
public static void main(String[] args){
Person person1 = new Person("Vivek",26);
Person person2 = new Person("Vicky",27);
aList.add(person1);
aList.add(person2);
}
}
In your main code you can do something like:
Person p = new Person();
p.setName("Jericho Jones");
p.setAge(153);
Of course, it's impossible to tell exactly without seeing the Person class.
You need to instantiate an object for this class.
Person p = new Person();
p.getName()....